1.项目背景 

这里简单介绍一下项目需求背景,之前公司的项目基于EF++Repository+UnitOfWork的框架设计的,其中涉及到的技术有RabbitMq消息队列,Autofac依赖注入等常用的.net插件。由于公司的发展,业务不断更新,变得复杂起来,对于数据的实时性、存储容量要求也提高了一个新的高度。数据库上下文DbContext设计的是单例模式,基本上告别了多线程高并发的数据读写能力,看了园子里很多大神的博客,均为找到适合自己当前需求的DbContext的管理方式。总结目前主要的管理方式:

1)DbContext单例模式(长连接)。即公司之前的设计。很明显,这种设计方式无法支持多线程同步数据操作。报各种错误,最常见的,比如:集合已修改,无法进行枚举操作。---弃用

2)Using模式(短连接)。这种模式适合一些对于外键,导航属性不经常使用的场合,由于导航属性是放在上下文缓存中的,一旦上下文释放掉,导航属性就为null。当然,也尝试了其他大神的做法,比如,在上下文释放之前转换为        ToList或者使用饥饿加载的方式(ps:这种方式很不灵活,你总不可能遇到一个类类型就去利用反射加载找到它具有的导航属性吧或者直接InCluding),这些方法依旧没有办法解决目前的困境。也尝试这直接赋值给一个定义的同类      型的变量,但是对于这种带导航的导航的复杂类的深拷贝,没有找到合适的路子,有知道的可以告诉我,非常感谢!

以上两种方式及网上寻找的其他方式都没有解决我的问题。这里先上一下之前的Repository:

 1 using System.Data.Entity;
 2 using System.Data.Entity.Validation;
 3 
 4 namespace MM.Data.Library.Entityframework
 5 {
 6     public class EntityFrameworkRepositoryContext : RepositoryContext, IEntityFrameworkRepositoryContext
 7     {
 8         protected DbContext container;
 9 
10         public EntityFrameworkRepositoryContext(DbContext container)
11         {
12             this.container = container;
13         }
14 
15         public override void RegisterNew<TAggregateRoot>(TAggregateRoot obj)
16         {            
17             this.container.Set<TAggregateRoot>().Add(obj);
18             this.IsCommit = false;
19         }
20 
21         public override void RegisterModified<TAggregateRoot>(TAggregateRoot obj)
22         {
23             if (this.container.Entry<TAggregateRoot>(obj).State == EntityState.Detached)
24             {
25                 this.container.Set<TAggregateRoot>().Attach(obj);
26             }
27             this.container.Entry<TAggregateRoot>(obj).State = EntityState.Modified;
28             this.IsCommit = false;
29         }
30 
31         public override void RegisterDeleted<TAggregateRoot>(TAggregateRoot obj)
32         {
33             this.container.Set<TAggregateRoot>().Remove(obj);
34             this.IsCommit = false;
35         }
36 
37         public override void Rollback()
38         {
39             this.IsCommit = false;
40         }
41 
42         protected override void DoCommit()
43         {
44             if (!IsCommit)
45             {
46                 //var count = container.SaveChanges();
47                 //IsCommit = true;
48                 try
49                 {
50                     var count = container.SaveChanges();
51                     IsCommit = true;
52                 }
53                 catch (DbEntityValidationException dbEx)
54                 {
55                     foreach (var validationErrors in dbEx.EntityValidationErrors)
56                     {
57                         foreach (var validationError in validationErrors.ValidationErrors)
58                         { 
59                         }
60                     }
61                     IsCommit = false;
62                 }
63             }
64         }
65 
66         public System.Data.Entity.DbContext DbContext
67         {
68             get { return container; }
69         }
70 
71         public override void Dispose()
72         {
73             if (container != null)
74                 container.Dispose();
75         }
76     }
77 }
View Code

相关文章: