Separating your application layers into class libraries offers several advantages, but one of the disadvantages is that you lose access to ASP.NET's dynamically-compiled ProfileCommon class, which provides strong-typing for profile properties. Here's how to regain the advantage of strongly typed profile properties in a class library.
(EDIT: If you're looking for a similar example dealing with accessing profiles in web application projects, don't forget to check out this post as well.)
Profiles offer a much more reliable alternative to cookies or Session state for storing user information. Unlike cookies or Session state, profile properties are strongly typed, meaning that their types are known at design time. This lets us access them without casting, do compile-time checking on them, use Intellisense with them, and so on.
As you'll recall, profile properties are defined in web.config file as follows:
This is a pretty simple profile - just three properties, two String and one DateTime. Let's take a look at how we can refer to these properties from our code.
Strongly Typed Profile from the Page
Accessing strongly typed profile properties from the page is pretty easy - in fact, it's totally transparent in ASP.NET, requiring no explicit configuration whatsoever. This happens thanks to a bit of clever sleight-of-hand ASP.NET performs behind the scenes.
When you define a profile, ASP.NET dynamically creates a class called ProfileCommon which inherits from System.Web.Profile.ProfileBase. Here is the actual ProfileCommon class that is compiled from the profile defined in the example above.
ProfileBase provides untyped access to profile properties through its GetPropertyValue and SetPropertyValue methods. As you can see above, ProfileCommon inherits this class, and casts the untyped values to the types defined in web.config before returning them.
When the application runs, ASP.NET populates the Page.Profile property with an instance of this ProfileCommon class. This lets us read and modify the profile's properties in a strongly-typed way through the page's Profile property:
As I said, you don't have to do anything to make this work -- the runtime takes care of it all for us. So far, so good.
Strongly Typed Profile from a Class
Now, let's say we have a custom class in App_Code from which we want to access profile properties.
Obviously, our class won't have an automatically-populated Profile property like the Page class does. But fortunately, we do still have access to the ProfileCommon class, which means we can create our own Profile property.
As you can see, to create a strongly-typed Profile for use in a class, all we need to do is to get a reference to the Profile property of the current HttpContext, which is of type ProfileBase. Then, we can cast it to type ProfileCommon to get the strong typing. From there, we can use our created Profile property in any way we choose. To illustrate this, I've created a couple of methods that use it.
The following code shows how we can call this App_Code class from a page:
Strongly Typed Profile from a Class Library
Now, what if we now want to move this class from App_Code out to a class library?
You'll find that if you do this, you'll encounter the following build error:
The type or namespace name 'ProfileCommon' could not be found (are you missing a using directive or an assembly reference?)
Bummer. As you can see, the ProfileCommon class isn't accessible from an outside class library. What to do?
One option is to use the Profile object from the current HttpContext directly. This will work, but unfortunately we lose the benefits of strong typing. As you'll recall, HttpContext.Profile is of type ProfileBase, which provides only untyped access to profile information.
However, there is another option that will allow us to access a strongly typed Profile from a class library.
To do that, we need to define a new class in the library that contains the properties we want in our profile. This class must inherit ProfileBase. Let's call our class CustomProfile:
Then in web.config, instead of defining the properties directly, we'll inherit this new class in the
Now, we can use this type to define a Profile from any class in the library. This Profile will be of the type we just created and inherited in web.config:
Hey, it looks like we've got our strong typing back! If you think about it, this is somewhat akin to "rolling our own" ProfileCommon class.
Here's how we could now call our library class from a page:
As you can see, it's possible to access a strongly-typed Profile object from anywhere you need to in ASP.NET - from the page, from a dynamically-compiled class in App_Code, and yes, even from a class library. Enjoy!
Subscribe to this blog for more cool content like this!