在进行数据库添加、修改、删除时,为了保证事务的一致性,即操作要么全部成功,要么全部失败。例如银行A、B两个账户的转账业务。一方失败都会导致事务的不完整性,从而事务回滚。而工作单元模式可以跟踪事务,在操作完成时对事务进行统一提交。

具体实践

首先,讲解下设计思想:领域层通过相应的库实现泛型仓储接口来持久化聚合类,之后在抽象库中将对泛型仓储接口提供基础实现,并将对应的实体转化为SQl语句。这样领域层就不需要操作Sql语句即可完成CRUD操作,同时使用工作单元对事务进行统一提交。

1)定义仓储接口,包含基本的CRUD操作及其重载不同的查询

public interface IRepository<T>
{
/// <summary>
/// 插入对象
/// </summary>
/// <param name="entity"></param>
int Insert(T entity); /// <summary>
/// 更新对象
/// </summary>
/// <param name="entity"></param>
/// <param name="predicate"></param>
int Update(T entity, Expression<Func<T, bool>> express); /// <summary>
/// 删除对象
/// </summary>
/// <param name="predicate"></param>
int Delete(Expression<Func<T, bool>> express = null); /// <summary>
/// 查询对象集合
/// </summary>
/// <param name="predicate"></param>
/// <returns></returns>
List<T> QueryAll(Expression<Func<T, bool>> express = null); /// <summary>
/// 查询对象集合
/// </summary>
/// <param name="index"></param>
/// <param name="pagesize"></param>
/// <param name="order"></param>
/// <param name="asc"></param>
/// <param name="express"></param>
/// <returns></returns>
List<T> QueryAll(int index,int pagesize,List<PropertySortCondition> orderFields, Expression<Func<T, bool>> express = null); /// <summary>
/// 查询对象集合
/// </summary>
/// <param name="type"></param>
/// <param name="predicate"></param>
/// <returns></returns>
List<object> QueryAll(Type type, Expression<Func<T, bool>> express = null); /// <summary>
/// 查询对象
/// </summary>
/// <param name="predicate"></param>
/// <returns></returns>
T Query(Expression<Func<T, bool>> express); /// <summary>
/// 查询数量
/// </summary>
/// <param name="predicate"></param>
/// <returns></returns>
object QueryCount(Expression<Func<T, bool>> express = null);
}

其次,对仓储接口提供基本实现,这里由于使用了Lambda表达式,所以就需要进行表达式树的解析(这里我希望园友能自己去研究)。

