views:

51

answers:

1

I'm an app where everything is tied to the currently connected user. So far almost all my actions have a call to my UsersService.GetUser(guid) method which brings the user and all its associated data. It does work ok but having said call in every action is really bothering me.

After giving it 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 have my controllers inherit from the base controller and use CurrentUser instead of calling the service in every action. Still, it feels like I'm just hiding the dirt under the rug.

So please, if you have a better way do share.

+1  A: 

To me, what you are doing makes sense to me. Use of a Base controller is good practice. One thing to maybe expand on is place the user in a session object so you don't need to hit the db every request.

Jeff Spicoli
My only grief with stoing it in a session object is that data would be cached and we guessed users would like to keep updating the data they'll be managing.
JoseMarmolejos
Yeah, gotcha. You could implment a method in your base class to clear that session object and where you update the User you could call that method. This way, it will be updated when you call the CurrentUser property again.
Jeff Spicoli