This article is the third article in the series of articles
on the new personalization feature of ASP.NET 2.0, the Profile object.
The first article discussed the basics of a Profile object,
how is it different from the Session object, and how to use simple properties
in the Profile configuration section in the Web application configuration file
(web.config).
The second article discussed more advanced features on how
to use the Profile object, mainly, using custom defined objects as Profile
objects.
In this article, we are going to discuss the Profile
provider-model, which is a new concept implemented by most of the new features
in ASP.NET 2.0. In addition, the default SQL Profile provider that is
configured with SQL Server 2005 will be studied in depth, and I will present a
trick on how to use the same provider with SQL Server 2000.
In the first two parts of this series on the new Profile
object in ASP.NET 2.0, we explained the Profile object and implemented all the
possible ways in which a Profile object can be configured in the Web
application configuration file (web.config).
In this article, we will be looking at more advanced topics
including the new Provider model in ASP.NET 2.0, the Profile provider, creating
a custom Profile provider, and finally, how to configure the default Profile
provider to work with SQL Server 2000.
What is a Provider Model?
The new provider model is an interface module between an
ASP.NET 2.0 feature and the data store used to store the state of that feature.
Providers in ASP.NET 2.0 abstract the physical data storage for a feature from
the classes and the business logic exposed by a feature.
Some of the new features in ASP.NET 2.0 are based on a
provider model in order to have access to the data store used. ASP.NET 2.0
ships with a default provider for each of the new provider-based features that
are configured to work with SQL Server 2005 Express. However, if we want to
make the features interact with another data store like SQL Server 2005, Oracle,
or another database, then it is as simple as changing some configuration
sections in the Web application configuration file (web.config) without having
to change any single line of code in the features classes working each with
its specific provider. More on the provider model can be found in this
article: Introduction
to the Provider Model
Importance of the Provider Model
The new provider model introduces several advantages in
developing Web applications.
- It isolates the new features controls from the data store
used in the application. The only way for those controls to access the
data store is by using that provider.
- The application being developed can be used with any data
source of choice without having to change any line of code in those
features. We only need to create a custom provider and do some
configuration changes in the Web application configuration file
(web.config).
Profile Provider
The Profile feature has been designed with a provider-based
model. The Profile feature ships with a provider for Microsoft SQL Server
Express. You can create your own custom providers and configure them to work
with the Profile feature. Pages that use the Profile objects will continue to
work unchanged with your custom providers, since the Profile provider is a
middle layer between the data store and the classes that provide the different
methods to work with the Profile object.
More over, the Profile feature provides not only the Profile
object but also a ProfileManager to manage the Web application Profile objects.
This manager can handle both anonymous and authenticated Profile objects.
Management of Profile objects includes the following:
- Generating statistical information about all Profiles, for
authenticated users, as well as for anonymous users.
- Determining the number of Profiles that have not been
modified in a given period of time.
- Deleting individual Profiles as well as deleting multiple
Profiles based on a Profile's last modification date.
The diagram below shows the different layers of the new
Profile feature in ASP.NET 2.0:
Figure 1 - Profile Layer Diagram
As shown in Figure 1 above, the new Profile feature consists
of three layers:
- Profile classes that provide direct access to the Profile
properties.
- Profile providers that represent the abstraction layer
between the upper layer which is the Profile classes and the lower layer
which is the data store(s).
- Profile Data Stores that represent the storage medium
where the Profile property values are stored. In Figure 1 above, two main
data stores are shown: the SQL Server and SQL Server Express. Any other
data base can be added by just generating a custom Profile provider.
The ASP.NET 2.0 Profile provider implements all of the
methods and properties that are part of the Profile object and ProfileManager
class. Those methods and properties include the following.
- ApplicationName is the application name using the Profile
provider. Usually profile data are scoped to one application.
- GetPropertyValues returns the profile property
values from the data store used to store the Profile data.
- SetPropertyValues stores the profile property
values into the data store configured in the Web application.
- DeleteProfiles deletes profile records stored inside
the data store configured in the Web application.
- DeleteInactiveProfile deletes inactive profile
records stored inside the data store configured in the Web application.
- GetNumberOfInactiveProfiles returns the number of
inactive profile records inside the data store configured in the Web
application.
- GetAllProfiles returns a collection of ProfileInfo
objects containing information about all profiles stored in the data
store.
- GetAllInactiveProfiles returns a collection of
ProfileInfo objects containing information about all profiles stored in
the data store that has not been accessed from a specific time.
- FindProfilesByUserName returns a collection of
ProfileInfo objects containing information about the profiles of a
specific username.
- FindInactiveProfilesByUserName is similar to the
above method; however, it returns a collection of inactive profiles belonging
to a certain username.
The above methods and properties are all implemented by the
SQL Server Profile provider that is shipped with ASP.NET 2.0. This default
Profile provider is configured to be used with SQL Server 2005. In the next
section, we will discuss in detail how to make this default Profile provider
work with SQL Server 2000.