public abstract class BaseRepository<T> : IRepository<T>
where T:class,new()
{
private IUnitOfWork unitOfWork; private IUnitOfWorkContext context; public BaseRepository(IUnitOfWork unitOfWork, IUnitOfWorkContext context)
{
this.unitOfWork = unitOfWork;
this.context = context;
} Lazy<ConditionBuilder> builder = new Lazy<ConditionBuilder>(); public string tableName {
get
{
TableNameAttribute attr= (TableNameAttribute)typeof(T).GetCustomAttribute(typeof(TableNameAttribute));
return attr.Name;
}
} /// <summary>
/// 插入对象
/// </summary>
/// <param name="entity"></param>
public virtual int Insert(T entity)
{
Func<PropertyInfo[], string, IDictionary<string, object>, int> excute = (propertys, condition, parameters) =>
{
List<string> names = new List<string>();
foreach (PropertyInfo property in propertys)
{
if (property.GetCustomAttribute(typeof(IncrementAttribute)) == null)
{
string attrName = property.Name;
object value = property.GetValue(entity);
names.Add(string.Format("@{0}", attrName));
parameters.Add(attrName, value);
}
}
string sql = "Insert into {0} values({1})";
string combineSql = string.Format(sql, tableName, string.Join(",", names), builder.Value.Condition);
return unitOfWork.Command(combineSql, parameters);
};
return CreateExcute<int>(null, excute);
} /// <summary>
/// 修改对象
/// </summary>
/// <param name="entity"></param>
/// <param name="express"></param>
public virtual int Update(T entity, Expression<Func<T, bool>> express)
{ Func<PropertyInfo[], string, IDictionary<string, object>, int> excute = (propertys, condition, parameters) =>
{
List<string> names = new List<string>();
foreach (PropertyInfo property in propertys)
{
if (property.GetCustomAttribute(typeof(IncrementAttribute)) == null)
{
string attrName = property.Name;
object value = property.GetValue(entity);
names.Add(string.Format("{0}=@{1}", attrName, attrName));
parameters.Add(attrName, value);
}
}
string sql = "update {0} set {1} where {2}";
string combineSql = string.Format(sql, tableName, string.Join(",", names), builder.Value.Condition);
return unitOfWork.Command(combineSql, parameters);
};
return CreateExcute<int>(express, excute);
}
/// <summary>
/// 删除对象
/// </summary>
/// <param name="express"></param>
public virtual int Delete(Expression<Func<T, bool>> express = null)
{
Func<PropertyInfo[], string, IDictionary<string, object>, int> excute = (propertys, condition, parameters) =>
{
string sql = "delete from {0} {1}";
string combineSql = string.Format(sql, tableName, condition);
return unitOfWork.Command(combineSql, parameters);
};
return CreateExcute<int>(express, excute);
} /// <summary>
/// 查询对象集合
/// </summary>
/// <param name="express"></param>
/// <returns></returns>
public virtual List<T> QueryAll(Expression<Func<T, bool>> express = null)
{
Func<PropertyInfo[], string, IDictionary<string, object>, List<T>> excute = (propertys, condition, parameters) =>
{
string sql = "select {0} from {1} {2}";
string combineSql = string.Format(sql, string.Join(",", propertys.Select(x => x.Name)), tableName, condition);
return context.ReadValues<T>(combineSql, parameters);
};
return CreateExcute<List<T>>(express, excute);
} /// <summary>
/// 查询对象集合(分页)
/// </summary>
/// <param name="index"></param>
/// <param name="pagesize"></param>
/// <param name="order"></param>
/// <param name="asc"></param>
/// <param name="express"></param>
/// <returns></returns>
public virtual List<T> QueryAll(int index,int pagesize,List<PropertySortCondition> orderFields,Expression<Func<T, bool>> express = null)
{
Func<PropertyInfo[], string, IDictionary<string, object>, List<T>> excute = (propertys, condition, parameters) =>
{
if (orderFields == null) { throw new Exception("排序字段不能为空"); }
string sql = "select * from (select {0} , ROW_NUMBER() over(order by {1}) as rownum from {2} {3}) as t where t.rownum >= {4} and t.rownum < {5}";
string combineSql = string.Format(sql, string.Join(",", propertys.Select(x => x.Name)),string.Join(",", orderFields), tableName, condition, (index - ) * pagesize + , index * pagesize);
return context.ReadValues<T>(combineSql, parameters);
};
return CreateExcute<List<T>>(express, excute);
} /// <summary>
/// 查询对象集合
/// </summary>
/// <param name="type"></param>
/// <param name="express"></param>
/// <returns></returns>
public virtual List<object> QueryAll(Type type, Expression<Func<T, bool>> express = null)
{
Func<PropertyInfo[], string, IDictionary<string, object>, List<object>> excute = (propertys, condition, parameters) =>
{
string sql = "select {0} from {1} {2}";
string combineSql = string.Format(sql, string.Join(",", propertys.Select(x => x.Name)), tableName, condition);
return context.ReadValues(combineSql, type, parameters);
};
return CreateExcute<List<object>>(express, excute);
} /// <summary>
/// 查询对象
/// </summary>
/// <param name="express"></param>
/// <returns></returns>
public virtual T Query(Expression<Func<T, bool>> express)
{
Func<PropertyInfo[], string, IDictionary<string, object>, T> excute = (propertys, condition, parameters) =>
{
string sql = "select {0} from {1} {2}";
string combineSql = string.Format(sql, string.Join(",", propertys.Select(x => x.Name)), tableName, condition);
return context.ExecuteReader<T>(combineSql, parameters);
};
return CreateExcute<T>(express, excute);
} /// <summary>
/// 查询数量
/// </summary>
/// <param name="express"></param>
/// <returns></returns>
public virtual object QueryCount(Expression<Func<T, bool>> express = null)
{
Func<PropertyInfo[], string, IDictionary<string, object>, object> excute = (propertys, condition, parameters) =>
{
string sql = "select * from {0} {1}";
string combineSql = string.Format(sql, string.Join(",", propertys.Select(x => x.Name)), tableName, condition);
return context.ExecuteScalar(combineSql, parameters);
}; return CreateExcute<object>(express, excute);
} private TValue CreateExcute<TValue>(Expression<Func<T, bool>> express, Func<PropertyInfo[], string, IDictionary<string, object>, TValue> excute)
{
Dictionary<string, object> parameters = new Dictionary<string, object>();
PropertyInfo[] propertys = typeof(T).GetProperties();
string condition = "";
if (express != null)
{
builder.Value.Build(express, tableName);
condition = string.Format("where {0} ", builder.Value.Condition);
for (int i = ; i < builder.Value.Arguments.Length; i++)
{
parameters.Add(string.Format("Param{0}", i), builder.Value.Arguments[i]);
}
}
return excute(propertys, condition, parameters);
}
}

