1. Create an application module object
In your application, you often need to create objects such as database or cachemanager. The enterprise database supports a series of methods to create the objects required by the application.
You can use the factory method to create these objects. The Enterprise Library contains the factory method in each function module and core library to complete this task. These static factory methods automatically read the application configuration information. The provider factory method allows you to manually create the appropriate configuration in the configuration file, and then create the provider in the code using the factory method.
You can also create objects that are not described in the configuration information. For example, you can create a sqldatabase object by adding database connection information to the constructor.
In addition, the Enterprise Library 4.1 contains a lightweight and scalable dependency injection container that supports constructor, attribute, and method call injection. The unity module.
The following describes how to create an object using different methods:
- Use static methods to create objects
- Use the factory method of provider to create an object
- Use unity to create objects
- Directly use constructors to create objects
Tooltip: In addition to creating Enterprise Library objects, you can also use unity to create custom business objects.
1. Use static methods to create objects
The static method of the Enterprise Library implements the factory mode. If you use these factory methods to create objects, you do not need to implement any provider information required to create objects.
Shows how to create objects in the core library
The following code uses the databasefactory method of the data access module to create a database object
Database DB = databasefactory. createdatabase ("northwind ");
Database is an abstract class that defines common interfaces for implementing providers. The static method createdatabase returns an object (such as sqldatabase) described in the provider. The object type is determined by the configuration information.
The static factory method uses the configuration information in the default configuration source. You can use the command line to specify the default configuration source. If you do not define the configuration source, the static method uses the system configuration source, which means that your configuration information must be stored in the app. config and web. config files of the application.
<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>
The preceding XML shows a configuration section defined in the application configuration file, which defines two configuration sources. One file is test.exe. config, and the other is the system configuration source.
Note the following when using the breeding source:
- The static method uses the configuration file to determine the default configuration source, which means that you must have an application configuration file when using the static method.
- If your configuration file does not contain </enterpriselibrary. configurationsource> in the configuration section, the Enterprise Library will use the System Configuration source, which means you must store the configuration information directly in your configuration file, instead of in the configuration source defined in the configuration section.
- All static methods use the default configuration source, which means that the configuration information required to create all module objects is placed in the same location.
2. Create an object using the factory method of provider
First, create an appropriate configuration source object using a static method, and then construct a provider object. You can also use the factory method of provider to create objects in the application.
To create an object using the factory method of provider, complete the following steps:
1) create a configuration source object
2) create a provider factory object using the configuration source object
3) use the provider factory object to create the provider
2.1 create a configuration Source
You can use the configurationsourcefactory class to create a configuration source object. The create method accepts a parameter, which is the name of the configuration source. This configuration source must be defined in the application configuration file first. If it is not defined, an exception is thrown. This means that if you use configurationsourcefactory, the application must have a configuration file. The following code creates a configuration source object using the configurationsourcefactory class.
Iconfigurationsource source = configurationsourcefactory. Create ("filesource ");
You can also directly construct the configuration source object. The constructor needs to configure the source information.
Fileconfigurationsource filesource = new fileconfigurationsource (@ "productapplication. config ");
Systemconfigurationsource systemsource = new systemconfigurationsource ();
Tooltip: The Enterprise Library contains an example of sqlconfigurationsource to configure the source provider. Use Data Access to read the configuration information stored in the SQL Server database. The configuration section used by this provider must inherit from the serializableconfigurationsection class. For more information, see sqlconfiguration Quickstart in the Enterprise Library.
Configuration update notification
All configuration source classes implement the iconfigurationsource interface. The interface definition is as follows. The following definition shows that the configuration source changes can be described using code.
Implement void addsectionchangehandler (string sectionname, configurationchangedeventhandler handler );
And void removesectionchangehandler (string sectionname, configurationchangedeventhandler handler); The method can implement update notification. In the enterprise database, you only need to register the logging module as an acceptance notification.
public interface IConfigurationSource{ // Methods void Add(IConfigurationParameter saveParameter, string sectionName, ConfigurationSection configurationSection); void AddSectionChangeHandler(string sectionName, ConfigurationChangedEventHandler handler); ConfigurationSection GetSection(string sectionName); void Remove(IConfigurationParameter removeParameter, string sectionName); void RemoveSectionChangeHandler(string sectionName, ConfigurationChangedEventHandler handler);}
2.1 create a provider Factory
The constructor parameter of provider is the configurationsource object. The provider factory object uses this configurationsource to read configuration information for the provider object.
Systemconfigurationsource systemsource = new systemconfigurationsource ();
Databaseproviderfactory providerfactory = new databaseproviderfactory (systemsource );
2.2 Use provider factory to create provider
You can use a provider factory to construct multiple providers. You can also use multiple provider factory objects created from multiple configuration sources to create application objects.
The following code creates two databaseproviderfactory instances from different configuration sources. The databaseproviderfactory object factory1 reads configuration information from the productapplication. config file, and the databaseproviderfactory object factory2 reads configuration information from the application configuration file.
FileConfigurationSource fileSource=new FileConfigurationSource(@"c:\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 tips:
A provider factory object can only be bound to one configuration source object. After you construct a provider factory, you cannot change the configuration source of this providerfactory.
3. Use unity to create an object
The Unity module provides developers with a new way to develop business objects and service interfaces, as well as organize and construct applications. When code writing and the Enterprise Library Work together, it also provides a new method. In enterprise libraries of Version 4.1, you can use the dependency injection container provided by unity to create Enterprise Library objects and your business objects.
This new function has no impact on existing code, and the Enterprise Library has done a lot of work to ensure compatibility with existing code and applications. However, if new applications are developed, unity is recommended, although it is not forced to be used by developers.
3.1 about the unity Module
The Unity module implements a container called unitycontainer and exposes some methods, including the methods used to register interfaces (or base classes, or abstract classes) and implementation classes (including instances and Singleton types) get the object instance, and use the dependent object to create an existing object. The instances returned by the resolve and buildup methods are suitable dependent object instances based on the types registered in the container. You can also create a hierarchical nested container to manage the lifecycle of objects and containers by yourself.
public interface IUnityContainer : IDisposable{ // Methods IUnityContainer AddExtension(UnityContainerExtension extension); IUnityContainer AddNewExtension<TExtension>() where TExtension: UnityContainerExtension, new(); T BuildUp<T>(T existing); T BuildUp<T>(T existing, string name); object BuildUp(Type t, object existing); object BuildUp(Type t, object existing, string name); TConfigurator Configure<TConfigurator>() where TConfigurator: IUnityContainerExtensionConfigurator; object Configure(Type configurationInterface); IUnityContainer CreateChildContainer(); IUnityContainer RegisterInstance<TInterface>(TInterface instance); IUnityContainer RegisterInstance<TInterface>(string name, TInterface instance); IUnityContainer RegisterInstance(Type t, object instance); IUnityContainer RegisterInstance<TInterface>(TInterface instance, LifetimeManager lifetimeManager); IUnityContainer RegisterInstance<TInterface>(string name, TInterface instance, LifetimeManager lifetimeManager); IUnityContainer RegisterInstance(Type t, object instance, LifetimeManager lifetimeManager); IUnityContainer RegisterInstance(Type t, string name, object instance); IUnityContainer RegisterInstance(Type t, string name, object instance, LifetimeManager lifetime); IUnityContainer RegisterType<T>(params InjectionMember[] injectionMembers); IUnityContainer RegisterType<TFrom, TTo>(params InjectionMember[] injectionMembers) where TTo: TFrom; IUnityContainer RegisterType<T>(LifetimeManager lifetimeManager, params InjectionMember[] injectionMembers); IUnityContainer RegisterType<TFrom, TTo>(LifetimeManager lifetimeManager, params InjectionMember[] injectionMembers) where TTo: TFrom; IUnityContainer RegisterType<T>(string name, params InjectionMember[] injectionMembers); IUnityContainer RegisterType<TFrom, TTo>(string name, params InjectionMember[] injectionMembers) where TTo: TFrom; IUnityContainer RegisterType(Type t, params InjectionMember[] injectionMembers); IUnityContainer RegisterType<T>(string name, LifetimeManager lifetimeManager, params InjectionMember[] injectionMembers); IUnityContainer RegisterType<TFrom, TTo>(string name, LifetimeManager lifetimeManager, params InjectionMember[] injectionMembers) where TTo: TFrom; IUnityContainer RegisterType(Type t, LifetimeManager lifetimeManager, params InjectionMember[] injectionMembers); IUnityContainer RegisterType(Type t, string name, params InjectionMember[] injectionMembers); IUnityContainer RegisterType(Type from, Type to, params InjectionMember[] injectionMembers); IUnityContainer RegisterType(Type t, string name, LifetimeManager lifetimeManager, params InjectionMember[] injectionMembers); IUnityContainer RegisterType(Type from, Type to, LifetimeManager lifetimeManager, params InjectionMember[] injectionMembers); IUnityContainer RegisterType(Type from, Type to, string name, params InjectionMember[] injectionMembers); IUnityContainer RegisterType(Type from, Type to, string name, LifetimeManager lifetimeManager, params InjectionMember[] injectionMembers); IUnityContainer RemoveAllExtensions(); T Resolve<T>(); T Resolve<T>(string name); object Resolve(Type t); object Resolve(Type t, string name); IEnumerable<T> ResolveAll<T>(); IEnumerable<object> ResolveAll(Type t); void Teardown(object o); // Properties IUnityContainer Parent { get; }}
The Unity module does not automatically read configuration information to create objects and configure containers. With maximum flexibility, you can use code to initialize a unity container, and then initialize registration information, type ing, and extension information. These will be defined in the configuration information loaded when you create a container. You can also use the method provided by the container to modify them after loading.
You can store the configuration information in the app. config or web. config file. You can also use a custom configuration file or custom configuration source.
In the Enterprise Library of Version 4.1, if you put the unity configuration information in the application configuration file (App. config and web. config), you need to manually configure the unity information. For specific configuration, see unity Application Block documentation.
The following table lists some methods of Unity containers. using these methods, you can return an appropriate Enterprise Library object.
| Method Signature |
Description |
| Resolve <t> () |
Returns the implementation class of a default object instance defined in the configuration. The type is generic. |
| Resolve <t> (string name) |
Returns the implementation class of an object instance with the specified name defined in the configuration. The type is generic. |
| Resolve (type T) |
Returns an implementation class of the default object instance defined in the configuration. The type is specified by parameters. |
| Resolve (type T, string name) |
Returns an implementation class of a default object instance defined in the configuration, which is specified by parameters. |
Not complete ................