.NETdotNETASP.NET Core见识录

命令模式-工作单元

2019-11-05  本文已影响0人  Carson_jz

摘要:工作单元是命令模式得一种具体应用,就以webapi操作数据库来说工作单元模式,一次请求,需要操作多个或多次操作数据库,如果每操作一次都直接提交给数据库,那有可能在某些瞬态错误导致没法完成所有操作而产生数据不一致,不考虑分布式事务,最好得做法就是把所有操作打包,最后一次性把操作提交给数据库,这样可以最大程度保证一致性。设计模式Github源码

拿数据库操作来描述一个工作单元

Repository(仓储类):这是用来写操作命令的库

UnitOfWork(工作单元类):里边有一个委托List 用来存储仓储类的命令,凡是你在程序里调用的Repository的方法都会存储在此,这个类还有一个执行方法Save,就是最终一起执行所有仓储命令

工作单元除了有以上有点,还有其他方面的优点,例如:

你在 UnitOfWork Save()方法里可以完成在你提交redis之前,提交redis之后执行相关逻辑,这也是aop的一种接入方式,分离了操作命令与数据持久化。

代码实现

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace UnitOfWork
{
    public class Model
    {
        public static Model CreateNew(string myKey)
        {
            return new Model()
            {
                Key = myKey
            };
        }
        public string Key { get; set; }
    }
    public class Repository
    {
        public string Add(Model model)
        {
            Console.WriteLine(model.Key);
            return "add command";
        }

        public string Update(Model model)
        {
            Console.WriteLine(model.Key);
            return "update command";
        }

        public string Delete(Model model)
        {
            Console.WriteLine(model.Key);
            return "delete command";
        }
    }

    public class UnitOfWork
    {
        public List<Func<string, string>> Commands { get; set; } = new List<Func<string, string>>();

        public void AddCommand(Func<string, string> repositoryFunc)
        {
            Commands.Add(repositoryFunc);
        }

        public void Save()
        {
            var publicKey = "PublicKey-";
            //执行委托集合命令
            var commandList = Commands.Select(c => c(publicKey)).ToList();

            //打包命令然后提交
            //code...
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            UnitOfWork uow = new UnitOfWork();

            Repository repository = new Repository();

            uow.AddCommand((publicKey) =>
            {
                var model = Model.CreateNew(publicKey + "AddKey");
                return repository.Add(model);
            });
            uow.AddCommand((publicKey) =>
            {
                var model = Model.CreateNew(publicKey + "UpdateKey");
                return repository.Update(model);
            });
            uow.AddCommand((publicKey) =>
            {
                var model = Model.CreateNew(publicKey + "DeleteKey");
                return repository.Delete(model);
            });

            Console.WriteLine("Hello World!");

            //到这里才把所有语句执行
            uow.Save();

            Console.ReadKey();
        }
    }
}

上一篇 下一篇

猜你喜欢

热点阅读