业务层调用数据层对象,我不想每次都new一个数据层对象,而是在数据层创建一个仓储,统一管理所有的对象调用。

1、在IDAL项目中,新建IDBSession.tt模板

 

Ctrl+S后自动生成IDBSession接口

2、在DAL项目中实现IDBSession接口

新建DBSession.tt模板

Ctrl+S后自动生成DBSession类

接下来,我们创建DBSession工厂和上下文工厂,目的是为了提高效率,在线程中共用一个对象。

3、IDAL项目中添加IDBSessionFactory接口

namespace IDAL
{
/// <summary>
/// 数据仓储工厂
/// </summary>
public interface IDBSessionFactory
{
IDBSession GetDBSession();
}
}

DAL项目中添加DBSessionFactory类继承IDBSessionFactory接口

using System.Runtime.Remoting.Messaging;
using IDAL; namespace DAL
{
public class DBSessionFactory : IDBSessionFactory
{
/// <summary>
/// 此方法的作用: 提高效率,在线程中 共用一个 DBSession 对象!
/// </summary>
/// <returns></returns>
public IDBSession GetDBSession()
{
//从当前线程中 获取 DBContext 数据仓储 对象
IDBSession dbSesion = CallContext.GetData(typeof(DBSessionFactory).Name) as DBSession;
if (dbSesion == null)
{
dbSesion = new DBSession();
CallContext.SetData(typeof(DBSessionFactory).Name, dbSesion);
}
return dbSesion;
}
}
}

4、同样,我们再来创建一个上下文工厂,即便以后有多个数据库上下文,也能够很好的支持。

IDAL项目中,新建IDBContextFactory.cs接口

using System.Data.Entity;

namespace IDAL
{
/// <summary>
/// EF数据上下文 工厂
/// </summary>
public interface IDBContextFactory
{
/// <summary>
/// 获取 EF 上下文对象
/// </summary>
/// <returns></returns>
DbContext GetDbContext();
}

DAL项目中新建DBContextFactory类继承IDBContextFactory接口

using System.Data.Entity;
using System.Runtime.Remoting.Messaging;
using Model; namespace DAL
{
public class DBContextFactory : IDBContextFactory
{
#region 创建 EF上下文 对象,在线程中共享 一个 上下文对象 + DbContext GetDbContext()
/// <summary>
/// 创建 EF上下文 对象,在线程中共享 一个 上下文对象
/// </summary>
/// <returns></returns>
public DbContext GetDbContext()
{
////从当前线程中 获取 EF上下文对象
var dbContext = CallContext.GetData(typeof(DBContextFactory).Name) as DbContext;
if (dbContext == null)
{
dbContext = new OAEntities();
CallContext.SetData(typeof(DBContextFactory).Name, dbContext);
}
return dbContext;
}
#endregion
}
}

5、Common项目中,添加ConfigurationHelper.cs来操作配置文件

using System;
using System.Configuration; namespace Common
{
public static class ConfigurationHelper
{
public static string AppSetting(string key)
{
return ConfigurationManager.AppSettings[key];
}
}
}

Web.config中添加如下配置节点:

    <add key="DBSessionFatory" value="DAL.DBSessionFactory" />
<add key="DBSessionFatoryDLL" value="E:\WorkSpace\Study\Webs\MVC\OAsln\Web\bin\DAL.dll" />

6、修改BaseBLL类的调用方式,添加如下代码:

        /// <summary>
/// 2.0 数据仓储接口(相当于数据层工厂,可以创建所有的数据子类对象)
/// </summary>
private IDAL.IDBSession iDbSession; #region 数据仓储 属性 + IDBSession DBSession
/// <summary>
/// 数据仓储 属性
/// </summary>
public IDAL.IDBSession DBSession
{
get
{
if (iDbSession == null)
{
//1.读取配置文件
string strFactoryDLL = Common.ConfigurationHelper.AppSetting("DBSessionFatoryDLL");
string strFactoryType = Common.ConfigurationHelper.AppSetting("DBSessionFatory");
//2.1通过反射创建 DBSessionFactory 工厂对象
Assembly dalDLL = Assembly.LoadFrom(strFactoryDLL);
Type typeDBSessionFatory = dalDLL.GetType(strFactoryType);
IDAL.IDBSessionFactory sessionFactory = Activator.CreateInstance(typeDBSessionFatory) as IDAL.IDBSessionFactory;
//2.2根据配置文件内容 使用 DI层里的Spring.Net 创建 DBSessionFactory 工厂对象 //3.通过 工厂 创建 DBSession对象
iDbSession = sessionFactory.GetDBSession();
}
return iDbSession;
}
}
#endregion

在这里,使用到了工厂来创建对象,后面引入了Spring.net之后,会回过头来优化现有的代码。项目中使用到了许多接口,目的是为了解耦,每一个项目的职责尽量让其单一,业务层只让其调用数据层接口,也是为了依赖于抽象,而不是具体。每一个框架其实都是各种设计模式的一个集合,设计模式是为了解决一类问题,而框架就是为了解决一系列问题了。到现在为止,整个项目的雏形已经出来了,但是后续,我们一步一步来优化,好的框架不是一下子就能设计得完美的,而是能够不断的拥抱修改,可持续扩展,不断改进出来的。

最新文章

  1. python之最强王者(4)——字符串
  2. Jquery表单提交后获取返回Json值
  3. shell学习之路:流程控制(for)
  4. 【bzoj1060】[ZJOI2007]时态同步
  5. 【leetcode】Multiply Strings
  6. POJ 2352
  7. Zabbix_server.conf 的性能调优
  8. 用Perl编写Apache模块续二 - SVN动态鉴权实现SVNAuth 禅道版
  9. web工程导入MyEclipse 就变成Java工程 ———— 解决方案
  10. 解决git Push时请求username和password,而不是ssh-key验证
  11. HttpServlet was not found on the Java
  12. jmake 编译当前目录c/c++单文件 指定文件 可加选项
  13. CruiseControl.Net全面实现持续集成
  14. smarty模板基础2
  15. 669. Trim a Binary Search Tree
  16. 利用伪元素:after清除浮动
  17. 执行PHP脚本时遇到 mysql_connect(): Headers and client library minor version mismatch的解决方法
  18. 基于hashchange导航管理
  19. LeetCode(92):反转链表 II
  20. Qt调用JS

热门文章

  1. QVariant类及QVariant与自定义数据类型转换的方法
  2. 10、QT分析之WebKit
  3. L1&amp;L2 Regularization
  4. Python——eventlet.wsgi
  5. This kernel requires an x86-64 CPU, but only detected an i686 CPU.
  6. shell 下执行mysql 命令
  7. PostgreSQL安装入门教程
  8. linux下nginx安裝
  9. 关于ARM NEON学习的一些资料
  10. 【玩转Golang】 自定义json序列化对象时,非法字符错误原因