Unit of Work

IAggregateRoot接口本身属于标记(market interface)接口模式。这个接口充当类和方法的元数据,那些与该类实例交互的代码在执行这些实例的工作之前检查是否存在该接口。

    public interface IAggregateRoot{ };

IUnitOfWorkRepository是所有打算用在Unit of Work模式中的Repository需要实现的又一个接口。

    public interface IUnitOfWorkRepository
    {
        void PersistCreationOf(IAggregateRoot entity);
        void PersistUpdateOf(IAggregateRoot entity);
        void PersistDeletionOf(IAggregateRoot entity);
    }

IUnitOfWork接口在注册修改、增加、删除时需要IUnitOfWorkRepository,这样在提交的时候,IUnitOfWork可以将真正的持久化方法的工作委托给适当的具体实现。

    public interface IUnitOfWork
    {
        void RegisterAmended(IAggregateRoot entity, IUnitOfWorkRepository unitOfWorkRepository);
        void RegisterNew(IAggregateRoot entity, IUnitOfWorkRepository unitOfWorkRepository);
        void RegisterRemoved(IAggregateRoot entity, IUnitOfWorkRepository unitOfWorkRepository);
        void Commit();
    }

 

    public class UnitOfWork : IUnitOfWork
    {
        Dictionary<IAggregateRoot, IUnitOfWorkRepository> addedEntites;
        Dictionary<IAggregateRoot, IUnitOfWorkRepository> changedEntites;
        Dictionary<IAggregateRoot, IUnitOfWorkRepository> deletedEntites;
        public UnitOfWork()
        {
            addedEntites = new Dictionary<IAggregateRoot, IUnitOfWorkRepository>();
            changedEntites = new Dictionary<IAggregateRoot, IUnitOfWorkRepository>();
            deletedEntites = new Dictionary<IAggregateRoot, IUnitOfWorkRepository>();
        }

        public void RegisterAmended(IAggregateRoot entity, IUnitOfWorkRepository unitOfWorkRepository)
        {
            if (!changedEntites.ContainsKey(entity))
            {
                changedEntites.Add(entity, unitOfWorkRepository);
            }
        }

        public void RegisterNew(IAggregateRoot entity, IUnitOfWorkRepository unitOfWorkRepository)
        {
            if (!addedEntites.ContainsKey(entity))
            {
                addedEntites.Add(entity, unitOfWorkRepository);
            }
        }

        public void RegisterRemoved(IAggregateRoot entity, IUnitOfWorkRepository unitOfWorkRepository)
        {
            if (!deletedEntites.ContainsKey(entity))
            {
                deletedEntites.Add(entity, unitOfWorkRepository);
            }
        }

        public void Commit()
        {
            using (TransactionScope scope = new TransactionScope())
            {
                foreach (IAggregateRoot entity in this.addedEntites.Keys)
                {
                    this.addedEntites[entity].PersistCreationOf(entity);
                }
                foreach (IAggregateRoot entity in this.changedEntites.Keys)
                {
                    this.addedEntites[entity].PersistUpdateOf(entity);
                }
                foreach (IAggregateRoot entity in this.deletedEntites.Keys)
                {
                    this.addedEntites[entity].PersistDeletionOf(entity);
                }
                scope.Complete();
            }
        }
    }
UnitOfWork.cs

 用于银行转账的一个实体

    public class Account : IAggregateRoot
    {
        public decimal balance { get; set; }
    }

创建一个简化版的Repository(为了account持久化)

    public interface IAccountRepository
    {
        void Save(Account account);
        void Add(Account account);
        void Remove(Account account);
    }

创建一个服务类来协调两个账户之间的转账工作。

    public class AccountService
    {
        IAccountRepository _accountRepository;
        IUnitOfWork _unitOfWork;

        public AccountService(IAccountRepository accountRepository, IUnitOfWork unitOfWork)
        {
            _unitOfWork = unitOfWork;
            _accountRepository = accountRepository;
        }

        public void Transfer(Account from, Account to, decimal amount)
        {
            if (from.balance >= amount)
            {
                from.balance -= amount;
                to.balance += amount;
                _accountRepository.Save(from);
                _accountRepository.Save(to);
                _unitOfWork.Commit();
            }
        }
    }

 

    public class AccountRepository : IAccountRepository, IUnitOfWorkRepository
    {
        IUnitOfWork _unitOfWork;
        public AccountRepository(IUnitOfWork unitOfWork)
        {
            _unitOfWork = unitOfWork;
        }

        public void Save(Account account)
        {
            _unitOfWork.RegisterAmended(account, this);
        }

        public void Remove(Account account)
        {
            _unitOfWork.RegisterAmended(account, this);
        }

        public void RersistUpdateOf(IAggregateRoot entity)
        {
            //ADO.NET code to update the entity
        }

        public void RersistCreationOf(IAggregateRoot entity)
        {
            //ADO.NET code to add the entity
        }

        public void RersistDeltionOf(IAggregateRoot entity)
        {
            //ADO.NET code to delete the entity
        }
    }

 

posted @ 2020-06-11 10:39  水墨晨诗  阅读(256)  评论(0编辑  收藏  举报