views:

92

answers:

3

Like most web applications you have a method that gets called when you log in. There are a few things that may need to get done when logged in and over time this may increase. eg. logging, welcome emails, maintenance.

Should events be used to do this or is there a better way?? I'm using C# and ASP.net MVC.

Update
This is already in its on Service Layer class. eg.

  public void Login(User user)
        {
            SetAuthCookie(user);
            LogLogin(user, true);
            SendEmails();
        }
A: 

Aspects might be a good way to do it. If they're cross-cutting concerns, AOP is the way to go.

duffymo
+2  A: 

Extract the application logic to separate classes. Your application will be easier to work with if you keep the controllers as thin as possible.

The Post, Redirect, Get pattern is effective for MVC. A good post about this from my bookmarks collection is: http://www.eworldui.net/blog/post/2008/05/ASPNET-MVC---Using-Post2c-Redirect2c-Get-Pattern.aspx

I would also recommend looking into Action Filters. Robert Conery has a good introduction to using action filters on his blog at http://blog.wekeroad.com/blog/aspnet-mvc-securing-your-controller-actions/ . The post is authentication-specific, but the concepts can be extrapolated to whatever type of functionality you want to implement.

smartcaveman
Yeah...i know this, but maybe i worded the question wrong as this doesn't answer it
Schotime
A: 

I actually prefer your current approach. However, I have seen examples with events but to me it seems more effort than its worth unless you have a 'very' complex application.

I think what you are referring to is Domain Events. Also, have a look at this blog post by Jimmy Bogard which is based on the first link. And finally, from SO - http://stackoverflow.com/questions/1488739/how-to-avoid-anemic-domain-models-or-when-to-move-methods-from-the-entities-into

My advice - try to keep you design as simple and manageable for as long as possible is a way that makes sense to you. Unless there is a very compelling reason to change you current approach to use events, stick with it.

Ahmad