接下来,定义工作单元,所有的添加、删除、修改操作都会被储存到工作单元中。

public interface IUnitOfWork
{
/// <summary>
/// 命令
/// </summary>
/// <param name="commandText"></param>
/// <param name="parameters"></param>
/// <returns></returns>
int Command(string commandText, IDictionary<string, object> parameters); /// <summary>
/// 事务的提交状态
/// </summary>
bool IsCommited { get; set; } /// <summary>
/// 提交事务
/// </summary>
/// <returns></returns>
void Commit(); /// <summary>
/// 回滚事务
/// </summary>
void RollBack();
}

接下来是对工作单元的实现,其内部维护了一个命令集合,存储Sql语句。

public class UnitOfWork:IUnitOfWork
{
/// <summary>
/// 注入对象
/// </summary>
private IUnitOfWorkContext context; /// <summary>
/// 维护一个Sql语句的命令列表
/// </summary>
private List<CommandObject> commands; public UnitOfWork(IUnitOfWorkContext context)
{
commands = new List<CommandObject>();
this.context = context;
} /// <summary>
/// 增、删、改命令
/// </summary>
/// <param name="commandText"></param>
/// <param name="parameters"></param>
/// <returns></returns>
public int Command(string commandText, IDictionary<string, object> parameters)
{
IsCommited = false;
commands.Add(new CommandObject(commandText, parameters));
return ;
} /// <summary>
/// 提交状态
/// </summary>
public bool IsCommited{ get; set; } /// <summary>
/// 提交方法
/// </summary>
/// <returns></returns>
public void Commit()
{
if (IsCommited) { return ; }
using (TransactionScope scope = new TransactionScope())
{
foreach (var command in commands)
{
context.ExecuteNonQuery(command.command, command.parameters);
}
scope.Complete();
IsCommited = true;
}
} /// <summary>
/// 事务回滚
/// </summary>
public void RollBack()
{
IsCommited = false;
}
}

最后定义工作单元对事务提交处理的上下文及其实现,同仓储模式中的代码。

