CQRS and Microservices in Product Development

How to design a product so as not to bury money in the ground


At what stage in the creation of a product or system to connect the architectural design of the system so that then it would not be excruciatingly painful for the money spent? How to decide whether to combine CQRS and microservices.


This article is for business representatives requesting the development of an IT solution. We will show you how to launch a product and avoid unjustified costs associated with architecture. And also see how using CQRS will help with the implementation of functionality in different application clients, and whether microservices are the very panacea.


Briefly about CQRS


CQRS (Command-Query Responsibility Segregation) - a template used in the development of systems, which states that any method of the system can be either a request (not changing the state of the system) or a command (changing the state of the system). As practice shows, this is one of the most commonly used patterns in software development. It can be used at different levels and in different situations. For example, the classical division of systems into OLTP / OLAP, when data is often written to the OLTP system and read from the OLAP system, is nothing more than the use of the CQRS template in the database architecture.



β€œβ€ ( 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