• 工作单元模式


    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();
    
            }
        }
    }
  • 相关阅读:
    现代软件工程 第一周博客作业
    最后一周总结
    阅读后感
    软件工程作业个人项目——csdn app分析
    第二次结对编程
    软件工程作业二
    软件工程作业一
    ASE 课程最后小结
    阅读后感
    Judy Beta 第五天
  • 原文地址:https://www.cnblogs.com/kexb/p/9095489.html
Copyright © 2020-2023  润新知