views:

74

answers:

2

Hi


a) When current user accesses Profile object for the first time, does Asp.Net retrieve a complete profile for that user or are profile properties retrieved one at the time as they are called?


b) In any case, is profile data for current user retrieved from DB each time it is called or is it retrieved just once and then saved for the duration of current request?


thanx

+1  A: 

If you want to save a database call, you can use a utility method to cache the profile or you can implement your own custom MembershipProvider which handles the caching.

The utility method is probably the simplest solution in this case unless you have other functionality you want to implement which would be served by implementing a custom MembershipProvider.

You can refer to this link for more details: http://stackoverflow.com/questions/146896/how-to-access-userid-in-asp-net-membership-without-using-membership-getuser

Here's an example of a utility method to do caching. You can pass a slidingMinutesToExpire to make it expire from the cache after some duration of time to avoid consuming excess server memory if you have many users.

public static void AddToCache(string key, Object value, int slidingMinutesToExpire)
{
    if (slidingMinutesToExpire == 0)
    {
        HttpRuntime.Cache.Insert(key, value, null, System.Web.Caching.Cache.NoAbsoluteExpiration, System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.NotRemovable, null);
    }
    else
    {
        HttpRuntime.Cache.Insert(key, value, null, System.Web.Caching.Cache.NoAbsoluteExpiration, TimeSpan.FromMinutes(slidingMinutesToExpire), System.Web.Caching.CacheItemPriority.NotRemovable, null);
    }
}
dcp
So I gather that with website projects profile data for current user is retrieved from DB each time it is called?
PrgGnt
+1  A: 

You don't specify if this is a Website Project or a Web Application Project; when it comes to profiles there is a big difference as they are not implemented out of the box for the Web Application Project template:

http://codersbarn.com/post/2008/07/10/ASPNET-PayPal-Subscriptions-IPN.aspx

http://leedumond.com/blog/asp-net-profiles-in-web-application-projects/

Have you actually implemented it yet or are you just in the planning stage? If the latter, then the above links should provide some valuable info. As regards the caching issue, I would go with Dave's advice.

IrishChieftain
1) I didn't know there was a difference - in that case my initial questions refer to Website project2) May I ask why Web Projects don't implement profiles out of the box?
PrgGnt
Microsoft never saw fit to provide them for some reason. If you follow the solutions in these links, it is pretty easy to implement. Get them working first and then worry about caching issues, which are easy to solve :-)
IrishChieftain