EF架构~终于自己架构了一个相对完整的EF方案

回到目录

EF4.1学了有段时间了,没有静下来好好研究它的架构,今天有空正好把它的架构及数据操作这段拿出来,希望给大家带来帮助,对我自己也是一种总结:P

image 

从图中可以看到,我们用的是MVC3进行程序开发的,哈哈,也是刚开始用3.0,项目整体架构还是传统三层,其它公用层我就不说了,服务层和UI层也不说了,单说EF还在的实体层和数据层,我用EF生成器把它生成后,又整理了一个,因为我不想让EF的低层方法暴露给业务层.

image

 

我来一个一个的说我的方案:

OAContext.cs:这是生成器生成的,这不作修

RepositoryBase.cs:这是数据库基类,里面对它的子类公开了一个属性和一个方法,

属性就是OAContext的实例,而方法就是SaveChanges的一个封装,子类可以根据自己的逻辑去复写它.

IRepository.cs:这是提供更新操作的统一接口,需要有更新需要的子类去实现,EF的统一更新我还没有想出来,哈哈

IEntityRepository.cs:这是统一的数据操作接口,除了更新之外的所有操作,统一操作接口是一个泛型接口

EntityRepository.cs:对统一操作接口的实现

好了,下面我把源代码公开,大家可以看一下,有好的建设请和我联系!

RepositoryBase.cs

 1     /// <summary>
 2 
 3     /// 数据操作基类
 4 
 5     /// </summary>
 6 
 7     public abstract class RepositoryBase
 8 
 9     {
10 
11         #region 单件模式创建一个类对象
12 
13         /// <summary>
14 
15         /// 数据源对象
16 
17         /// </summary>
18 
19         private static OAContext dbContext = null;
20 
21         protected static OAContext CreateInstance()
22 
23         {
24 
25             if (dbContext == null)
26 
27                 dbContext = new OAContext();
28 
29             return dbContext;
30 
31         }
32 
33         #endregion
34 
35  
36 
37         public OAContext _db = CreateInstance();
38 
39        
40 
41         /// <summary>
42 
43         /// 存储变化 service层可能也会使用本方法,所以声明为public
44 
45         /// </summary>
46 
47         public virtual void SaveChanges()
48 
49         {
50 
51             this._db.Configuration.ValidateOnSaveEnabled = false;
52 
53             this._db.SaveChanges();
54 
55         }
56 
57     }

 

IRepository.CS

 1     /// <summary>
 2 
 3     /// 数据操作统一接口(更新)
 4 
 5     /// </summary>
 6 
 7     public interface IRepository<TEntity> where TEntity : class //这里使用泛型接口
 8 
 9     {
10 
11         /// <summary>
12 
13         /// 根据数据库实体—》更新记录
14 
15         /// </summary>
16 
17         /// <param name="entity"></param>
18 
19         void Update(TEntity entity);
20 
21  
22 
23         /// <summary>
24 
25         /// 根据响应的属性名称进行更新
26 
27         /// </summary>
28 
29         /// <param name="entity">要更新的实体</param>
30 
31         /// <param name="enums">要响应的列枚举</param>
32 
33         void Update(TEntity entity, Enum enums);
34 
35  
36 
37         /// <summary>
38 
39         /// 根据数据库实体—》[批量]更新记录
40 
41         /// </summary>
42 
43         /// <param name="entity"></param>
44 
45         void Update(IList<TEntity> list);
46 
47  
48 
49         /// <summary>
50 
51         /// 根据响应的属性名称进行批量更新
52 
53         /// </summary>
54 
55         /// <param name="list">要更新的实体IList<IDataEntity></param>
56 
57         /// <param name="enums">要响应的列枚举</param>
58 
59         void Update(IList<TEntity> list, Enum enums);
60 
61     }

 

