Recently for the first time I’ve used MediatR library in .NET Core. I quickly felt in love with it. In this post I want to share with you useful pattern I came up with working with MediatR.
When I started using MediatR I made all my commands and queries simple objects like this:
That’s perfectly fine. But I think it’s much better to implement them as immutable objects, because after command or query is created, it shouldn’t be changed by subsequent code. If object is immutable it’s guaranteed. It also helps to cleanup the code a little bit. Constructor is required, so default assignments can be removed. This approach also allows most commands and queries to be created in single line, without need for mapping libraries.
I like this pattern so much, that I’ve started using it for objects that are produced by handlers. So data transport object like this:
In my code it is implemented as immutable object, like this:
Reasons are the same as for making commands and queries immutable. I think it would be really bad design if objects returned by handler, were then updated by some other handler or method.
What about huge commands? Sometimes they could have more than twenty properties. It would be really messy if we would try to construct such objects in single line.
In those instances I implement mapping method. Let’s say data for command comes in following class.
I could change command code to include mapping method:
Problem with that solution is that it creates unnecessary coupling. Command shouldn’t depend on other classes. I think much better idea is to make From an extension method and keep it in separate class, like this:
This solution is much better. Command class should only be concerned with holding data. With this extension method we can implement creating CreateProductCommand in single line: