• Latest Posts

  • Latest Comments

  • Archives

ProfileProvider in an MVC World

I had some trouble getting the ASP.NET Profile provider to work with MVC (Model View Controller). Here are my notes for what I did to get it to finally work thanks to the help of numerous posts. (I figured I should do something other than watch the Houston Rockets lose…)

Unit Testing

The first step was to try to get it to work in a unit testing environment. Anyone that knows me knows I’m interested in having it be unit testable.

Overview

I always had trouble unit testing the original profile provider. Placing the properties in the web.config file would result in a ProfileCommon class being auto-generated in the App_Code folder (as best I recall). In the MVC role that doesn’t seem to be happening. I don’t actually see that as all bad. So…

I seems the approach is to inherit from the ProfileBase and define your properties. This lets you leverage the ASP.NET Provider Model to the storage of the information. The two benefits I see to this approach is that it is far easier to unit test and I can have strong typing. All the types in the auto generated code were strings. While the information is stored in the database as a string I can at least consume it as a strong type.

So here we go…

The ProfileCommon class

In my class library I created a class called Karlz.Profile.ProfileCommon. This class uses the ProfileBase as its base class. The trick is to use the GetPropertyValue and the SetPropertyValue of the ProfileBase class. For example:

public virtual bool NotifyMeByEmail
{
    get
    {
        return ((bool)(this.GetPropertyValue(“NotifyMeByEmail”)));
    }
    set
    {
        this.SetPropertyValue(“NotifyMeByEmail”value);
    }
}

This is very nicely unit testable.

The app.config file

BUT… In order to unit test, I needed to get the app.config file correct. Here is what that looks like:

<profile enabled=true
inherits=Karlz.Profile.ProfileCommon, Karlz
  
defaultProvider=ZachrysProfileProvider>
  <!–
 Not supported in MVC. –>
  <!–
<properties>
            <add name=”Language” defaultValue=”en-US” />
            <add name=”FirstName”/>
            <add name=”LastName”/>
            <add name=”NotifyMeByEmail” defaultValue=”true”/>
            <add name=”MyIndividualId”/>
            <add name=”ExcludeNameDisplay” defaultValue=”false”/>
            <add name=”ExcludeEmailDisplay” defaultValue=”true”/>
        </properties>
–>
  <
providers>
    <
add name=ZachrysProfileProvider
         connectionStringName=ZachrysEF applicationName=/
         type=System.Web.Profile.SqlProfileProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a />
  </
providers>
</
profile>

 

It is very important to include the dll in the inherits if you are using a class library.

And with that I was able to finally get the Profile provider to work in an MVC environment. And frankly, when it becomes documented, I actually prefer the way this works over the way the previous ASP.NET 2.0 Profile Provider worked. Mainly because it is easier to unit test.

3 Comments  »

  1. Jonas says:

    Following sentence did make my day: “It is very important to include the dll in the inherits if you are using a class library”.

    I’m pleased finding your blog.

    Thank you!

    Regards, Jonas

RSS feed for comments on this post, TrackBack URI

Leave a Comment