Creating Objects Using the Static 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.

The Enterprise Library application blocks include static factory methods that you can use to create application block objects. These static methods implement the Factory pattern to instantiate providers. This means that if your applications use these static factory methods, they do not require any information about the specific implementation of the provider being created.

The Enterprise Library Core provides the support for creating objects that require configuration information. Figure 1 illustrates how the Enterprise Library Core builds these objects.

Ff649473.da4e5a90-e410-4846-bce0-b451baacd766(en-us,PandP.10).png

Figure 1
Application block factories use of the Enterprise Library Core



Ff649473.note(en-us,PandP.10).gifNote:
The SqlConfigurationSource shown in Figure 1 is implemented in the QuickStart named SqlConfiguration that demonstrates how you can store configuration information in a database.


The following code shows how an application that uses the Data Access Application Block DatabaseFactory class can create a Database object.

No code example is currently available or this language may not be supported.

The Database class is an abstract class and defines the common interface for the provider implementations. The static method CreateDatabase returns a specific provider implementation type object (for example, a SqlDatabase object.) The type of object returned is determined by application configuration information.

The static factory methods use configuration information from the default configuration source. You can use the Configuration Console to define the default configuration source for your application configuration. If you do not define a configuration source, the static factory methods will use the System Configuration Source. This means that your configuration information must be stored in your application configuration file (App.config or Web.config).

The Configuration Console stores information for the configuration sources in the application configuration file. The following XML shows a configuration section in the application configuration file that defines two configuration sources.

<enterpriseLibrary.ConfigurationSource selectedSource="systemSource">
  <sources>
    <add name="fileSource"
         type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.FileConfigurationSource,
              Microsoft.Practices.EnterpriseLibrary.Common"
         filePath="test.exe.config"/>
    <add name="systemSource" 
         type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.SystemConfigurationSource,
              Microsoft.Practices.EnterpriseLibrary.Common"/>
  </sources>
</enterpriseLibrary.ConfigurationSource>

Here are some additional points about configuration sources:

  • The static factory methods use the application configuration file to determine the default configuration source. This means that you must have an application configuration file when you use the static factory methods.
  • If your application configuration file does not contain the <enterpriseLibrary.ConfigurationSource> section, the Enterprise Library uses System Configuration Source. This means your application block configuration data must be stored in the application configuration file.
  • All static factory methods use the default configuration source. This means that your configuration information for all application block objects constructed with the static factory methods is stored in the same location.

Show: