#Mediator 9 tutorial registration#
The Register action method is decorated with attribute and accepts the new user registration requests.
#Mediator 9 tutorial install#
We will need to install MediatR and packages from NuGet. We have already discussed the command pattern so it is time to define some commands and issue the command by using MediatR. Events (Caller do not care what happened next, do not expect result).Commands (Expect some output as result).The MediatR library supports two type of operations. MediatR allows us to decouple our controllers from our business logic by having our controller actions send a request message to a handler. It promotes the Single Responsibility Principle by allowing the communication to be offloaded to a class that handles just that.
After some time, however, we realize that there is also a business requirement of the confirmation email. Up until this, we have a simplified code. When a user places the order, we are supposed to save records in the database. We have a controller, OrderController, which is used to manage the orders. When we start development in MVC framework, the logic is written in action methods of the controller like we have a simple application of eCommerce where users are supposed to put orders. What are thin controllers and why we need them? What is MediatR? A broker class is also used which invokes the particular type of message class to handle the operation. Two type of commands are used and an interface is required which should be inherited by EmailMessageCommand and SMSMessageCommand classes. The Message class contains properties and methods like SendEmail() and SendSms(). The Invoker (Broker) object looks for the appropriate object which can handle this command, and passes the command to the corresponding object which executes the command.Ī simple example of this is messages of multiple types. We can say that the request wrapped under an object as a command is passed to an invoker object. An activity can have one or many commands and implementations. A command is some operation or action that we can perform and it can be the part of an activity. NET Core applicationīasically, a command pattern is a data-driven design pattern that falls in the category of behavior pattern. What are thin controllers in MVC, how our implementation makes the controller thin?.A simple example of Command Patterns and a short description of Mediator Patterns.So, we will be going through the following basics.
#Mediator 9 tutorial code#
In this article, I will explain command patterns and how we can implement them with a third party library which is built on command patterns, and how we can use it in ASP.NET Core to solve our problems and make the code clean.