Creating Application Block Objects Directly

Microsoft Enterprise Library 5.0

DropDown image DropDownHover image Collapse image Expand image CollapseAll image ExpandAll image Copy image CopyHover image

There are times when the configuration information for your application scenario does not reside in a configuration source. For example, your system may dynamically create configuration information from data entered by a user. For these situations, you can directly construct application block objects, passing the required configuration information to the constructor.


Note:
Other topics in this section show how you can inject resolved instances into your own custom classes, and how you can resolve instances of Enterprise Library objects on demand. For more information, see Injecting Resolved Types into Other Classes and Resolving Instances of Types Using Unity.


When you construct an application block object, you must construct a specific provider implementation type using the appropriate constructor parameters and pass the required arguments to the constructor. The following code shows how to construct the Data Access Application Block SqlDatabase object.

C# Copy Code
String connString = @"server=(local)\SQLEXPRESS;database=EntLibTest;" + "Integrated Security=true";
SqlDatabase db = new SqlDatabase(connString);
Visual Basic Copy Code
Dim connString As [String] = "server=(local)\SQLEXPRESS;database=EntLibTest;" + _
"Integrated Security=true"
Dim db As New SqlDatabase(connString)

Configuration Information for New Objects

Some object constructors have an overload that accepts an instance of a configuration source that implements the IConfigurationSource interface. This allows you to supply configuration information directly to the new object.

Dependency Injection for Existing Objects

When you create instances of objects without using the Unity dependency injection mechanism, dependent objects are not automatically injected into your new object. However, you can force Unity to resolve and populate dependencies by using the BuildUp method of the container. Keep in mind that constructor injection does not take place when you use the BuildUp method because the object already exists and so the constructor does not execute.

The following example shows how you can use the BuildUp method to apply dependency injection to an existing object instance named myDataService that implements the interface IMyService. For more information about the BuildUp method, see Using BuildUp to Wire Up Objects Not Created by the Container in the Unity documentation.

C# Copy Code
IMyService myDataService = new DataService();
IMyService builtupDataService = container.BuildUp<IMyService>( myDataService);
Visual Basic Copy Code
Dim myDataService As IMyService = New DataService()
Dim builtupDataService As IMyService = container.BuildUp(Of  IMyService)(myDataService)

Note:
Enterprise Library includes code to enable instrumentation. If you directly construct application block objects, instrumentation will not be enabled for those objects. However, in most cases, you can bind the appropriate instrumentation providers to the application providers. For more information about instrumentation listeners and instrumentation providers, see Enabling Instrumentation.