What is the best way to handle multiple actions?
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)
开发者_如何学C {
SetAuthCookie(user);
LogLogin(user, true);
SendEmails();
}
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.
Aspects might be a good way to do it. If they're cross-cutting concerns, AOP is the way to go.
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 - How to avoid anemic domain models, or when to move methods from the entities into services
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.
精彩评论