1/3/2023 0 Comments Mediator 9 tutorial![]() ![]() A request could be a new object which should be saved in the database or an id of on object which should be retrieved. The request is sent to the handler which processes this request. Using the Mediator patternĮvery call consists of a request and a handler. Now, I can use the IMediator object with dependency injection in my controllers. In the OrderApi, I am also using the ICustomerNameUpdateService interface as mediator. I can do this because the controllers are in the same project. In the Startup class, I registered my mediators using: I am installing the MediatR and the in my Api project. Additionally, I am using the MediatR NuGet package, which helps to call the services. ![]() In my microservices, the controllers call all needed services and therefore work as the mediator. You can find the code of the finished demo on GitHub. The mediator can become such a crucial factor in your application that it is called a “god class”.There is also one big disadvantage of the mediator pattern: Open/closed principle: Adding new mediators can be done without changing the existing code.Single Responsibility Principle: The services don’t have any logic to call other services, therefore they only do one thing.Easier reuse: Fewer dependencies also helps to reuse classes.Less coupling: Since the classes don’t have dependencies on each other, they are less coupled.The mediator pattern brings a couple of advantages: Advantages and Disadvantages of the Mediator Pattern Instead, they call the tower, their mediator, and the tower talks to all planes and organizes who goes where. It would be impossible for a plan to talk to all other planes. They all need to be coordinated to avoid crashes. Think about a big airport like JFK with many of arriving and departing planes. You can see the mediator pattern also in real life. No service has a dependency on another one, only on the mediator. The mediator pattern serves as an organizer and calls all needed services. Services or classes often have several dependencies on other classes and quickly you end up with a big chaos of dependencies. The main goal is to disallow direct communication between the objects and instead force them to communicate only via the mediator. ![]() The mediator pattern is a behavioral design pattern that helps to reduce chaotic dependencies between objects. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |