For demonstration, I’m converting the MusicStore application that’s using ASP.NET Core MVC.
If you’re new to this series, here are earlier posts in this series:
A command is request to change the state of our domain.
The first controller action we are going to rewrite is
Here is what the method originally looked like this.
As with our simple query, there are a couple things going on here.
- MVC is handling the albumId which is pass as an parameter to the method.
- We do some data access to get the album out of our EF DbContext
- Pass the album to our ShoppingCart
- Return a redirect to the Index action
Extract to Command
What we want to extract are the aspects that MVC is handling. From the list above it’s the
AlbumId being passed the URL/Route and then redirecting to the Index action.
Everything else is our feature, so let’s extract it into a command.
To do so, we need two pieces of information to add to our Request/Command. The
CartId and the
So we will create our Request/Command to contain that data.
Now we can create our handler for this Request/Command. Basically we are going to extract the majority of the code that was inside our Controller Action and put it into our handler.
You’ll notice we have our constructor take a couple dependencies on our
DbContext and the
Now we can jump back to our controller and create our
AddToCart Request/Command and send to the Mediator, which will invoke our Handler.
We’ve removed coupling of the the MVC framework from our feature code. We can now create AddToCart Request/Command from other locations and send along to the mediator to have it invoked.
That’s a very simple command endpoint that we moved our logic to a Command Handler using MediatR.
Next up we we will handle a more complex handler.
All the source code for this series is available on Github.
If anyone has any comments, suggestions or recommendations please leave comment or let me know on twitter.