1.依赖IRepository接口而不是直接使用EntityFramework

使用IRepository不只是架构上解耦的需要,更重要的意义在于Service的单元测试,Repository模式本身就是采用集合操作的方式简化数据访问,IRepository更容易Mock。先上图:

鉴于目前接触到的项目中,即使业务逻辑相对复杂的项目也只是应用逻辑复杂而非领域逻辑复杂,在实际使用中聚合根和单独Repository接口只是引入了更多的代码和类型定义,因此一般情况下使用泛型版本的Repository<T>接口即可。nopcommerce等开源项目中也是如此。Java中的伪泛型无法实现泛型版本的Repository<T>,简单的说你无法在Repository<T>的方法中获取T的类型。

 namespace Example.Application
{
public interface IRepository<T> where T : class
{
T FindBy(object id); IQueryable<T> Query { get; } void Add(T entity); void Remove(T entity); void Update(T entity); int Commit();
}
}

2.封装DbContext的依赖项

(1)定义一个通用的EfDbContext,将DbContext对IDbConnectionFactory、ConnectionString、实体类配置等的依赖封装到DbSettings中,既可以在使用使方便依赖注入也方便进行单元测试。


 namespace Example.Infrastructure.Repository
{
public class EfDbContext : DbContext, IDbContext
{
private DbSettings _dbSettings; public EfDbContext(IConfiguration configuration, ILogger logger, DbSettings dbSettings) : base(dbSettings.NameOrConnectionString)
{
this._dbSettings = dbSettings;
if (this._dbSettings.DbConnectionFactory != null)
{
#pragma warning disable
Database.DefaultConnectionFactory = this._dbSettings.DbConnectionFactory;
}
if (configuration.Get<bool>("database.log:", false))
{
this.Database.Log = sql => logger.Information(sql);
}
this.Database.Log = l => System.Diagnostics.Debug.WriteLine(l);
} protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder); modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
if (_dbSettings.EntityMaps != null)
{
foreach (var item in _dbSettings.EntityMaps)
{
modelBuilder.Configurations.Add((dynamic)item);
}
}
if (_dbSettings.ComplexMaps != null)
{
foreach (var item in _dbSettings.ComplexMaps)
{
modelBuilder.Configurations.Add((dynamic)item);
}
}
} public void SetInitializer<T>() where T : DbContext
{
if (this._dbSettings.Debug)
{
if (this._dbSettings.UnitTest)
{
Database.SetInitializer(new DropCreateDatabaseAlways<T>());
}
{
Database.SetInitializer(new DropCreateDatabaseIfModelChanges<T>());
}
}
else
{
Database.SetInitializer<T>(null);
}
} public new IDbSet<T> Set<T>() where T : class
{
return base.Set<T>();
} public int Commit()
{
return base.SaveChanges();
}
}
}

(2)在DbSettings中按需定义依赖,这里将实体类的配置也通过DbSettings注入。


 namespace Example.Infrastructure.Repository
{
public class DbSettings
{
public DbSettings()
{
this.RowVersionNname = "Version";
} public string NameOrConnectionString { get; set; } public string RowVersionNname { get; set; }
public bool Debug { get; set; } public bool UnitTest { get; set; } public IDbConnectionFactory DbConnectionFactory { get; set; } public List<object> EntityMaps { get; set; } = new List<object>(); public List<object> ComplexMaps { get; set; } = new List<object>();
}
}

3.定义SqlServerDbContext和VersionDbContext,解决使用开放式并发连接时,MySql等数据库无法自动生成RowVersion的问题。

(1)适用于SqlServer、SqlServeCe的SqlServerDbContext


 namespace Example.Infrastructure.Repository
{
public class SqlServerDbContext : EfDbContext
{
private DbSettings _dbSettings; public SqlServerDbContext(IConfiguration configuration, ILogger logger, DbSettings dbSettings)
: base(configuration, logger, dbSettings)
{
this._dbSettings = dbSettings;
} protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Properties().Where(o => o.Name == this._dbSettings.RowVersionNname).Configure(o => o.IsRowVersion());
base.SetInitializer<SqlServerDbContext>();
}
}
}

