项目架构开发:数据访问层之UnitOfWork (补充)

应lisansi同学回复(项目架构开发:数据访问层之UnitOfWork)要求,补上Dapper的DbContext实现

 1 using Dapper.Contrib.Extensions;
 2 using HG.Infrastructure.Common;
 3 using System;
 4 using System.Collections.Generic;
 5 using System.Data;
 6 using System.Transactions;
 7 
 8 namespace HG.Data.Dapper
 9 {
10     public class DbContext
11     {
12         public IDbConnection Conn { get; set; }
13 
14         private Dictionary<object, Action> addEntities;
15         private Dictionary<object, Action> updateEntities;
16         private Dictionary<object, Action> deleteEntities;
17 
18         public DbContext()
19         {
20             Conn = DbConnectionFactory.CreateConnection(Enums.Core);
21 
22             addEntities = new Dictionary<object, Action>();
23             updateEntities = new Dictionary<object, Action>();
24             deleteEntities = new Dictionary<object, Action>();
25         }
26 
27         public void Add(TEntity entity) where TEntity : class
28         {
29             this.addEntities.Add(entity, () =>
30             {
31                 Conn.Insert(entity);
32             });
33         }
34 
35         public void Update(TEntity entity) where TEntity : class
36         {
37             this.updateEntities.Add(entity, () =>
38             {
39                 Conn.Update(entity);
40             });
41         }
42 
43         public void Delete(TEntity entity) where TEntity : class
44         {
45             this.deleteEntities.Add(entity, () =>
46             {
47                 Conn.Delete(entity);
48             });
49         }
50 
51         public TEntity Get(int id) where TEntity : class
52         {
53             return Conn.Get(id);
54         }
55 
56         public int SaveChanges()
57         {
58             int count = 0;
59             using (TransactionScope scope = new TransactionScope())
60             {
61                 foreach (var entity in deleteEntities.Keys)
62                 {
63                     this.deleteEntities[entity]();
64                 }
65 
66                 foreach (var entity in updateEntities.Keys)
67                 {
68                     this.updateEntities[entity]();
69                 }
70 
71                 foreach (var entity in addEntities.Keys)
72                 {
73                     this.addEntities[entity]();
74                 }
75 
76                 scope.Complete();
77 
78                 count = deleteEntities.Count + updateEntities.Count + addEntities.Count;
79 
80                 deleteEntities.Clear();
81                 updateEntities.Clear();
82                 addEntities.Clear();
83 
84                 if (Conn != null)
85                 {
86                     if (Conn.State == ConnectionState.Open)
87                     {
88                         Conn.Close();
89                     }
90 
91                    // Conn.Dispose();
92                 }
93             }
94 
95             return count;
96         }
97     }
98 }

 

UnitOfWork相应改成如下
 1 using HG.Data.Interface;
 2 using System;
 3 using System.Data;
 4 
 5 namespace HG.Data.Dapper
 6 {
 7     public class UnitOfWork : DbContext, IUnitOfWork, ICRUDUnitOfWork
 8     {
 9         public void RegisterNew(TEntity entity)
10             where TEntity : class
11         {
12             base.Add(entity);
13         }
14 
15         public void RegisterDirty(TEntity entity)
16             where TEntity : class
17         {
18             base.Update(entity);
19         }
20 
21         public void RegisterDeleted(TEntity entity)
22             where TEntity : class
23         {
24             base.Delete(entity);
25         }
26 
27         public void RegisterClean(TEntity entity)
28             where TEntity : class
29         { }
30 
31         //public TEntity Get(int id) 
32         //    where TEntity : class
33         //{
34         //    return base.Get(id);
35         //}
36 
37         public bool Commit()
38         {
39             return base.SaveChanges() > 0;
40         }
41 
42         public void Rollback()
43         {
44             throw new NotImplementedException();
45         }
46     }
47 }

 

你可能感兴趣的:(项目架构开发:数据访问层之UnitOfWork (补充))