自研ORM框架实现工作单元模式

时间:2022-12-27 15:10:36

平时我们使用事务,需要显示的Try Catch 并且开启事务 提交事务 异常回滚事务 三步骤,使用工作单元后则只需要Commit。

1.接口定义

 1     /// <summary>
 2     /// 工作单元接口类
 3     /// </summary>
 4     public interface IUnitOfWork : IDisposable
 5     {
 6         /// <summary>
 7         /// 是否提交
 8         /// </summary>
 9         bool IsCommit { get; }
10 
11         /// <summary>
12         /// 数据库上下文
13         /// </summary>
14         IDbContext Db { get; }
15 
16         /// <summary>
17         /// 提交
18         /// </summary>
19         /// <returns></returns>
20         bool Commit();
21     }

2.接口实现

 1 /// <summary>
 2     /// 工作单元实现类
 3     /// </summary>
 4     public class UnitOfWork : IUnitOfWork
 5     {
 6         /// <summary>
 7         /// 释放
 8         /// </summary>
 9         private bool disposed = false;
10 
11         /// <summary>
12         /// 是否提交
13         /// </summary>
14         public bool IsCommit { get; private set; }
15 
16         /// <summary>
17         /// 数据库上下文
18         /// </summary>
19         public IDbContext Db { get; }
20 
21         /// <summary>
22         /// 构造方法
23         /// </summary>
24         /// <param name="db">数据库上下文</param>
25         public UnitOfWork(IDbContext db)
26         {
27             this.Db = db;
28             this.Db.BeginTranAsync().Wait();
29         }
30 
31         /// <summary>
32         /// 提交
33         /// </summary>
34         /// <returns></returns>
35         public bool Commit()
36         {
37             if (!IsCommit)
38             {
39                 this.Db.CommitTranAsync().Wait();
40                 IsCommit = true;
41             }
42             return IsCommit;
43         }
44 
45         /// <summary>
46         /// 释放
47         /// </summary>
48         public void Dispose()
49         {
50             Dispose(true);
51             GC.SuppressFinalize(this);
52         }
53 
54         /// <summary>
55         /// 释放
56         /// </summary>
57         /// <param name="disposing">释放标记</param>
58         protected virtual void Dispose(bool disposing)
59         {
60             if (!this.disposed)
61             {
62                 if (disposing)
63                 {
64                     if (!IsCommit)
65                     {
66                         this.Db.RollbackTranAsync().Wait();
67                     }
68                 }
69                 disposed = true;
70             }
71         }
72 
73         /// <summary>
74         /// 析构函数
75         /// </summary>
76         ~UnitOfWork()
77         {
78             Dispose(false);
79         }
80     }

3.使用方式

1.注册服务

1 // Add DbContext Service
2 builder.Services.AddFastDbContext();
3 // Add UnitOfWork Service
4 builder.Services.AddFastUnitOfWork();

2.构造方法注入  UnitOfWork 对象使用时 无需显示using 当发生异常时会自动调用 Dispose 方法

        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="logger"></param>
        public ValuesController(ILogger<ValuesController> logger,IUnitOfWork unitOfWork)
        {
            this.logger = logger;
            this.unitOfWork = unitOfWork;
        }

        [HttpGet]
        public async Task<string> TestUnitOfWork()
        {
            var result1 = await unitOfWork.Db.Insert(new Category()
            {
                CategoryName = "类别3"
            }).ExceuteAsync();

            var result2 = await unitOfWork.Db.Insert(new Product()
            {
                ProductCode = "测试工作单元_111",
            }).ExceuteAsync();

            unitOfWork.Commit();
            return "工作单元执行完成...";
        }

 

 

翻译

搜索

复制