I am coding a MVC 5 internet application, and I have a question in regards to if I should use the exact same method in each of my classes, or use a static class with this method instead.
Here is the method:
public Account GetAccount(string userName)
{
if (Session[userName] == null)
{
var userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext()));
ApplicationUser user = userManager.FindByNameAsync(userName).Result;
Session[userName] = user.account;
}
return Session[userName] as Account;
}
Currently, I am adding this method to many controllers and service classes. Instead of doing this in many classes, should I just declare this method once in a static class, and use this static class whenever I want to get an Account
object?
Are there disadvantages/advantages that I should be aware of?
Thanks in advance.
You are having multiple options, Option 1: You can create one separate class and put the method in that class, that class not necessary be static. Option 2: You can Create Extension Method of Account Class. Option 3: A. For Controller create one base class like : public class BaseController : Controller and inherit BaseColtroller for all controllers and B. same for service.
Thanks
Raviranjan