public interface IUnitOfWorkContext
{ /// <summary>
/// 注册新对象到上下文
/// </summary>
/// <param name="commandText"></param>
/// <param name="parameters"></param>
int ExecuteNonQuery(string commandText, IDictionary<string, object> parameters = null); /// <summary>
/// 查询对象集合
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="commandText"></param>
/// <param name="parameters"></param>
/// <param name="load">自定义处理</param>
/// <returns></returns>
List<T> ReadValues<T>(string commandText, IDictionary<string, object> parameters = null, Func<IDataReader, T> load = null) where T : class, new(); /// <summary>
/// 查询对象集合
/// </summary>
/// <param name="commandText"></param>
/// <param name="type"></param>
/// <param name="parameters"></param>
/// <param name="setItem"></param>
/// <returns></returns>
List<object> ReadValues(string commandText, Type type, IDictionary<string, object> parameters = null, Action<dynamic> setItem = null); /// <summary>
/// 查询对象
/// </summary>
/// <typeparam name="TEntity"></typeparam>
/// <param name="commandText"></param>
/// <param name="parameters"></param>
/// <param name="excute"></param>
/// <returns></returns>
T ExecuteReader<T>(string commandText, IDictionary<string, object> parameters = null, Func<IDataReader, T> load = null) where T : class,new(); /// <summary>
/// 查询数量
/// </summary>
/// <param name="commandText"></param>
/// <param name="parameters"></param>
/// <returns></returns>
object ExecuteScalar(string commandText, IDictionary<string, object> parameters = null);
}

最后实现。

public abstract class UnitOfWorkContext : IUnitOfWorkContext,IDisposable
{
/// <summary>
/// 数据库连接字符串标识
/// </summary>
public abstract string Key { get; } private SqlConnection connection; private SqlConnection Connection
{
get
{
if (connection == null)
{
ConnectionStringSettings settings = ConfigurationManager.ConnectionStrings[Key];
connection = new SqlConnection(settings.ConnectionString);
}
return connection;
}
} /// <summary>
/// 注册新对象到事务
/// </summary>
/// <typeparam name="TEntity"></typeparam>
/// <param name="entity"></param>
public int ExecuteNonQuery(string commandText, IDictionary<string, object> parameters = null)
{
Func<SqlCommand, int> excute = (commend) =>
{
return commend.ExecuteNonQuery();
};
return CreateDbCommondAndExcute<int>(commandText, parameters, excute);
} /// <summary>
/// 查询对象集合
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="commandText"></param>
/// <param name="parameters"></param>
/// <param name="load">自定义处理</param>
/// <returns>泛型实体集合</returns> public List<T> ReadValues<T>(string commandText, IDictionary<string, object> parameters = null, Func<IDataReader, T> load = null) where T : class,new()
{
Func<SqlCommand, List<T>> excute = (dbCommand) =>
{
List<T> result = new List<T>();
using (IDataReader reader = dbCommand.ExecuteReader())
{
while (reader.Read())
{
if (load == null)
{
load = (s) => { return s.GetReaderData<T>(); };
}
var item = load(reader);
result.Add(item);
}
return result;
}
};
return CreateDbCommondAndExcute(commandText, parameters, excute);
} /// <summary>
/// 查询对象集合
/// </summary>
/// <param name="commandText"></param>
/// <param name="parameters"></param>
/// <param name="setItem"></param>
/// <returns></returns>
public List<object> ReadValues(string commandText, Type type, IDictionary<string, object> parameters = null, Action<dynamic> setItem = null)
{
Func<SqlCommand, List<object>> excute = (dbCommand) =>
{
var result = new List<object>(); using (IDataReader dataReader = dbCommand.ExecuteReader())
{
while (dataReader.Read())
{
var item = dataReader.GetReaderData(type);
if (setItem != null)
{
setItem(item);
}
result.Add(item);
}
}
return result;
};
return CreateDbCommondAndExcute<List<object>>(commandText, parameters,
excute);
} /// <summary>
/// 查询对象
/// </summary>
/// <typeparam name="TEntity"></typeparam>
/// <param name="commandText"></param>
/// <param name="parameters"></param>
/// <param name="excute"></param>
/// <returns></returns>
public T ExecuteReader<T>(string commandText, IDictionary<string, object> parameters = null, Func<IDataReader, T> load = null) where T : class,new()
{
Func<SqlCommand, T> excute = (dbCommand) =>
{
var result = default(T);
using (IDataReader reader = dbCommand.ExecuteReader())
{
while (reader.Read())
{
if (load == null)
{
load = (s) => { return s.GetReaderData<T>(); };
}
result = load(reader);
}
return result;
}
};
return CreateDbCommondAndExcute<T>(commandText, parameters, excute);
} /// <summary>
/// 查询数量
/// </summary>
/// <param name="commandText"></param>
/// <param name="parameters"></param>
/// <returns></returns>
public object ExecuteScalar(string commandText, IDictionary<string, object> parameters = null)
{
Func<SqlCommand, object> excute = (dbCommand) =>
{
return dbCommand.ExecuteScalar();
};
return CreateDbCommondAndExcute(commandText, parameters, excute);
} /// <summary>
/// 创建命令并执行
/// </summary>
/// <typeparam name="TValue"></typeparam>
/// <param name="commandText"></param>
/// <param name="parameters"></param>
/// <param name="excute"></param>
/// <returns></returns>
private TValue CreateDbCommondAndExcute<TValue>(string commandText,
IDictionary<string, object> parameters, Func<SqlCommand, TValue> excute)
{
if (Connection.State == ConnectionState.Closed) { Connection.Open(); };
using (SqlCommand command = new SqlCommand())
{
command.CommandType = CommandType.Text;
command.CommandText = commandText;;
command.Connection = Connection;
command.SetParameters(parameters);
return excute(command);
}
} /// <summary>
/// 关闭连接
/// </summary>
public void Dispose()
{
if (connection != null)
{
Connection.Dispose();//非托管资源
}
}
}

