003-单例OR工厂模式
单例模式:DbContextFactory.cs
using CZBK.ItcastOA.Model;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Runtime.Remoting.Messaging;
using System.Text;
using System.Threading.Tasks; namespace CZBK.ItcastOA.DAL
{
public class DbContextFactory
{
/// <summary>
/// 保证EF上下文实例线程内唯一.
/// </summary>
/// <returns></returns>
public static DbContext CreateDbContext()
{
DbContext dbContext = (DbContext)CallContext.GetData("db");
if (dbContext == null)
{
dbContext = new OAEntities();
CallContext.SetData("db", dbContext);
}
return dbContext;
}
}
}
public class Singleton
{
private static Singleton _instance = null;
private Singleton(){}
public static Singleton CreateInstance()
{
if(_instance == null)
{
_instance = new Singleton();
}
return _instance;
}
}
工厂模式:SimpleFactory.cs
using CZBK.Shop.IDal;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace CZBK.Shop.DalFactory
{
/// <summary>
/// 工厂模式:解决的就是对象的创建(将对象的创建封装起来)
/// </summary>
public class SimpleFactory
{
public static IUserInfoDal CreateUserInfoDal()
{
return new SqlServerDal.UserInfoDal();
}
}
}
抽象工厂模式:AbstractFactory.cs
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks; namespace CZBK.Shop.DalFactory
{
/// <summary>
/// 抽象工厂:本质上与工厂是一样的,都是负责创建对象,但是创建的方式不一样。
/// </summary>
public class AbstractFactory
{
private static readonly string DalAssembly = ConfigurationManager.AppSettings["DalAssembly"];
private static readonly string DalNameSpace = ConfigurationManager.AppSettings["DalNameSpace"];
public static IDal.IUserInfoDal CreateUserInfoDal()
{
//通过反射的形式来创建.
string fullClassName = DalNameSpace + ".UserInfoDal";
return CreateInstance(fullClassName) as IDal.IUserInfoDal;
}
private static object CreateInstance(string fullClassName)
{
var assmebly = Assembly.Load(DalAssembly);//加载程序集.
return assmebly.CreateInstance(fullClassName);
}
}
}
003-单例OR工厂模式的更多相关文章
- Redis 单例、主从模式、sentinel 以及集群的配置方式及优缺点对比(转)
摘要: redis作为一种NoSql数据库,其提供了一种高效的缓存方案,本文则主要对其单例,主从模式,sentinel以及集群的配置方式进行说明,对比其优缺点,阐述redis作为一种缓存框架的高可用性 ...
- PHP设计模式之工厂/单例/注册者模式
工厂模式 简单工厂模式 [静态工厂方法模式](Static Factory Method)是类的创建模式 工厂模式的几种形态: 1.简单工厂模式(Simple Factory)又叫做 静态工厂方法模式 ...
- JavaScript 创建对象之单例、工厂、构造函数模式
01单例模式 首先看一个问题,我们要在程序中描述两个人,这两个人都有姓名和年龄,可能刚刚开始学习js的时候会写成这样: var name1 = 'iceman'; var age1 = 25; var ...
- oop的三种设计模式(单例、工厂、策略)
参考网站 单例模式: 废话不多说,我们直接上代码: <?php /** 三私一公 *私有的静态属性:保存类的单例 *私有的__construct():阻止在类的外部实例化 *私有的__clone ...
- java面试记录一:跳表、判断二叉树相同、冒泡排序、cookie和session的区别、设计模式(单例、工厂、模板方法、原型、代理、策略)、抽象类与接口的区别
1.什么是跳表? 跳表实际上就是多层链表 跳表可用在让链表的元素查询接近线性时间 代码结构及java实现参考博客园随笔 2.判断两棵二叉树是否相同?(结构相同,内容相同) 思路:(1)先定义树节点Tr ...
- Swift百万线程攻破单例(Singleton)模式
一.不安全的单例实现 在上一篇文章我们给出了单例的设计模式,直接给出了线程安全的实现方法.单例的实现有多种方法,如下面: class SwiftSingleton { class var shared ...
- JavaScript设计模式 样例一 —— 工厂模式
工厂模式(Factory Pattern): 定义:定义一个创建对象的接口,但让实现这个接口的类来决定实例化哪个类.工厂方法让类的实例化推迟到子类中进行. 目的:工厂模式是为了解耦,把对象的创建和使用 ...
- Redis单例、主从模式、sentinel以及集群的配置方式及优缺点对比
https://my.oschina.net/zhangxufeng/blog/905611
- PHP设计模式-工厂模式、单例模式、注册模式
本文参考慕课网<大话PHP设计模式>-第五章内容编写,视频路径为:http://www.imooc.com/video/4876 推荐阅读我之前的文章:php的设计模式 三种基本设计模式, ...
随机推荐
- Yarn集群的搭建、Yarn的架构和WordCount程序在集群提交方式
一.Yarn集群概述及搭建 1.Mapreduce程序运行在多台机器的集群上,而且在运行是要使用很多maptask和reducertask,这个过程中需要一个自动化任务调度平台来调度任务,分配资源,这 ...
- Microsoft Office Word 中的公式自动编号
先插入公式,#,插入题注(交叉引用),生成了标号.此时整个公式是题注样式.在公式和标号之间插入一个样式分隔符. ____________________________________________ ...
- SAS 对数据的拼接与串接
SAS 对数据的拼接与串接 使用SAS对数据进行串接.合并.更新与修改. 1. 数据集的纵向串接 数据集的纵向串接指的是,将两个或者多个数据集首尾相连,形成 一个新的数据集. 对数据集的纵向串接可以通 ...
- webpack报错运行时没有定义
一.问题描述 ReferenceError: regeneratorRuntime is not defined 二.问题分析 缺少regenerator的运行时库,具体原理,可查看babel文章. ...
- jmeter之JDBC Request各种数据库配置
URL和JDBC驱动: Datebase Driver class Database URL MySQL com.mysql.jdbc.Driver jdbc:mysql://host:port/{d ...
- 通用导出excel
循环导出所有行和列 def export_excel(table_name): host,user,passwd,db='192.168.0.12','root','myjcyf','us_sys' ...
- VS的Mvc项目右键没有控制器右键菜单(转)
今天遇到了一个比较少见的问题,我用vs2012打开一个从Svn上拉下来的mvc5项目,在Controller文件夹上右键却发现没有新建控制器的选项,在View文件夹上右键也没有新建视图的选项. 我的第 ...
- C++调用C语言的库函数
在项目中,使用C语言编写了一个socket后台程序tkcofferd,并且为方便客户端的使用,提供了动态库,其中包含socket接口. 现在的需求是使用qt做一个前端界面,用来展示tkcofferd的 ...
- ORACLE中通过SQL语句(alter table)来增加、删除、修改字段
1.添加字段: alter table 表名 add (字段 字段类型) [ default '输入默认值'] [null/not null] ; 2.添加备注: comment on ...
- Web API Request Content多次读取
使用自宿主OWIN 项目中要做日志过滤器 新建类ApiLogAttribute 继承ActionFilterAttribute ApiLogAttribute : ActionFilterAttri ...