views:

55

answers:

2

Hey, I'm a first time write long time reader of this site.

I'm building an .NET MVC site, using ProfilBase to store a custom user object. The profile base is stored on an database on an other server than the webserver. I have an helper class, UserInfoHelper that contains static method to retrive username, email, telephone etc.

Right now a method to for examlpe get retive the email address looks like this:

  public static String GetEmail()
    {
        ProfileBase profileBase = HttpContext.Current.Profile as ProfileBase;
        UserObject user = (UserObject)profileBase.GetPropertyValue("UserObject");
        return user.PrimaryEmail;
    }

And from a view i call them like this:

<%= UserInfoHelper.GetEmail() %>

So for each time when i call a method in the helper class the user is fetch from the db. I would like it to just fetch it once for each user when loged in or att least once per response. What is the best approach to do this.

Also is there an easy way to get the default ProfileBase using web services instead of calling the stored procedures direcly?

Many thanks in advance.

+1  A: 

I think the issue is the way you are using MVC more than anything. You are using a call to a helper method directly in your web page. Rather than doing this you should hand this processing off to controller or code that calls (one way or another) and then add that to the model and use something like <%=Model.Email%> in the view

Once you have this model implemented you would already have the processing only making the call once per request. At that stage you can profile your application and see if any further optimisation is needed. Options open to you then are to wrap the retrieval of the email address in some sort of caching that could persist between calls. But that's only if it's needed - you might be optimising prematurely as the overhead of only calling it once per request may be acceptable.

Paul Hadfield
+2  A: 

I can concur with what @Paul just replied...

Use cases like these are better handled with controller actions which return a partial view in which you would render user information. You would send an entire UserObject instance (or ViewModel representation of it) to the View and have the View strongly typed to that class. Then, if this would still represent performance issue, you could cache the controller action output by using OutputCache attributes on the action. This is the prefered way of doing things in ASP.NET MVC.

If, however, you have a page where you need lots of information from across different business domain objects, you can make composite View Model classes like this:

public class DashboardViewModel
{
    public UserObject User { get; set; }
    public BusinessData BizData { get; set; }
    public FirmObject Firm { get; set; }
}

In your view page, you start with this line

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<MyNamespace.DashboardViewModel>" %>

then you can render out anything from DashboardViewModel you've sent to the page like this:

<%: Model.User.Firstname %>

or

<%: Model.BizData.SalesTotal %>

I'm just making this up here but this is generally how it should be done.

mare
Ok, thanks for taking you're time answer. Maybe I should refractor it then. The thing is that i have a login user control in the top right corner that shows information about the user when logged in and i also has an control on the left that shows more user information + theres user information on some of the views in the logged in area. So if i do it in that way there will be at least 3 fetches for the same data.
I guess I was right about the fact that you are displaying the same data across multiple partial controls. You don't need three fetches. You fetch the data and prepare the model in the controller that is most general, controller that holds everything together, that would be for instance the Page controller.
mare