产品开发中的CQRS和微服务

如何设计产品以免将钱埋在地下


在创建产品或系统以连接系统的体系结构设计的哪个阶段,这样花费的钱就不会造成极大的痛苦?如何决定是否结合CQRS和微服务。


本文适用于要求开发IT解决方案的业务代表。我们将向您展示如何发布产品并避免与架构相关的不合理成本。还可以查看使用CQRS如何在不同的应用程序客户端中实现功能,以及微服务是否是万灵药。


简要介绍CQRS


CQRS(命令查询责任隔离) -在系统开发中使用的模板,指出系统的任何方法都可以是请求(不更改系统状态)或命令(更改系统状态)。如实践所示,这是软件开发中最常用的模式之一。它可以在不同级别和不同情况下使用。例如,当数据经常写入OLTP系统并从OLAP系统读取数据时,将系统经典分为OLTP / OLAP就是在数据库体系结构中使用CQRS模板。



“” ( 2000 ) CQRS. , Interbase/FirebirdSQL . . , , Query-, CRM- Command. CQRS JS . , JS … , , .



: A Big Ball of Mud



. , MVP , . , . — , . , . , .


, . , . , . .


: “ . , ”. , , , . “ ” . . . - , .



. , , . , . .


. , , . , . (Elastic ), - . , , - . , .


, , , . , - , - , . , . , , , .. 1 . - . , .


, , , , , . , . — . , , CAP-. , , .




/ . : , . .


/ , . . , , , .



, -, , , , . , -, (.. -), , , - .


, , . (.. ), , CRM- , . , , ..


?


- , , low-fidelity . Business Canvas ( ) , . , : -, . , . , . — ? : 2 . , , .


, . - - . , . , , (, COVID-19 !). CQRS .



- . CQRS .


:



, SOLID, Dependency Inversion Dependency Injection, , . , .


3 , :


  1. (Admin office Desktop App)
  2. (Ship Desktop Mechanic app)
  3. (SyncService)

, , , , . , Offline- , , . ? , , , . , — . - .


,


C#, .


/// <summary>
///       .
///       Composition Root(web api ,     ..)
/// </summary>
    public interface IHandlersFactory
    {
        IQueryHandler<TQuery, TResult> CreateQueryHandler<TQuery, TResult>();

        IAsyncQueryHandler<TQuery, TResult> CreateAsyncQueryHandler<TQuery, TResult>();

        ICommandHandler<TCommand> CreateCommandHandler<TCommand>();

        IAsyncCommandHandler<TCommand> CreateAsyncCommandHandler<TCommand>();
    }
/// <summary>
///       
/// </summary>
 public interface ICommandHandler<TCommand>
    {
        void Execute(TCommand command);
    }
/// <summary>
///       
/// </summary>

public interface IQueryHandler<TQuery, TResult>
    {
        TResult Execute(TQuery query);
    }

/// <summary>
///     Ninject,     
/// </summary>
public class NinjectFactory : IHandlersFactory
    {
        private readonly IResolutionRoot _resolutionRoot;

        public NinjectFactory(IResolutionRoot resolutionRoot)
        {
            _resolutionRoot = resolutionRoot;
        }

        public IAsyncCommandHandler<TCommand> CreateAsyncCommandHandler<TCommand>()
        {
            return _resolutionRoot.Get<IAsyncCommandHandler<TCommand>>();
        }

        public IAsyncQueryHandler<TQuery, TResult> CreateAsyncQueryHandler<TQuery, TResult>()
        {
            return _resolutionRoot.Get<IAsyncQueryHandler<TQuery, TResult>>();
        }

        public ICommandHandler<TCommand> CreateCommandHandler<TCommand>()
        {
            return _resolutionRoot.Get<ICommandHandler<TCommand>>();
        }

        public IQueryHandler<TQuery, TResult> CreateQueryHandler<TQuery, TResult>()
        {
            return _resolutionRoot.Get<IQueryHandler<TQuery, TResult>>();
        }
    }

Ninject


public override void Load()
        {
            // queries
            Bind<IQueryHandler<GetCertificateByIdQuery, Certificate>>().To<GetCertificateByIdQueryHandler>();
            Bind<IQueryHandler<GetCertificatesQuery, List<Certificate>>>().To<GetCertificatesQueryHandler>();
            Bind<IQueryHandler<GetCertificateByShipQuery, List<Certificate>>>().To<GetCertificateByShipQueryHandler>();
…………
}

IHandlerFactory :


:


Ship ship = mHandlersFactory.CreateQueryHandler<GetShipByIdQuery, Ship>().Execute(new GetShipByIdQuery(id));

:


mHandlersFactory.CreateCommandHandler<DeleteReportCommand>()
                    .Execute(new DeleteReportCommand(report));

, :


  1. , , .
  2. , , .
  3. , . /, . .
  4. , !

. . 300 . , 3 :


  1. - ? ?
  2. , ?
  3. ?

Bounded Context . , . .


CQRS


: MVVM, SOLID, CQRS .. . , .


, . : . , .



Agile- “ , , — ”. , — . , — . , , .


, . . -, . -, , . -, . , , .


All Articles