jasontaylordev / NorthwindTraders

Northwind Traders is a sample application built using ASP.NET Core and Entity Framework Core.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Move the validators into the command class?

MrJonBirch opened this issue · comments

So I have noticed that the handlers have been moved into the command file and I was just wondering what everyones thoughts are for moving the validator in there too? I'm sort of split I like that it consolidates everything but also think it could cause the command class to start to get bloated if we keep adding more things into it.

The command would start to look like this...

public class UpdateCommand : IRequest
{
    public string SomeEntityId { get; set; }

    public class UpdateCommandHandler : IRequestHandler<UpdateCommand>
    {
        private IDbContext _context;

        public JoinGroupCommandHandler(IDbContext context)
        {
            _context = context;
        }

        public async Task<Unit> Handle(UpdateCommand request, CancellationToken cancellationToken)
        {
            // handler logic
      

            return new Unit();
        }
    }

    public class UpdateCommandValidator : AbstractValidator<UpdateCommand>
    {
        public UpdateCommandValidator()
        {
            RuleFor(e => e.SomeEntityId....)
        }
    }

}

Thoughts?

So I have noticed that the handlers have been moved into the command file and I was just wondering what everyones thoughts are for moving the validator in there too?

I's this move on purpose? For example here https://github.com/jasontaylordev/NorthwindTraders/tree/master/Src/Application/Products/Commands/CreateProduct we still have CreateProductCommandHandler.cs file seperated from CreateProductCommand.cs file

Also Handlers are separated from Queries while integrated with some Commands. So Commands are not consistent with this. Should we put all command handlers in command files or just some? If some, what are the rules to keep them in command file, and what to keep the in command handler file?
image

@neman

Yes, this has been done on purpose, its to increase discoverability. See this point in the presentation for more details. https://youtu.be/5OtUm1BLmG0?t=1054

As far as this project I believe they are trying to show case all the possibilities, not just one and let the implementer decide what works best for them. It is perfectly valid for the handlers to be embedded in a query or a command. With this example project being the one exception I think the most important part is just to remain consistent through out your project and really it all comes down to what you feel is best for your team.

Thank you for your interest in this project. This repository has been archived and is no longer actively maintained or supported. We appreciate your understanding. Feel free to explore the codebase and adapt it to your own needs if it serves as a useful reference. If you have any further questions or concerns, please refer to the README for more information.