Creating Objects Using the Provider Factory Methods

Retired Content

This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This page may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.

The latest Enterprise Library information can be found at the Enterprise Library site.

A static factory method creates the appropriate configuration source object and then constructs a provider factory object. You can also use provider factory objects in your application code.

To create a provider by using a provider factory

  1. Create a configuration source object.
  2. Create the provider factory object, passing the configuration source object.
  3. Use the provider factory object to create the provider.

You can use the ConfigurationSourceFactory class to create a configuration source object. The Create method accepts a string parameter that identifies the name of the configuration source. This configuration source must be defined in the application configuration file (an exception will occur if the configuration source is not defined). This means you must have an application configuration file to use the ConfigurationSourceFactory. The following code shows how to create a configuration source using the ConfigurationSourceFactory.

IConfigurationSource source = ConfigurationSourceFactory.Create("fileSource");

You can also directly construct configuration source objects. The constructor of the configuration source accepts the information required by that configuration source. The following code shows how to construct configuration sources directly.

FileConfigurationSource fileSource = new FileConfigurationSource(@"ProductApplication.config");

SystemConfigurationSource systemSource = new SystemConfigurationSource();

The Enterprise Library includes the SqlConfigurationSource configuration source provider as a sample. This provider uses the Data Access Application Block to read configuration settings from a SQL database. The section used by this provider must derive from the SerializableConfigurationSection class. See the SqlConfiguration QuickStart (located in the QuickStarts folder) for an example of using this configuration source.

Notification of Configuration Changes

All configuration source classes implement the IConfigurationSource interface. This interface allows your application code to subscribe to notifications of configuration changes. In the Enterprise Library, only the Logging Application Block registers to receive notifications of configuration changes.

The constructor for a provider factory object accepts a configuration source object. The provider factory object uses this configuration source to read configuration information for the provider object.

SystemConfigurationSource systemSource = new SystemConfigurationSource();
DatabaseProviderFactory factory = new DatabaseProviderFactory(systemSource);

You can use a single provider factory instance to construct multiple provider implementations. You can also use multiple provider factory objects to create application block objects from multiple configuration sources.

The following code shows how to create two DatabaseProviderFactory objects with different configuration source objects. The DatabaseProviderFactory object factory1 reads the configuration information from the file ProductApplication.config. The DatabaseProviderFactory object factory2 reads the configuration information from the application configuration file.

FileConfigurationSource fileSource = new FileConfigurationSource(@"ProductApplication.config");
DatabaseProviderFactory factory1 = new DatabaseProviderFactory(fileSource);
Database db1 = factory1.CreateDefault();

SystemConfigurationSource systemSource = new SystemConfigurationSource();
DatabaseProviderFactory factory2 = new DatabaseProviderFactory(systemSource);
Database db2 = factory2.Create("Northwind");

Usage Notes

Here are some additional points about provider factories:

  • A provider factory instance is bound to a single configuration source. You cannot change the configuration source for a provider factory object after you construct that object. This means that the configuration settings for each provider object returned by that instance are read from the same configuration source.
  • The following application block provider factories always return the same instance for the named provider:
    • CacheManagerFactory
    • SecurityCacheProviderFactory
    • ExceptionPolicyFactory