工作单元模式
Posted 听哥哥的话
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了工作单元模式相关的知识,希望对你有一定的参考价值。
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Transactions; using UnitOfWork; namespace UnitOfWork { public interface IUnitOfWorkRepository { void NewItem(object item); void UpdatedItem(object item); void DeletedItem(object item); } public interface IUnitOfWork { void RegisterAdded(object entity, IUnitOfWorkRepository repository); void RegisterChanged(object entity, IUnitOfWorkRepository repository); void RegisterRemoved(object entity, IUnitOfWorkRepository repository); void Commit(); } public class UnitOfWork : IUnitOfWork { private Dictionary<object, IUnitOfWorkRepository> addedEntities; private Dictionary<object, IUnitOfWorkRepository> changedEntities; private Dictionary<object, IUnitOfWorkRepository> deletedEntities; ///<summary> /// 实例化工作单元 ///</summary> public UnitOfWork() { this.addedEntities = new Dictionary<object, IUnitOfWorkRepository>(); this.changedEntities = new Dictionary<object, IUnitOfWorkRepository>(); this.deletedEntities = new Dictionary<object, IUnitOfWorkRepository>(); } #region IUnitOfWork 方法 ///<summary> /// 注册添加操作 ///</summary> ///<param name="entity"></param> ///<param name="repository"></param> public void RegisterAdded(object entity, IUnitOfWorkRepository repository) { this.addedEntities.Add(entity, repository); } ///<summary> /// 注册更改操作 ///</summary> ///<param name="entity"></param> ///<param name="repository"></param> public void RegisterChanged(object entity, IUnitOfWorkRepository repository) { this.changedEntities.Add(entity, repository); } ///<summary> /// 注册删除操作 ///</summary> ///<param name="entity"></param> ///<param name="repository"></param> public void RegisterRemoved(object entity, IUnitOfWorkRepository repository) { this.deletedEntities.Add(entity, repository); } ///<summary> /// 执行内存列表保存的操作 ///</summary> public void Commit() { using (TransactionScope scope = new TransactionScope()) { foreach (object entity in this.deletedEntities.Keys) { this.deletedEntities[entity].DeletedItem(entity); } foreach (object entity in this.addedEntities.Keys) { this.addedEntities[entity].NewItem(entity); } foreach (object entity in this.changedEntities.Keys) { this.changedEntities[entity].UpdatedItem(entity); } scope.Complete(); } this.deletedEntities.Clear(); this.addedEntities.Clear(); this.changedEntities.Clear(); } #endregion } public class TestRepository : IUnitOfWorkRepository { private IUnitOfWork unitOfWork; private IUnitOfWork TestUnitOfWork { get { return this.unitOfWork; } } public TestRepository(IUnitOfWork unitOfWork) { this.unitOfWork = unitOfWork; } public void Add(object _object) { TestUnitOfWork.RegisterAdded(_object, this); Console.WriteLine("注册添加数据" + _object); } public void Delete(object _object) { TestUnitOfWork.RegisterRemoved(_object, this); Console.WriteLine("注册删除数据" + _object); } public void update(object _object) { TestUnitOfWork.RegisterChanged(_object, this); Console.WriteLine("注册更新数据" + _object); } public void NewItem(object _object) { Console.WriteLine("执行添加数据" + _object); } public void UpdatedItem(object _object) { Console.WriteLine("执行更新数据" + _object); } public void DeletedItem(object _object) { Console.WriteLine("执行删除数据" + _object); } } public class TestServer { private static IUnitOfWork unitOfWork; private static TestRepository _TestRepository; static TestServer() { TestServer.unitOfWork = new UnitOfWork(); _TestRepository = new TestRepository(unitOfWork); } public static void Add() { _TestRepository.Add(1); _TestRepository.Add(2); _TestRepository.Add(3); _TestRepository.Add(4); _TestRepository.Add(5); unitOfWork.Commit(); } public static void Add(object _object) { _TestRepository.Add(_object); _TestRepository.Delete(_object); _TestRepository.update(_object); unitOfWork.Commit(); } public static void Delete(object _object) { _TestRepository.Delete(_object); unitOfWork.Commit(); } public static void Update(object _object) { _TestRepository.update(_object); unitOfWork.Commit(); } } } namespace ConsoleApplication3 { class Program { static void Main(string[] args) { TestServer.Add(1); Console.WriteLine("----------------------------"); TestServer.Add(); Console.ReadKey(); } } }
以上是关于工作单元模式的主要内容,如果未能解决你的问题,请参考以下文章
.NET Core MongoDB数据仓储和工作单元模式封装
[工作积累] UE4 并行渲染的同步 - Sync between FParallelCommandListSet & FRHICommandListImmediate calls(代码片段