C
C#•3y ago
WibblyWobbly

MediatR and CQRS for basic logic

So started doing abit of MediatR CQRS pattern in abit of Clean Architecture. I've been doing the Query and Commands for Database / API stuff as the articles suggest but what about general business logic? Say I have a List of People and I want to capitalize all their Surnames right so Wibbly Wobbly becomes Wibbly WOBBLY and find people aged 30 and above. Would it be:
public async Task<IEnumerable<People>> GetPeopleOver30WithCapitalSurname()
{
IEnumerable<People> people = await _mediator.Send(new GetPeopleQuery());
return await _mediator.Send(new ToCapitalSurnameAndOver30(people));
}

public async Task<IEnumerable<People>> GetPeopleOver30WithCapitalSurname()
{
IEnumerable<People> people = await _mediator.Send(new GetPeopleQuery());
return await _mediator.Send(new ToCapitalSurnameAndOver30(people));
}

ToCapitalSurnameAndOver30 simply takes a list foreach person in people, gets the surname ToUpperCase() and if (person.Age > 30) add to another list maybe. Nothing fancy but using _mediator.Send() to get to this logic or:
public async Task<IEnumerable<People>> GetPeopleOver30WithCapitalSurname()
{
IEnumerable<People> people = await _mediator.Send(new GetPeopleQuery());
return await _myService.ToCapitalSurnameAndOver30(people)
}

public async Task<IEnumerable<People>> GetPeopleOver30WithCapitalSurname()
{
IEnumerable<People> people = await _mediator.Send(new GetPeopleQuery());
return await _myService.ToCapitalSurnameAndOver30(people)
}

As you might find with most stuff. In other words, should you use it for the majority of logic or just API/Database stuff.
3 Replies
Saber
Saber•3y ago
I'd have a query that specifically gets the people that meets my task and returns them in the appropriate manner. So if that is a specific task my system needs to do, I'd have a GetPeopleOver30Query which gets and transforms the people directly in that query instead of passing it around, pulling information that isn't required
Tvde1
Tvde1•3y ago
what would the advantage of MediatR be in this case?
WibblyWobbly
WibblyWobblyOP•3y ago
Great thanks 🙂
Want results from more Discord servers?
Add your server