从Entity Framework的实现方式来看DDD中的repository仓储模式运用

一:最普通的数据库操作


static void Main(string[] args)
{
using (SchoolDBEntities db = new SchoolDBEntities())
{
db.Students.Add(new Student() { StudentName = "nihao" });

db.SaveChanges();
}
}


domain 和 db 是怎么操作。。。

DbSet<Student> 集合 【用于存放集合】 从名称中可以看出,是一个叫做Student Set的一个集合。。

可以看出,是一个叫做实体的仓库。。。


SaveChanges() 模式提交,会从两个仓储中获取添加的domain entity,然后整体性的提交数据库。。。

执行操作之前,会开启一个transaction。。。两条insert之后, commit transction。。

UI

BLL

DAL


DBSet => Repository

DbContext => unitofwork


//
// 摘要:
// Interface implemented by objects that can provide an System.Data.Entity.Infrastructure.IObjectContextAdapter.ObjectContext
// instance. The System.Data.Entity.DbContext class implements this interface to
// provide access to the underlying ObjectContext.
public interface IObjectContextAdapter
{
//
// 摘要:
// Gets the object context.
ObjectContext ObjectContext { get; }
}

我们在ef4.0,4.1的时候,用的都是objectContext。。。 code first出现之后,都用DBContext进行了封装。。。

 

posted @ 2017-03-07 22:38  dragon.net  阅读(848)  评论(0编辑  收藏  举报