MVC: What would be the best way to keep a link to the current user in your controllers?

I am an application where everything is tied to the currently connected user. So far, almost all of my actions have called my UsersService.GetUser (guid) method, which fetches the user and all the data associated with it. It works fine, but speaking of every action really bothers me.

After some thought, I decided to go with a base controller that declares something like

protected UserProfile CurrentUser
    {
        get { return UsersService.GetUser((Guid)Membership.GetUser().ProviderUserKey); }
    }

      

And then my controllers inherit from the base controller and use CurrentUser instead of calling the service in every action. However, it seems to me that I am just hiding the dirt under the rug.

So please if you have a better way to share.

+2


a source to share


1 answer


To me, what you do makes sense to me. Using a base controller is good practice. One thing that possibly expands is to put the user in the session object, so you don't have to hit the db every request.



+1


a source







All Articles