EntityFramework用法探索(四)Repository和UnitOfWork

简介:

以上一篇CodeFirst生成代码为基础,继续探索使用方式。

引入Repository模式,定义最简单的IRepository接口,仅包含增删改查接口,

复制代码
1   public interface IRepository<T>
2     where T : class
3   {
4     IQueryable<T> Query();
5     void Insert(T entity);
6     void Update(T entity);
7     void Delete(T entity);
8   }
复制代码

引入UnitOfWork模式,因为EntityFramework会负责失败回滚,所以此处只定义提交方法。

1   public interface IUnitOfWork
2   {
3     void Commit();
4   }

实现IRepository接口,

复制代码
 1   public class Repository<T> : IRepository<T> where T : class
 2   {
 3     private readonly IObjectSetFactory _objectSetFactory;
 4     private readonly IObjectSet<T> _objectSet;
 5 
 6     public Repository(IObjectSetFactory objectSetFactory)
 7     {
 8       _objectSetFactory = objectSetFactory;
 9       _objectSet = objectSetFactory.CreateObjectSet<T>();
10     }
11 
12     #region IRepository<T> Members
13 
14     public IQueryable<T> Query()
15     {
16       return _objectSet;
17     }
18 
19     public void Insert(T entity)
20     {
21       _objectSet.AddObject(entity);
22     }
23 
24     public void Update(T entity)
25     {
26       _objectSet.Attach(entity);
27       _objectSetFactory.ChangeObjectState(entity, EntityState.Modified);
28     }
29 
30     public void Delete(T entity)
31     {
32       _objectSet.DeleteObject(entity);
33     }
34 
35     #endregion
36   }
复制代码

实现IUnitOfWork接口,

复制代码
 1   public class UnitOfWork : IUnitOfWork, IDisposable
 2   {
 3     private readonly IObjectContext _objectContext;
 4 
 5     public UnitOfWork(IObjectContext objectContext)
 6     {
 7       _objectContext = objectContext;
 8     }
 9 
10     #region IUnitOfWork Members
11 
12     public void Commit()
13     {
14       _objectContext.SaveChanges();
15     }
16 
17     #endregion
18 
19     #region IDisposable Members
20 
21     public void Dispose()
22     {
23       if (_objectContext != null)
24       {
25         _objectContext.Dispose();
26       }
27 
28       GC.SuppressFinalize(this);
29     }
30 
31     #endregion
32   }
复制代码

CustomerRepository类的实现需要做一些配置,

复制代码
 1     public CustomerRepository()
 2     {
 3       Mapper.CreateMap<DomainModels.Customer, Customer>();
 4       Mapper.CreateMap<Customer, DomainModels.Customer>();
 5 
 6       DbContext context = new RETAILContext();
 7       DbContextAdapter contextAdaptor = new DbContextAdapter(context);
 8 
 9       IObjectSetFactory objectSetFactory = contextAdaptor;
10       _repository = new Repository<Customer>(objectSetFactory);
11 
12       IObjectContext objectContext = contextAdaptor;
13       _uow = new UnitOfWork(objectContext);
14     }
复制代码

则具体增删改查的逻辑实现,

复制代码
 1     public void InsertCustomer(DomainModels.Customer customer)
 2     {
 3       Customer entity = Mapper.Map<DomainModels.Customer, Customer>(customer);
 4 
 5       _repository.Insert(entity);
 6       _uow.Commit();
 7 
 8       customer.Id = entity.Id;
 9     }
10 
11     public void UpdateCustomer(DomainModels.Customer customer)
12     {
13       Customer entity = _repository.Query().Single(c => c.Id == customer.Id);
14 
15       entity.Name = customer.Name;
16       entity.Address = customer.Address;
17       entity.Phone = customer.Phone;
18 
19       _repository.Update(entity);
20 
21       _uow.Commit();
22     }
复制代码

在同样的示例下仍然可以工作,

复制代码
 1       ICustomerRepository customerRepository = new CustomerRepository();
 2 
 3       // =============== 增 ===============
 4       Console.ForegroundColor = ConsoleColor.DarkRed;
 5 
 6       DomainModels.Customer customer1 = new DomainModels.Customer()
 7       {
 8         Name = "Dennis Gao",
 9         Address = "Beijing",
10         Phone = "18888888888",
11       };
12       customerRepository.InsertCustomer(customer1);
13       Console.WriteLine(customer1);
复制代码

同时,UnitOfWork可以保证相关的业务操作在同一个Transaction中,

完整代码和索引

EntityFramework用法探索系列

完整代码下载

 







本文转自匠心十年博客园博客,原文链接:http://www.cnblogs.com/gaochundong/archive/2013/06/06/entityframework_usage_repository_unitofwork.html,如需转载请自行联系原作者


目录
相关文章
|
数据库
EF-CodeFirst数据库迁移时可能出现的几种错误- The EntityFramework package is not installed on project 'MovieEF'
EF-CodeFirst数据库迁移时可能出现的几种错误- The EntityFramework package is not installed on project 'MovieEF'
EF-CodeFirst数据库迁移时可能出现的几种错误- The EntityFramework package is not installed on project 'MovieEF'
|
SQL 存储 Java
利用 Repository 中的方法解决实际问题
基于Repository的一些基础函数来解决业务中的一些问题
|
数据库 容器
Entity Framework Core(3)-配置DbContext
设计时 DbContext 配置 EF Core 设计时工具如迁移需要能够发现和创建的工作实例DbContext以收集有关应用程序的实体类型以及它们如何映射到数据库架构的详细信息的类型。 此过程可以为自动,只要该工具可以轻松地创建DbContext,会将其配置同样到它如何将配置在运行时的方式。
910 0
|
数据库 .NET 开发框架
【译】EntityFramework6与EntityFrameworkCore的区别
EntityFramework6 EF6 是一个久经考验的数据库访问技术,发展多年,拥有许多特性,并且成熟稳定。2008年EF作为 .Net 3.5 Sp1 和Visual Studio 2008 SP1 的一部分首次发布。
1377 0