Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storing a global object in ASP.NET MVC

I'm currently building a rather large ASP.NET MVC application with user logins. Some methods/functions are restricted to users of a certain type/role (not the regular ASP.NET Role, though) and when running these functions, I have to check in the database if the current, logged in user has access to perform the given task.

As you probably can imaging, this leads to quite a few calls to the database, to get the person. So, currently, for every page load and every restricted method, I'm making this call:

var person = ctx.People
             Include("Person_Firm_PersonResponsibility.Firm")
             .Include("Person_Firm_PersonResponsibility.PersonResponsibility")
             .Include("Person_Firm_PersonOption.PersonOption")
             .Include(x => x.City)
             .Include(x => x.Country)
             .FirstOrDefault(x => x.ID == personId);

Since I need all of these informations for globally displayed data.

Is there any way to simply make this call once (i.e. when a user logs in) and then store that object somewhere for later use? Obviously, a Session variable could be used, but since the person object contains some sensitive data, I'm not sure if that would be a good idea ;-)

Any help is greatly appreciated!

Thanks in advance.

like image 780
Bo Mortensen Avatar asked Dec 06 '25 22:12

Bo Mortensen


1 Answers

Depending on the scope that you wish to have, you can use:

  • Session: for the current user, in the current navigation session (no other user can see it);
  • Cache: for all users, with a fixed expiration;
  • Application: for all users, with no expiration.

For cache and application, the key might be composed by some user property, such as id or login. These guys (https://efcache.codeplex.com/) have implemented a second level cache for Entity Framework, might be worth checking it out.

like image 112
Ricardo Peres Avatar answered Dec 09 '25 14:12

Ricardo Peres



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!