Complex Types:
You can even use Profile object with the Complex Types which includes Entity Classes.
Let's first make a very simple entity class.
Here is a very simple class:
| [Serializable]
public class User
{
/* PRIVATE FIELDS */
private string _firstName;
private string _lastName;
/* PUBLIC PROPERTIES */
public string FirstName
{
get
{
if (!String.IsNullOrEmpty(_firstName))
return _firstName;
else return null;
}
set { _firstName = value; }
}
public string LastName
{
get
{
if (!String.IsNullOrEmpty(_lastName))
return _lastName;
else return null;
}
set { _lastName = value; }
}
public User()
{
}
}
|
When using Profile object with the entity class you have to mark the class Serializable.
Now, let's see the settings in the web.config file.
| <anonymousIdentification enabled="true"/>
<profile defaultProvider="MyProfileProvider">
<providers>
<add name="MyProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="ConnectionString"/>
</providers>
<properties>
<add name="User" type="User" serializeAs="Binary" allowAnonymous="true"/>
</properties>
</profile>
|
The name attribute simply represents the name of the Profile. The serializaAs attribute represents that what will be the format of the serialized object. In this case I have defined the format as Binary but you can also choose XML, String or ProviderSpecific.
Now, let's see how you can use the Profile object.
| Profile.User.FirstName = "Andy";
Profile.User.LastName = "Johnson";
|
As you can see it is very similar to the approach that we were already using. One important question that we need to ask our self is that when is the Profile object updated when we are using an entity class.
You might be thinking that since we can any type to be used for the properties then we can also use WebControls.Image types. Let's see this in action:
| <anonymousIdentification enabled="true"/>
<profile defaultProvider="MyProfileProvider">
<providers>
<add name="MyProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="ConnectionString"/>
</providers>
<properties>
<add name="User" type="
System.Web.UI.WebControls.Image
" serializeAs="Binary" allowAnonymous="true"/>
</properties>
</profile>
|
When you used the Image type in your code and assign it anything it will throw an exception. The reason is because Image class is not serializable and for the Profile properties to work the class must be serializable. You can off course save the image path as a string and later retrieve the string using the path.
When is the Profile Object Updated?
The data is updated in the database whenever we assign something to the Profile Object and when the request is completed. The data will only be updated in the database after the request has been completed.
Migrating Anonymous Profiles:
The last thing that I will discuss in this article is migrating the anonymous profiles to authenticated profiles. When ever the user comes to the website he always has his anonymous profile in which the username is created using GUID. You can consider a situation that a user visits the shopping cart application and after all the shopping he is asked for the username and password. This means that all that time the user was shopping using anonymous authentication. You can easily migrate user's anonymous Profile by using the following code in Global.asax file.
| void Profile_MigrateAnonymous(Object s, ProfileMigrateEventArgs e)
{
ProfileCommon anonProfile = Profile.GetProfile(e.AnonymousID);
Profile.Name = anonProfile.Name;
Profile.PicturePath = anonProfile.PicturePath;
}
|
In the next article we will see how to make a Custom Profile Provider.
I hope you liked the article, happy programming!