jeudi 26 septembre 2019

Is wrong to use a Mediator as something looks like a Facade?

As a Way to decouple my Presentation from my Application, I searched for many samples and ways trying to find the right approach.

After studied a little about the sample from Mathew Renze here https://github.com/matthewrenze/clean-architecture-demo and Jason Taylor here https://github.com/JasonGT/NorthwindTraders, I see myself between a few questions, but until now, without answers.

I tried to create a clean app (gathering knowledge from Clean Architecture's book) but sometimes I just get confused.

Now, my question is: Isn't my Mediator a Facade pattern? First, let me explain how did I created my classes:

In-time: I'm using the Mediatr to create the Mediator Pattern.

I have a controller wich is injected with the mediator:

public class CategoryController : Controller
{
    private readonly IMediator _mediator;

    public CategoryController(IMediator mediator) => _mediator = mediator;

    [HttpGet]
    public ActionResult List()
    {
        var request = new GetCategoriesRequest();

        var response = _mediator.Send(request).Result;

        var viewModel = new List<CategoryViewModel>();

        foreach (var category in response)
        {
            var categoryViewModel = new CategoryViewModel
            {
                Id = category.Id,
                ParentId = category.ParentId,
                Name = category.Name,
                Uri = category.Uri
            };
            viewModel.Add(categoryViewModel);
        }

        return View(viewModel);
    }
}

and the Handler has this code

public class GetCategoriesHandler : RequestHandler<GetCategoriesRequest, List<GetCategoriesResponse>>
{
    private readonly IDocDevsDbContext _context;

    public GetCategoriesHandler(IDocDevsDbContext context)
    {
        _context = context;
    }

    protected override List<GetCategoriesResponse> Handle(GetCategoriesRequest request)
    {
        var categories = _context.Categories.AsQueryable();
        if (request.Id > 0)
            categories = categories.Where(cat => request.Id == 0 || cat.Id == request.Id);
        if (!string.IsNullOrWhiteSpace(request.Name))
            categories = categories.Where(cat => !string.IsNullOrWhiteSpace(cat.Name) && cat.Name.ToLower().Equals(request.Name.ToLower()));
        if (!string.IsNullOrWhiteSpace(request.Uri))
            categories = categories.Where(cat => !string.IsNullOrWhiteSpace(cat.Uri) && cat.Uri.ToLower().Equals(request.Uri.ToLower()));

        var response = new List<GetCategoriesResponse>();
        foreach (var category in categories)
        {
            var res = new GetCategoriesResponse
            {
                Id = category.Id,
                ParentId = category.ParentId,
                Name = category.Name,
                Uri = category.Uri
            };
            response.Add(res);
        }

        return response;
    }
}

Inside my Handler class I'm executing the rules and validations. I know this is just a Query but in few Commands I can have something like: - Invoice Order (database) - Lower Stock (database) - Generate Sale Notification (service) - Increase Buyer Milestone (database) - Commit Changes

So, should I have all those rules on the mediator as private methods? Or should I have classes for every step (invoice, stock, milestone) and make my mediator work as a Facade? OR AM I TOTALLY WRONG ABOUT EVERYTHING (it could happen more often then I want :lol:).

Any tips, hints, blogs, discussion will make me a lot happier guys.

Thanks

Aucun commentaire:

Enregistrer un commentaire