2

I decided to use Mediator Pattern (with Mediatr) to call my application layer. It's cool and I got how to work with it sometimes, but sometimes I get confused.

For example, when we publish a document on our new product, we uses a RequestHandler to do everything and check all rules it needs, it's fine and works like a charm, but, when I want just a quick data, it looks likes too much for just a simple thing.

As an example, every time the user does any kind of action on my web application, I have to check if he is still logged in. We have single login per user, so, if the same user connect anywhere else, the older session expires. We do it by saving on database.

On every action of my app, I go to base and check if the session key is the same, as bellow.

var sessionKey = bibliotecaCookie.Value;
var mediator = controller.GetMediator();
var isUserSessionKeyValidRequest = new IsUserSessionKeyValidRequest() 
{
    sessionKey = sessionKey
};
var isValidSession = mediator.Send(isUserSessionKeyValidRequest).Result;
if (!isValidSession)
    throw new UnauthorizedAccessException();

So, I have a RequestHandler (a Handler and a Request wich returns a bool) just to check if the user session is ok. When the handler catches this request, it goes to database and execute a simple rule which is "Is the passed session the same as the stored session key?".

EDIT

Just trying to clarify my question as asked in comments, how much granulated handlers should be in a mediator pattern. Should I use calls like "Is User Logged?" and with the response call again with "Do Something" or should I use something like "Do Something but throw exception if user is logged"?

On the first example I have two handlers, so, two calls should be done. On the second (Do something but throw...) I have only one call which expects the user to be logged and if not, throws an exception.

  • 1
    What specific problem in your project is your use of the Mediator Pattern trying to solve? – Robert Harvey Aug 6 '19 at 20:17
  • 8
    If you don't need it, you don't need it. Every software pattern, technique and tool must prove it's worth; otherwise, adding it is worse than leaving it out. – Robert Harvey Aug 6 '19 at 20:29
  • 2
    So unless you have a specific reason to put it in, you don't need it. – Robert Harvey Aug 6 '19 at 20:29
  • 1
    I think you should focus on why clean architecture uses mediator and cqrs. The reasons you come up with are the same reasons you would use them in your own projects. – Robert Harvey Aug 6 '19 at 21:06
  • 2
    If I understood you correctly, your question is really more about how to go about the kind of logic you described within the context of ASP.NET MVC, and it doesn't have much to do with the Mediator pattern itself (you happen to use it, but it's just a "cog in the machine"). – Filip Milovanović Aug 7 '19 at 8:53
1

Is it necessary to check if the user is logged in every time he does something? Can't you solve this by having an event handler? The standard response is to have event handlers here.

At the login event have an event listener that listens to the login event. So when a user logs in, if it's a user who has already logged in from somewhere else you can end the previous session of that user.

You can pass the event listener to the login code as a callback method from your application layer.

I haven't tackled the Mediator part of your problem. As another user says I also feel that your concern is more about how to go about this kind of logic within the technologies you're using.

0

Some concerns are globally valid for all your calls. For example a call needs a user that is authenticated for the method they want to call. Or maybe you want to log all the parameters of every call. Or maybe you want routing based on a version parameter in your API.

So there is a stack of middlewares in ASP.NET that gets called for every method. That is the point to implement for example user authentication. So you implement it once and it gets used everywhere.

It's pretty independent on how you actually structure your controllers and methods or which additional framework or pattern you use.

I don't know which exact version and .NET Framework you use, but you should be able to find a link for your version if you put "middleware" and your version of ASP.NET MVC into your search engine of choice. This is the link to the current version at the time of writing this.

  • Thanks a lot about your answer. I'm not using asp.net core yet, but you did open my mind at least enough to understand better a few terminologies. – Leandro De Mello Fagundes Aug 7 '19 at 14:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.