0

I was reading the followed guide in order to know how to share a common view model, but I have a dubt,

Sharing common view model data in asp.net mvc - The objective of the article is to populate authomatically a common view model "LayoutModel" with all their properties using ActionFilterAttributes

 public class HomeModel : LayoutModel
    {
        public string Content { get;set; }
    }

The content is populated using an _userService that will return the "CurrentUser", My question is:

Where I have to store my common data? User data etc..? because I don't want to call the database each time that the filter is executed, and I don't know what I have to do on the userservice in that case, I was reading that is not a good idea to store it in session, some ideas will be very agreed!

public void Set<T>(T model) where T : SharedContext, new()
        {
            var user = _userService.GetCurrent();

            model.User = user;
            model.UnreadMessageCount = _userMessageService.GetUnreadCount(user.Id);
        }
user1520494
  • 1,134
  • 2
  • 11
  • 27
  • Instead of session you could store the data in ASP.NET cache – Stefan P. Sep 05 '14 at 11:37
  • Hi stefan, Thanks for your response, the info that will be stored is only per-user and is not too large.. I don't know if store that in cache for that case will be a good aproach – user1520494 Sep 05 '14 at 11:51
  • You can prefix the cache key with your client ID. I think the cache is the best place for you because data like "UnreadCount" should expire quite often and the ASP.NET cache lets you specify that. – Stefan P. Sep 05 '14 at 11:58
  • Ok, With the (user.Id) I can create a key for the cache and then retrieve all data. That's true, and also a good idea, but my small problem is that I don't know how to retrieve the user using _userService.GetCurrent(); I don't know what I have to do there, in order to get the current user. xD – user1520494 Sep 05 '14 at 12:09
  • Take a look at this http://stackoverflow.com/questions/18448637/how-to-get-current-user-and-how-to-use-user-class-in-mvc5 – Stefan P. Sep 05 '14 at 12:13
  • Where did you read not to store in session? IMO that's exactly where this should be stored. – Mike C. Sep 05 '14 at 12:14
  • It depends on what you mean by _"is not too large"_. and if the data is static, but another option is to use a `CustomPrincipal` where the additional data is read once at login and serialized to the authentication ticket so its available on each request –  Sep 06 '14 at 02:38

0 Answers0