1

I am currently building an app, and I would like to use micro services. I use Mediatr for implementing a CQRS pattern and EventStore for event sourcing.

I have a problem with checking that an entity exists before creating an event of aggregate and appending it to the EventStore.

For example: I have LanguageAggregateRoot

public class LanguageAggregateRoot 
{
    public Guid Id {get;set}
    public string Code { get; private set; }
    public string Name { get; private set; }
    public bool Enable { get; private set; }
    public string Icon { get; private set; }
}

Field Code is unique and user can change code for language.

When I use Code field for stream id of eventstore, if the user sends a CreateLanguageCommand and ChangeCodeCommand, I need to check that the new code exists.

So I use Id field for stream id. But I don't understand how I can validate whether code field is unique?

As far as I've found out should not use query handling in command handling.

If i use client to check existed then send command to server. I think it doesn't look good. Because something/someone can request only command with out my client.

How can I do that?

Thanks for your support.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Minh Mít
  • 127
  • 4
  • 11
  • 1
    Do it in your rest api layer -- the controllers. They are the real client of your business logic, not the end-user's browser -- that's a client of the API. That layer should have access to both the query and command side and can validate user input before creating the command. – Software Engineer Jul 25 '21 at 07:02
  • So, Api layer -- RestApi or OData Controller, GrpcService -- GrpcBase or GraphQL layer is a client of business logic. That right? Thanks you. – Minh Mít Jul 25 '21 at 08:15

1 Answers1

2

It should be fine to validate your request in your command itself. you can use the below link for more details. CQRS - is it allowed to call the read side from the write side?

Nitheesh Govind
  • 170
  • 1
  • 7
  • So, Api layer -- RestApi or OData Controller, GrpcService -- GrpcBase or GraphQL layer is a client of business logic. That right? Thanks you. – Minh Mít Jul 25 '21 at 08:15
  • @MinhMít I would avoid placing the logic for handling commands to the API layer. You already mentioned three options, would you be happy to copy-paste your logic to another API if you decide to change or add a transport? That's what a command service does, and the API layer is just a mean to call it. – Alexey Zimarev Jan 29 '22 at 20:25