zoukankan      html  css  js  c++  java
  • 项目架构开发:数据访问层之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>(TEntity entity) where TEntity : class
    28         {
    29             this.addEntities.Add(entity, () =>
    30             {
    31                 Conn.Insert<TEntity>(entity);
    32             });
    33         }
    34 
    35         public void Update<TEntity>(TEntity entity) where TEntity : class
    36         {
    37             this.updateEntities.Add(entity, () =>
    38             {
    39                 Conn.Update(entity);
    40             });
    41         }
    42 
    43         public void Delete<TEntity>(TEntity entity) where TEntity : class
    44         {
    45             this.deleteEntities.Add(entity, () =>
    46             {
    47                 Conn.Delete(entity);
    48             });
    49         }
    50 
    51         public TEntity Get<TEntity>(int id) where TEntity : class
    52         {
    53             return Conn.Get<TEntity>(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>(TEntity entity)
    10             where TEntity : class
    11         {
    12             base.Add<TEntity>(entity);
    13         }
    14 
    15         public void RegisterDirty<TEntity>(TEntity entity)
    16             where TEntity : class
    17         {
    18             base.Update<TEntity>(entity);
    19         }
    20 
    21         public void RegisterDeleted<TEntity>(TEntity entity)
    22             where TEntity : class
    23         {
    24             base.Delete<TEntity>(entity);
    25         }
    26 
    27         public void RegisterClean<TEntity>(TEntity entity)
    28             where TEntity : class
    29         { }
    30 
    31         //public TEntity Get<TEntity>(int id) 
    32         //    where TEntity : class
    33         //{
    34         //    return base.Get<TEntity>(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 }
  • 相关阅读:
    Linux useradd 命令介绍
    lsscsi
    安装MegaCli,查看linux服务器raid信息
    ipmitool命令详解
    python 收发邮件
    angularjs 高级玩法 创建递归的模板
    我的Android进阶之旅------&gt;Android Activity的singleTask载入模式和onActivityResult方法之间的冲突
    Git实战(三)环境搭建
    使用Samba实现Linux与Windows文件共享实践
    设计模式个人备忘(享元模式,strategy, templete strategy)
  • 原文地址:https://www.cnblogs.com/lanxiaoke/p/8296684.html
Copyright © 2011-2022 走看看