IEntityRepository.cs

 1     /// <summary>
 2 
 3     /// 数据操作统一接口(插入,查詢,刪除)
 4 
 5     /// </summary>
 6 
 7     public interface IEntityRepository<TEntity> where TEntity : class //这里使用泛型接口
 8 
 9     {
10 
11  
12 
13         /// <summary>
14 
15         /// 根据数据库实体—》插入记录
16 
17         /// </summary>
18 
19         void Insert(TEntity entity);
20 
21  
22 
23         /// <summary>
24 
25         /// 根据数据库实体—》[批量]插入记录
26 
27         /// </summary>
28 
29         void Insert(IList<TEntity> list);
30 
31  
32 
33         /// <summary>
34 
35         /// 删除单条记录
36 
37         /// </summary>
38 
39         /// <param name="oArr"></param>
40 
41         void Delete(TEntity entity);
42 
43  
44 
45         /// <summary>
46 
47         /// 删除列表
48 
49         /// </summary>
50 
51         /// <param name="list"></param>
52 
53         void Delete(IList<TEntity> list);
54 
55  
56 
57         /// <summary>
58 
59         /// 得到实体列表
60 
61         /// </summary>
62 
63         /// <returns></returns>
64 
65         DbSet<TEntity> GetList();
66 
67     }

 

EntityRepository.cs

  1      /// <summary>
  2 
  3     /// 数据操作实现类(统一实现类)
  4 
  5     /// </summary>
  6 
  7     /// <typeparam name="TEntity"></typeparam>
  8 
  9     public class EntityRepository<TEntity> : RepositoryBase, IEntityRepository<TEntity>
 10 
 11         where TEntity : class
 12 
 13     {
 14 
 15         #region IEntityRepository<TEntity> Members
 16 
 17  
 18 
 19         public void Insert(TEntity entity)
 20 
 21         {
 22 
 23             this._db.Set<TEntity>().Add(entity);
 24 
 25             this._db.Entry(entity).State = System.Data.EntityState.Added;
 26 
 27             this.SaveChanges();
 28 
 29         }
 30 
 31  
 32 
 33         public void Insert(IList<TEntity> list)
 34 
 35         {
 36 
 37             list.ToList().ForEach(entity =>
 38 
 39             {
 40 
 41                 this._db.Set<TEntity>().Add(entity);
 42 
 43                 this._db.Entry(entity).State = System.Data.EntityState.Added;
 44 
 45             });
 46 
 47             this.SaveChanges();
 48 
 49         }
 50 
 51  
 52 
 53         public void Delete(TEntity entity)
 54 
 55         {
 56 
 57             this._db.Set<TEntity>().Remove(entity);
 58 
 59             this._db.Entry(entity).State = System.Data.EntityState.Deleted;
 60 
 61             this.SaveChanges();
 62 
 63         }
 64 
 65  
 66 
 67         public void Delete(IList<TEntity> list)
 68 
 69         {
 70 
 71             list.ToList().ForEach(entity =>
 72 
 73             {
 74 
 75                 this._db.Set<TEntity>().Remove(entity);
 76 
 77                 this._db.Entry(entity).State = System.Data.EntityState.Deleted;
 78 
 79             });
 80 
 81             this.SaveChanges();
 82 
 83         }
 84 
 85  
 86 
 87         public System.Data.Entity.DbSet<TEntity> GetList()
 88 
 89         {
 90 
 91             return this.DbSet;
 92 
 93         }
 94 
 95  
 96 
 97         /// <summary>
 98 
 99         /// 泛型数据表属性
100 
101         /// </summary>
102 
103         protected DbSet<TEntity> DbSet
104 
105         {
106 
107             get { return this._db.Set<TEntity>(); }
108 
109         }
110 
111         #endregion
112 
113  
114 
115         /// <summary>
116 
117         /// 操作提交
118 
119         /// </summary>
120 
121         public override void SaveChanges()
122 
123         {
124 
125             base.SaveChanges();
126 
127         }
128 
129     }

 回到目录

posted @ 2011-07-22 16:19  张占岭  阅读(19082)  评论(15编辑  收藏  举报