Sponsor: Do you build complex software systems? See how NServiceBus makes it easier to design, build, and manage software systems that use message queues to achieve loose coupling. Get started for free.
I’ve been meaning to create a sample application that uses the
MediatR library to dispatch command and queries. All of this organizing and writing your code in vertical slices rather than layers.
Why?
I can only speak from my experiences and how I came to applying CQRS with mediator pattern. Hopefully you can can relate and translate it to your own code base and determine if it is a
right fit for you.
And that’s really important. Is it a right fit for your application.
What this series will demonstrate is not to be used as silver bullet. Nor should it be used everywhere.
My intent with this series is to show how to use command and queries to decouple your application from you web framework.
And that’s exactly how I came to using command and queries. By realizing I was building an application that was incredibly tied to my web framework. I had little ability to take any code and easily move it around.
Fat Controller
Over time, my web application started to becoming was a collection of massive controllers. They did everything from being HTTP endpoints, basic data validation, data access, business logic, authorization and probably had a pile more responsibilities.
Multiple Uses
As mentioned before with my fat controllers, I had no ability to reuse code in any meaningful way. A lot of the code lived in controllers. If by chance it wasn’t in a controller, it likely had some dependency on the MVC framework in some way.
Why is that problematic? Because I wanted to reuse some of these features in a new application that was being developed as a native Windows app.
Hiding Use Cases
I remember watching a video from Uncle Bob where he talked about how an MVC applications using a specific framework all looked the same and did not describe at all what the application did.
What were the actual uses cases?
What features did this application provide?
The project layout and structure were all the same. Two applications that provided completely different functionality but were structured and organized the same way.
This rang very true to me at the time. I was hiding my uses cases by burying and coupling them to a web framework.
Music Store
Instead of writing an demo application from the ground up, I figured it would be a good idea to take an existing demo app and convert it using commands, queries and features slices.
I’m going to use the
ASP.NET MusicStore sample application (.NET Core), since it has both a combination of HTML views and JSON endpoints.
If anyone has any other suggestions or recommendations to a different sample application to use, please leave comment or let me know on twitter.
Hi Derek, I’ll be watching this with interest. I’m contributing on the the https://github.com/htbox/allready project where we put in some basic Mediatr patterns to separate our concerns and it’s been really helpful. While not fully complete, our controllers and actions are becoming much more focused.
Awesome. Just took a look at and you’re definitely inline with my approach.
Cool idea.
I was thinking to refactor our codebase to Commands/Queries as well for obvious reasons.
I am curious how you will handle complex ViewModel creation process with mediator.
Waiting for updates 🙂
I’m not sure how “complex” some of the ViewModels are in the MusicStore app. I suspect pretty simple so I may have to deviate and create something myself for the purpose of example.
I would do that …most of the real world apps have some logic behinn model creation and would be nice to have something clean in place based con Queries.
Great idea to convert an existing app!
I agree with the others, this is a great idea and I’m glad you’re doing it. Thanks and looking forward to more information on it!
Great idea. My team fully converted to CQRS and MediatR and the benefits are great. It was difficult figuring everything out in the beginning, so doing a sample app will surely help many in future.
Will you use Nancy?
+1 for Nancy, if it counts.
Actually this is a good idea. I’ll make it apart of it’s own which would be after I have sliced up different features, I’ll change some of them to use Nancy.
Pingback: Fat Controller CQRS Diet: Vertical Slices - CodeOpinion
Pingback: Fat Controller CQRS Diet: Trade-offs - CodeOpinion
Pingback: Fat Controller CQRS Diet: Command Pipeline - CodeOpinion
Hi Derek, I’ve been following this series on youtube and your blog with interest. I would love to watch the Uncle Bob video about use cases that you mentioned. Do you possibly have the link to it? Thanks.
I believe this was the talk. Around the 22min mark.
https://www.youtube.com/watch?v=HhNIttd87xs
Thanks Derek. I look forward to checking that out.