(2)适用于Myql、Sqlite等数据库的VersionDbContext。使用手动更新Version,通过GUID保证版本号唯一。


 namespace Example.Infrastructure.Repository
{
public class VersionDbContext : EfDbContext
{
private DbSettings _dbSettings; public VersionDbContext(IConfiguration configuration, ILogger logger, DbSettings dbSettings)
: base(configuration,logger,dbSettings)
{
this._dbSettings = dbSettings;
} protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Properties().Where(o => o.Name == this._dbSettings.RowVersionNname)
.Configure(o => o.IsConcurrencyToken().HasDatabaseGeneratedOption(DatabaseGeneratedOption.None));
base.SetInitializer<VersionDbContext>();
} public override int SaveChanges()
{
this.ChangeTracker.DetectChanges();
var objectContext = ((IObjectContextAdapter)this).ObjectContext;
foreach (ObjectStateEntry entry in objectContext.ObjectStateManager.GetObjectStateEntries(EntityState.Modified | EntityState.Added))
{
var v = entry.Entity;
if (v != null)
{
var property = v.GetType().GetProperty(this._dbSettings.RowVersionNname);
if (property != null)
{
var value = Encoding.UTF8.GetBytes(Guid.NewGuid().ToString());
property.SetValue(v, value);
}
}
}
return base.SaveChanges();
}
}
}

4.使用XUnit、Rhino.Mocks和SqlServerCe进行单元测试

这是参考nopcommerce中的做法,nopcommerce使用的NUnit需要安装NUnit扩展,XUnit只需要通过Nuget引入程序包,看看GitHub上的aspnet源码,微软也在使用XUnit。


 namespace Example.Infrastructure.Test.Repository
{
public class CustomerPersistenceTest
{
private IRepository<T> GetRepository<T>() where T : class
{
string testDbName = "Data Source=" + (System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location)) + @"\\test.sdf;Persist Security Info=False";
var configuration = MockRepository.GenerateMock<IConfiguration>();
var logger = MockRepository.GenerateMock<ILogger>();
var repository = new EfRepository<T>(new SqlServerDbContext(configuration,logger,new DbSettings
{
DbConnectionFactory = new SqlCeConnectionFactory("System.Data.SqlServerCe.4.0"),
NameOrConnectionString = testDbName,
Debug = true,
UnitTest = true,
EntityMaps = new List<object> { new EntityTypeConfiguration<Customer>() }
}));
return repository;
} [Fact]
public void SaveLoadCustomerTest()
{
var repository = this.GetRepository<Customer>();
repository.Add(new Customer { UserName = "test" });
repository.Commit();
var customer = repository.Query.FirstOrDefault(o => o.UserName == "test");
Assert.NotNull(customer);
}
}
}

5.确保在ASP.NET中使用依赖注入时,配置DbContext的生命周期为Request范围


 namespace Example.Web
{
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
ObjectFactory.Init();
ObjectFactory.AddSingleton<IConfiguration, AppConfigAdapter>();
ObjectFactory.AddSingleton<ILogger, Log4netAdapter>();
ObjectFactory.AddSingleton<DbSettings, DbSettings>(new DbSettings { NameOrConnectionString = "SqlCeConnection", Debug = true });
ObjectFactory.AddScoped<IDbContext, SqlServerDbContext>();
ObjectFactory.AddTransient(typeof(IRepository<>), typeof(EfRepository<>));
ObjectFactory.Build();
ObjectFactory.GetInstance<ILogger>().Information(String.Format("Start at {0}",DateTime.Now));
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
} protected void Application_EndRequest()
{
ObjectFactory.Dispose();
}
}
}

依赖注入这里采用的是StructureMap。HttpContextLifecycle提供了Request范围内的生命周期管理但未定义在StructureMap程序包中,需要引入StructureMap.Web程序包。使用HttpContextLifecycle时需要在Application_EndRequest调用HttpContextLifecycle.DisposeAndClearAll()方法。

EntityFramework系列:Repository模式与单元测试的更多相关文章

  1. 基于 EntityFramework 生成 Repository 模式代码

    借助 WeihanLi.EntityFramework 实现简单的 Repository Intro 很多时候一些简单的业务都是简单的增删改查,动态生成一些代码完成基本的增删改查,而这些增删改查代码大 ...

  2. 使用ASP.NET Web Api构建基于REST风格的服务实战系列教程【二】——使用Repository模式构建数据库访问层

    系列导航地址http://www.cnblogs.com/fzrain/p/3490137.html 前言 在数据访问层应用Repository模式来隔离对领域对象的细节操作是很有意义的.它位于映射层 ...

  3. PHP 设计模式系列 —— 资源库模式(Repository)

    1.模式定义 Repository 是一个独立的层,介于领域层与数据映射层(数据访问层)之间.它的存在让领域层感觉不到数据访问层的存在,它提供一个类似集合的接口提供给领域层进行领域对象的访问.Repo ...

  4. 关于Repository模式

    定义(来自Martin Fowler的<企业应用架构模式>): Mediates between the domain and data mapping layers using a co ...

  5. LCLFramework框架之Repository模式

    Respository模式在示例中的实际目的小结一下 Repository模式是架构模式,在设计架构时,才有参考价值: Repository模式主要是封装数据查询和存储逻辑: Repository模式 ...

  6. [转]关于Repository模式

    原文链接:关于Repository模式 定义(来自Martin Fowler的<企业应用架构模式>): Mediates between the domain and data mappi ...

  7. Asp.Net Core + Dapper + Repository 模式 + TDD 学习笔记

    0x00 前言 之前一直使用的是 EF ,做了一个简单的小项目后发现 EF 的表现并不是很好,就比如联表查询,因为现在的 EF Core 也没有啥好用的分析工具,所以也不知道该怎么写 Linq 生成出 ...

  8. Entity Framework Repository模式

    Repository模式之前 如果我们用最原始的EF进行设计对每个实体类的“C(增加).R(读取).U(修改).D(删除)”这四个操作. 第一个:先来看看查询,对于实体类简单的查询操作,每次都是这样的 ...

  9. Repository模式介绍汇总

    1.Linq To Sql中Repository模式应用场景 http://www.cnblogs.com/zhijianliutang/archive/2012/02/24/2367305.html ...

随机推荐

  1. 用js获取当前页面的url的相关信息方法

    当前页面对应的URL的一些属性: ( http://bbs.xxx.net/forum.php?mod=viewthread&tid=2709692&page=1&extra= ...

  2. 正确使用 Volatile 变量——Brian Goetz

    本文转自:http://www.ibm.com/developerworks/cn/java/j-jtp06197.html 由Java并发大师Brian Goetz所撰写的. Java 语言中的 v ...

  3. Solr 参考资料

    solr 的入门好资料 https://cwiki.apache.org/confluence/display/solr/Apache+Solr+Reference+Guide https://two ...

  4. emacs24下使用jedi对python编程进行补全

    在开始前先装好pip和virtualenv(见pip的安装一文),另需安装好make 1.emacs下安装: epc deferred.el auto-complete 使用M-x package-i ...

  5. [转][原]openstack-kilo--issue(六)kilo版openstack的dashboard在session超时后重新登录报错解决办法

    http://blog.csdn.net/wylfengyujiancheng/article/details/50523373?locationNum=1&fps=1 1.现象描述: kil ...

  6. R语言绘图高质量输出

    R语言通过支持Cairo矢量图形处理的类库,可以创建高质量的矢量图形(PDF,PostScript,SVG) 和 位图(PNG,JPEG, TIFF),同时支持在后台程序中高质量渲染.在ggplot2 ...

  7. nginx实现动态分离,解决css和js等图片加载问题

    改帖专门为使用nginx,通过nginx把请求转发到web服务器再返回客户端的时候,解决css和js和图片加载不出来的问题. 如果没安装nginx,请访问一下地址进行安装 http://www.cnb ...

  8. IPC之PIPE

    管道是一种只允许用在有亲属关系的进程间通信的方式,由函数pipe创建一个管道,read,write进行读写操作. #include <unistd.h> ]); 参数pipefd[2]数组 ...

  9. 使用EntityFramework6完成增删查改和事务

    使用EntityFramework6完成增删查改和事务 上一节我们已经学习了如何使用EF连接数据库,并简单演示了一下如何使用EF6对数据库进行操作,这一节我来详细讲解一下. 使用EF对数据库进行操作, ...

  10. Java语言中的基本词汇

    1.标识符包.类.方法.参数和变量的名称.大小写字母.数字._和$符号的组合,不以数字开始,不能使关键字,不能包括分隔符和换行.(严格区分大小写,最大长度255个字符) 2.字面量  某种类型的值(具 ...