Welcome to fuzzydev Sign in | Join | Help
in Search

.Net

.Net Profile Object "my customers, my ambassadors" - Part 2

For those who haven't read my previous posting on ASP.Net profile object (.Net Profile Object "my customers, my ambassadors") can read here

This has been long overdue I couldn't post sequel as I was held up with other commitments.

Anyway, let's kick start the Story Of Profiling part 2.

To be short and sweet, a good website should remember my preferrences. So that the next time I visit , I get a personalized welcome.

So how do we achieve this without using the out of the box solution provided by ASP.Net 2.0?

  • Create couple of tables and stored procedures to store and manipulate user profile details.
  • Write few classes' which will hold user's preferrence.
  • Write data access code to store and retrieve user's preferrence.

That's not much work and we can very well go ahead and develop our own personalization framework.

What does ASP.Net 2.0 provide?

It not only provides us with predefined set of tables, stored procedures and classes but like all other new features it's using provider model and so customization is not a painfull task. Provider model is the best thing that has happened to ASP.Net as it allows us to plug and play the implementations, this is acieved with the mixture of configuration settings and judicious use of reflection.

Let's dive in to the code and config settings to implement personalization.

Before we get our hands dirty with any code let's just set the required configuration. Below given is the sample profile properties.

<configuration><system.web>

<profile>

<properties>
<add name="DisplayName">
<add name="ShowOnlineStatus" type="System.Boolean">
<group name="UserInterface">
<add name="PageTheme" defaultValue="Green">
<add name="EmailArticles" type="System.Boolean" defaultValue="false">
<group>
</properties>

</profile>

</system.web></configuration>

The green colored config section is specific to profile. Although the elements are self describing let me explain the elements and it's attributes. By adding these elements and attributes we are asking the IDE and ASP.Net to recognize these properties as part of the profile object exposed in our Page class.

So in the above scenario our profile object will have properties like DisplayName, ShowOnlineStatus which is of type boolean and also we are asking to group PageTheme and EmailArticles inside UserInterface. So the final profile object will have the below given structure

Page.Profile.DisplayName                           = "Sachin Joshi";
Page.Profile.ShowOnlineStatus                   = true;
Page.Profile.UserInterface.PageTheme     = "Blue";
Page.Profile.UserInterface.EmailArticles    = false;

But hold a second, how did our profile object in our page got to know about the configuration and exposed these strongly-typed properties. It's not magic but the ASP.Net parser have recognized the profile configuration and have generated a class called ProfileCommon inherited from ProfileBase and have exposed the configured properties. Just check your App_Code folder and you will find a class called ProfileCommon inherited from ProfileBase.

So far so good, we have created a very simple configuration for personalization and in our web pages we will give our users  the ability to save these details. But the question here is where exactly these details are saved.

Actually we have not yet configured any profile provider which helps us in storage and retrieval of these customized profile. SqlProfileProvider comes to our rescue, this class is part of the ASP.Net 2.0 framework and as the name suggests it stores and retrieves profile data to and from Sql Server.

Below given is the sample configuration of profile provider.

<configuration><system.web>

<profile>
  <providers defaultprovider="SqlServerProfileProvider">
    <add name="SqlServerProfileProvider"
         connectionStringName="SqlServer2005"
         applicationName="/"
         type="System.Web.Profile.SqlProfileProvider" />
  </providers>
</profile>

</system.web></configuration>

In the above config we are asking ASP.Net to use the connection string from the specified section and use the SqlProfileProvider. So when we use the profile object ASP.Net in turn will be responsible to create an instance of the right provider and use the provider to store and retrieve the profile data.

Below is the final configuration we will end up with to acieve the objective.

<configuration><system.web>

<!-- Connection String START-->
<connectionStrings>

<add
name="SqlServer2005"
connectionString="server=SOME_SERVER;database=SOME_DB;uid=XXXXXX;pwd=XXXXXX;"
/>

<add
name="MYSql"
connectionString="server=SOME_SERVER;database=SOME_DB;uid=XXXXXX;pwd=XXXXXX;"
/>

</connectionStrings>
<!-- Connection String END-->


<profile>

<!-- Profile Properties START-->
  <properties>
    <add name="DisplayName">
    <add name="ShowOnlineStatus" type="System.Boolean">
    <group name="UserInterface">
      <add name="PageTheme" defaultValue="Green">
      <add name="EmailArticles" type="System.Boolean" defaultValue="false">
    <group>
  </properties>
<!-- Profile Properties END-->

<!-- Profile Providers START-->
  <providers defaultprovider="SqlServerProfileProvider">
    <add name="SqlServerProfileProvider"
         connectionStringName="SqlServer2005"
         applicationName="/"
         type="System.Web.Profile.SqlProfileProvider" />

    <add name="MySqlProfileProvider"
         connectionStringName="MySql"
         applicationName="/"
         type="fuzzydev.Providers.MySqlProfileProvider" />
  </providers>
<!-- Profile Providers END-->
 
</profile>

</system.web></configuration>

Note that in the above configuration there are two connection strings, one pointing to Sql Server and the other to MySql. Similarly there are two providers one for each DBMS, now we can switch to any provider by changing the defaultProvider attribute in the providers section.

Our UI need not worry about which provider or dbms is being used, this is made possible with combination of polymorphism, configuration and reflection.

Before you start playing with it there are couple of things which would be helpfull.

  • Serialization

          By default all the primitive types are serialized as string, this can be

overriden by specifying the serializeAs attribute in the configuration

<add name="DisplayName" serializeAs="Binary" />

or

<add name="DisplayName" serializeAs="Xml" />

  • Allow Profiling For Anonymous Users

Profiling can also be done for anonymous users we just need to specify

allowAnonymous="true" in the configuration.

<add name="DisplayName" allowAnonymous="true" serializeAs="Xml" />

Anonymous users are tracked using cookies, this discussion will require a

whole new blog entry and so further details are deferred.

This is just a brief introduction of ASP.Net profiling, there are lot of things which needs to be looked in to like

  1. Writing our own custom profile class by inheriting from ProfileBase and setting the attributes like allowAnonymous, serializeAs to the properties.
  2. Writing our own profile provider for our custom data store for example Xml, Access, etc.
  3. Migration of saved preferrences of anonymous users when they are promoted to registered users.

ASP.Net profiling is a great way of adding richness to your website with the simplicity and flexibility of plug and play components.

Published 01 July 2006 by sachinjoshi
Filed Under:
New Comments to this post are disabled

This Blog

Post Calendar

<July 2006>
SuMoTuWeThFrSa
2526272829301
2345678
9101112131415
16171819202122
23242526272829
303112345

Sponsored Links

Syndication

Copyright 2006, Sachin Joshi. All rights reserved.
The content on this site represents my own personal opinions and thoughts at the time of posting, and does not reflect those of my employer's in any way.
Powered by Community Server, by Telligent Systems