We see in the previous initialize method that it uses
settings it retrieves from the configuration file. This is the next part of
the provider setup. To do this, the NewsletterSection exposes a
DefaultProvider and Provider collection property to store the name of the
default provider, and contains a list of providers that the static class will
use (discussed later). The following NewsletterSection class, inheriting from ConfigurationSection,
has these properties:
Listing 6
[
ConfigurationProperty("defaultProvider",
DefaultValue = "AspNetDatabaseNewsletterProvider"),
StringValidator(MinLength = 1)
]
public override string DefaultProvider
{
get { return (string)this["defaultProvider"]; }
set { this["defaultProvider"] = value; }
}
[ConfigurationProperty("providers")]
public ProviderSettingsCollection Providers
{
get { return (ProviderSettingsCollection)this["providers"]; }
}
The ProviderSettingsCollection class is a standard .NET
framework collection for exposing provider elements in the configuration file.
As with any provider, you need the name of the provider to reference it by, and
the type of the provider to implement, which could be located in the App_Code, bin,
or global assembly cache.
Listing 7
<newsletters defaultProvider="AspNetDatabaseNewsletterProvider">
<providers>
<add name="AspNetDatabaseNewsletterProvider"
type="Nucleo.Web.Providers.DatabaseNewsletterProvider, Nucleo.Web"
connectionStringName="NucleoDB" applicationName="Business" />
</providers>
</newsletters>
Obviously, each provider needs its own custom settings;
these settings are regulated by the Initialize method we saw previously. In
that initialize method, it made sure that the connection string name and
application name attributes are present. In this way, these two portions tie
in together in this way.