工作单元的目标是维护变化的对象列表。使用IUnitOfWorkRepository负责对象的持久化,使用IUnitOfWork收集变化的对象,并将变化的对象放到各自的增删改列表中,
最后Commit,Commit时需要循环遍历这些列表,并由Repository来持久化。
要实现一个银行卡简单转账的功能,Demo框架如下设计:
代码实现如下:
EntityBase,领域类的基类。
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Jack.Gao.UnitOfWork.Infrastructure { public class EntityBase { } }
IUnitOfWork,复杂维护变化的对象列表,并最后Commit,依次遍历变化的列表,并持久化,这就是Commit的事情。
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Jack.Gao.UnitOfWork.Infrastructure { public interface IUnitOfWork { void RegisterAdded(EntityBase entityBase, IUnitOfWorkRepository unitOfWorkRepository); void RegisterChangeded(EntityBase entityBase, IUnitOfWorkRepository unitOfWorkRepository); void RegisterRemoved(EntityBase entityBase, IUnitOfWorkRepository unitOfWorkRepository); void Commit(); } }
IUnitOfWorkRepository,负责持久化对象。
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Jack.Gao.UnitOfWork.Infrastructure { public interface IUnitOfW