Bo Mortensen
Bo Mortensen

Reputation: 975

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.

Upvotes: 3

Views: 1853

Answers (1)

Ricardo Peres
Ricardo Peres

Reputation: 14535

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.

Upvotes: 2

Related Questions