共用方式為


Creating Application Block Objects

You can resolve instances of both Enterprise Library objects and your own custom classes using the container. To understand how to resolve the appropriate Enterprise Library objects, you must be aware of the way that the Enterprise Library configuration information for your application maps to the registrations in the container.

Default and Named Object Registrations

Most of the Enterprise Library application blocks use plug-in providers to accomplish their tasks, while providing the flexibility required for use in the widest range of scenarios and environments. For example, you might add two cache managers to the Caching Application Block; one that accesses a database backing store, and one that uses the local Isolated Storage mechanism. If you use the Data Access Application Block, you might define several connections to different databases, depending on the requirements of your application.

Each provider is identified by a name, and the mappings between the provider interface or base class and the concrete implementations of the providers are differentiated by this name. An application configuration may, for example, define two cache managers, DBStore and LocalStore, while the Data Access Application Block configuration may contain mappings named CustomerDB, SalesDB, and ProductsDB.

When you create instances of these objects using any of the approaches shown in the related topics listed below, you must provide the name to select the appropriate object. However, Enterprise Library also has the concept of a default provider for most of the blocks. This is specified in the configuration, and defines the provider that the block will use if you do not specify a named provider. When you resolve a type without providing a name, the container will return an instance of the default provider if it is specified for the block. This is a useful feature that makes it easy to switch the application to use a different provider by just changing the default for the block in the configuration file.

For information about configuring default and named providers, see the topic "Entering Configuration Information" in the section "Developing Applications" for each of the application blocks.

Creating and Resolving Objects

The following topics describe in more detail the three main scenarios for creating or injecting instances of objects:

For details on how to create and reference objects see Creating and Referencing Enterprise Library Objects. For information about initializing the Enterprise Library container and—if required—storing a reference to it, see Storing a Reference to the Container.

Note

Previous versions of Enterprise Library used static factory methods to create application block objects. The static facades and static types are included in this version of Enterprise Library, and existing code that uses them will continue to work. For more information about using the static facades and static types, see the online guidance at https://msdn.microsoft.com/entlib/.

One point to note if you are familiar with the provider factory approach for generating instances of objects used in versions of Enterprise Library prior to version 5.0 is that the static facades used in these earlier versions cannot be injected. New non-static facades are included in this version of Enterprise Library.

For more information about Unity, see Unity Dependency Injection and Interception. For details of how Unity integrates with the underlying configuration mechanism in Enterprise Library, see The Dependency Injection Model.