在调用方法时需要注意,一个事务涉及多个聚合时,需要保证传递同一工作单元,并在方法的最后调用Commit() 方法。

public class Services : IService
{
private IMemberRespository member; private IUnitOfWork unitOfWork; public Services(IMemberRespository member, IUnitOfWork unitOfWork)
{
this.member = member;
this.unitOfWork = unitOfWork;
} /// <summary>
/// 测试用例
/// </summary>
public void Demo()
{ member.Test(); unitOfWork.Commit();
}
}

工作单元 — Unit Of Work的更多相关文章

  1. 工作单元(Unit of Work)

    维护受业务事务影响的对象列表,并协调变化的写入和并发问题的解决. 从DB中存取Data时,必须记录增删改动作,以将对DB有影响的数据写会到DB中去. 如果在每次修改对象模型时就对DB进行相应的修改,会 ...

  2. ABP理论学习之工作单元(Unit of Work)

    返回总目录 本篇目录 公共连接和事务管理方法 ABP中的连接和事务管理 仓储类 应用服务 工作单元 工作单元详解 关闭工作单元 非事务的工作单元 工作单元方法调用其它 工作单元作用域 自动保存 IRe ...

  3. 基于DDD的.NET开发框架 - ABP工作单元(Unit of Work)

    返回ABP系列 ABP是“ASP.NET Boilerplate Project (ASP.NET样板项目)”的简称. ASP.NET Boilerplate是一个用最佳实践和流行技术开发现代WEB应 ...

  4. 【.Net设计模式系列】工作单元(Unit Of Work)模式 ( 二 )

    回顾 在上一篇博客[.Net设计模式系列]仓储(Repository)模式 ( 一 ) 中,通过各位兄台的评论中,可以看出在设计上还有很多的问题,在这里特别感谢 @横竖都溢 @ 浮云飞梦 2位兄台对博 ...

  5. ABP(现代ASP.NET样板开发框架)系列之12、ABP领域层——工作单元(Unit Of work)

    点这里进入ABP系列文章总目录 基于DDD的现代ASP.NET开发框架--ABP系列之12.ABP领域层——工作单元(Unit Of work) ABP是“ASP.NET Boilerplate Pr ...

  6. ABP领域层——工作单元(Unit Of work)

    ABP领域层——工作单元(Unit Of work) 点这里进入ABP系列文章总目录 基于DDD的现代ASP.NET开发框架--ABP系列之12.ABP领域层——工作单元(Unit Of work) ...

  7. 解析ABP框架中的事务处理和工作单元,ABP事务处理

    通用连接和事务管理方法连接和事务管理是使用数据库的应用程序最重要的概念之一.当你开启一个数据库连接,什么时候开始事务,如何释放连接...诸如此类的. 正如大家都知道的,.Net使用连接池(connec ...

  8. ABP领域层知识回顾之---工作单元

    1. 前言   在上一篇博文中(http://www.cnblogs.com/xiyin/p/6842958.html) 我们讲到了ABP领域层的仓储.这边博文我们来讲 工作单元.个人觉得比较重要.文 ...

  9. [.NET领域驱动设计实战系列]专题四:前期准备之工作单元模式(Unit Of Work)

    一.前言 在前一专题中介绍了规约模式的实现,然后在仓储实现中,经常会涉及工作单元模式的实现.然而,在我的网上书店案例中也将引入工作单元模式,所以本专题将详细介绍下该模式,为后面案例的实现做一个铺垫. ...

随机推荐

  1. java实现磁盘先来先服务算法

    package demo; import java.awt.List; import java.util.ArrayList; import java.util.Arrays; public clas ...

  2. Java同步数据结构之SynchronousQueue

    前言 严格来说SynchronousQueue并不是像它的名字那样是一种Queue,它更像是一个数据接力的交汇点,还记得在介绍Exchanger的时候提到过Exchanger可以看作是Synchron ...

  3. Swift 字面量

    所谓字面量,就是指像特定的数字,字符串或者是布尔值这样,能够直接了当地指出自己的类型并为变量进行赋值的值.比如在下面: let aNumber = //整型字面量 let aString = &quo ...

  4. WebSphere 安装部署,发布web应用

    转: WebSphere 安装部署,发布web应用 2017年11月20日 23:51:08 greensure 阅读数 20099   版权声明:本文为博主原创文章,遵循CC 4.0 by-sa版权 ...

  5. 打印Linq生成的SQL语句

    var t1 = source.OrderBy<T>(orderby).Skip<T>(_skip).Take<T>(_take); var t2 = t1.ToO ...

  6. Consul 随记

    consul 包含多个组件,但是作为一个整体对外提供服务发现和服务配置工具: 提供的关键特性有: 服务发现:发现的是服务对应的IP地址和PORT端口号 健康检查:检查服务节点状态 Key/Value存 ...

  7. forge k8s/kubernetes ci/cd 最佳实践

    forge的官网是: http://forge.sh forge工具是一个部署工具, 将你的应用自动更新到kubernets集群中, 只需要配置简洁的配置文件和模板即可. 其它的就交给forge, 它 ...

  8. Flutter dio伪造请求头获取数据

    在很多时候,后端为了安全都会有一些请求头的限制,只有请求头对了,才能正确返回数据.这虽然限制了一些人恶意请求数据,但是对于我们聪明的程序员来说,就是形同虚设.下面就以极客时间为例,讲一下通过伪造请求头 ...

  9. Java工程师学习指南第3部分:Spring与SpringMVC源码解析

    本文整理了微信公众号[Java技术江湖]发表和转载过的Spring全家桶优质文章,想看到更多Java技术文章,就赶紧关注吧. 前后端分离,我怎么就选择了 Spring Boot + Vue 技术栈? ...

  10. Go语言中defer语句使用小结

    defer是Go语言中的延迟执行语句,用来添加函数结束时执行的代码,常用于释放某些已分配的资源.关闭数据库连接.断开socket连接.解锁一个加锁的资源.Go语言机制担保一定会执行defer语句中的代 ...