Microsoft enterprise database 5.0 Study Notes (6) Create and reference enterprise database objects

Source: Internet
Author: User

Generally, you can use either of the following two methods to create an enterprise database object instance.
1. Use the Unity Server Locator (using the unity service locator): This is the easiest way, if your application only

This method is recommended if you have a small number of dependencies and do not want to use the current architecture mode such as dependency injection. It does not need to be initialized or set. You only need to configure your application to use the Enterprise Library, and then call the service locator method to obtain the desired Enterprise Library instance.
2. directly use the unity container. This complicated method allows you to use the current architecture mode for your layers, components, and custom types, such

Lai injection has all the benefits. It only requires a small amount of configuration, but must be referenced to the container in the application.

Other methods you can use:
1. If you decide to use a container different from unity, you can use another service locator.
2. in earlier versions, traditional static facades and factories were used.
Use the Unity Server locator:
The easiest way to create Enterprise Library objects is to create the desired object instances through the service locator, instead of injecting them into your application images. The service locator is actually a container packaging, exposing a series of methods to return appropriate types of instances based on registration and ing. Obtain an instance from the data access module by nameC # </P> <p> database DB = enterpriselibrarycontainer. current. getinstance <database> ("mydatabase"); <br/> dB. executenonquery ("dosomethinginteresting"); </P> <p> Visual Basic <br/> dim dB as database = isiselibrarycontainer. current. getinstance (of Database) ("mydatabase") <br/> dB. executenonquery ("dosomethinginteresting") </P> <p>
Directly access the unity container:
The basic principle is to receive the desired objects instead of receiving them as input. A container is basically a container used for storage type registration and

 

The mechanism for ing and instantiating request types. When your application code's request (or solution) interface, a base class, or by defining a dependency on this

When the type is specific, it is configured to return an appropriate type of instance.
When dependency injection is used, you do not need to specify the request instance. Instead, the dependency injection mechanism determines the desired instance and injects it. Dependency Injection

You must create all dependent objects and pass them to the specified target for relief. The dependency injection container creates an object for you and determines the dependency.
If you accept the method of dependency injection, or you want to manage its cycle through containers, or create and modify the type registration and ing, you can

Create a container and configure it. This means that you can resolve other objects in your application except the Enterprise Library object, such as the user interface.

Components, attempted models, and business objects.
The Enterprise Library provides extension for the enterpriselibrarycoreextension container, which can be easily initialized and filled with the Enterprise Library configuration information

The Unity container. The following example instantiate a new unity container and add extensions to make all Enterprise Library registration and ing available.

C # <br/> var Container = new unitycontainer (). addnewextension <incluiselibrarycoreextension> (); </P> <p> Visual Basic <br/> dim Container = new unitycontainer (). addnewextension (of enterpriselibrarycoreextension )();

 

Example of dependency injection:
Generally, when you directly access the container, it will benefit from dependency injection, and you will use it to solve your own specific class. The container will inject the Enterprise Library object as well as all other objects and dependencies.
The following example shows that the taxcalculator type has two dependencies: predictionmanager and logwriter.C # <br/> public class taxcalculator <br/>{< br/> private exceptionmanager _ exceptionmanager; <br/> private logwriter _ logwriter; </P> <p> Public taxcalculator (exceptionmanager em, logwriter LW) <br/>{< br/> This. _ exceptionmanager = em; <br/> This. _ logwriter = LW; <br/>}</P> <p> Visual Basic <br/> public class taxcalculator </P> <p> private _ exceptionmanager as predictionmanager <br /> private _ logwriter as logwriter </P> <p> Public sub new (EM as predictionmanager, LW as logwriter) <br/> me. _ exceptionmanager = em <br/> me. _ logwriter = LW <br/> end sub </P> <p> end class <br/>
When you have a container reference, you can use it to solve specific objects, such as taxcalculator.
C # <br/> taxcalculator calc = container. resolve <taxcalculator> (); </P> <p> Visual Basic <br/> dim Calc as taxcalculator = container. resolve (of taxcalculator )() 
 
The container automatically fills in to resolve all dependencies in the instance. If the taxcalculator class is defined as another type of dependency. When his

 

When the parent class is parsed, predictionmanager and logwriter automatically create and inject the class.

Non-static instances and instance factories
All application blocks include non-static instances and non-static factories to replace static methods and instance factories earlier than MySQL 5.0. New non-static instances and factory classes

It cannot be instantiated directly.

Example of amorphous and factory:
The following example uses the getinstance method to obtain the exceptionmanager instance from the exception handling application module and call its method.C # <br/> exceptionmanager em = enterpriselibrarycontainer. current. getinstance <exceptionmanager> (); <br/> em. process () => methodthatmightthrow (), "My exception policy"); </P> <p> visual <br/> dim em as predictionmanager = enterpriselibrarycontainer. current. getinstance (of exceptionmanager) (); <br/> em. process (addressof methodthatmightthrow, "My exception policy"); <br/>
The following example uses the getinstance method to obtain the validatorfactory instance from the verification application module, and uses it to create an object to verify the members of the customer.C # <br/> validatorfactory factory = enterpriselibrarycontainer. current. getinstance <validatorfactory> (); <br/> validator <customer> customervalidator = factory. createvalidator <customer> (); </P> <p> customer mycustomer = new customer ("a name that is too long"); <br/> validationresults r = customervalidator. validate (mycustomer); <br/> If (! R. isvalid) <br/>{< br/> throw new invalidoperationexception ("validation error found. "); <br/>}</P> <p> Visual Basic <br/> dim factory as validatorfactory = enterpriselibrarycontainer. current. getinstance (of </P> <p> validatorfactory) () <br/> dim customervalidator as validator (of customer) <br/> = factory. createvalidator (of customer) () </P> <p> dim mycustomer as new customer ("a name that is too long") <br/> dim R as validationresults = customervalidator. validate (mycustomer) <br/> if not R. isvalid then <br/> throw new invalidoperationexception ("validation error found. ") <br/> end if </P> <p>

 

Legacy facades and factories
To maintain forward compatibility
 Initialize and set the current container
The enterpriselibrarycontainer class encapsulates an instance of the Unity container by default, and uses the current attribute and the iservicelocator interface's

Expose reference to it. Calling the getinstance method is the real work of the container. You can configure a dedicated unity container. Configuration is responsible for obtaining configuration information from a specific source and applying it to the container as a series of types of registration and ing. The following example uses the default unity container configuration.

C # <br/> var Container = new unitycontainer (); <br/> var ator = new unitycontainerator ator (container ); <br/> // read the configuration files and set up the container. <br/> enterpriselibrarycontainer. configurecontainer (configurator, configurationsourcefactory. create ()); </P> <p> // The container is now ready to resolve Enterprise Library objects </P> <p> visual <br/> dim Container = new unitycontainer () <br/> dim configurator = new unitycontainerconfigurator (container) <br/> 'read the configuration files and set up the container. <br/> enterpriselibrarycontainer. configurecontainer (configurator, configurationsourcefactory. create () </P> <p> 'the iner is now ready to resolve Enterprise Library objects <br/>
If you do not use the dependency injection style in the application, or if you use shortcuts to initialize and populate containers

 

Details. But in some cases, you want to change the underlying container, such:
1. You have an application that uses dependency injection containers for some things, but there are still some Enterprise Library client code in use, you want to not rewrite

Integrate all things when calling static appearances.
2. Your application uses the static appearance or enterpriselibrarycontainer. Current. getinstance to get the Enterprise Library object and you

You want to use a configuration file of the source rather than the application to change the final configuration.
It is quite easy to change the container. All you need is to implement the iservicelocator interface. However, if you want to load configuration information from the configuration source,

You will benefit from using the iconfigurationsource instance and configuration, as well as the implementation of the iservicelocator interface.
An object that implements the iconfigurationsource Interface contains the configuration information of the Enterprise Library. Several implementations attached to the Enterprise Library can be implemented through the application process

Read the information in the configuration file, independent file, and memory dictionary. It will be used together with the configuration to create a container.
The following code demonstrates how to use the unitycontainerconfigurator configuration to create a container, configure a container, and use servicelocator to package

Container.C # </P> <p> iunitycontainer Container = new unitycontainer (); </P> <p> unitycontainerator ator = new unitycontainerator ator (container ); </P> <p> iconfigurationsource configsource = readconfigsource (); </P> <p> enterpriselibrarycontainer. configurecontainer (configurator, configsource); </P> <p> iservicelocator locator = new unityservicelocator (container); </P> <p> enterpriselibrarycontainer. current = locator; </P> <p> Visual Basic </P> <p> dim container as iunitycontainer = new unitycontainer () </P> <p> dim configurator as unitycontainerconfigurator _ <br/> = new unitycontainerconfigurator (container) </P> <p> dim configsource as iconfigurationsource = readconfigsource () </P> <p> enterpriselibrarycontainer. configurecontainer (configurator, configsource) </P> <p> dim locator as iservicelocator = new unityservicelocator (container) </P> <p> enterpriselibrarycontainer. current = locator </P> <p>

 

If you are using the default container implementation (Unity) of the Enterprise Library, you can use the following code to achieve the same results:

{
DP. Sh. toolbar. Command ('viewsource', this); Return false;
} "Href =" http://blog.csdn.net/sfbirp/archive/2010/05/19/5606055.aspx# "> View plain {
DP. Sh. toolbar. Command ('copytoclipboard', this); Return false;
} "Href =" http://blog.csdn.net/sfbirp/archive/2010/05/19/5606055.aspx# "> copy to clipboard {
DP. Sh. toolbar. Command ('printsource', this); Return false;
} "Href =" http://blog.csdn.net/sfbirp/archive/2010/05/19/5606055.aspx# "> Print {
DP. Sh. toolbar. Command ('about', this); Return false;
} "Href =" http://blog.csdn.net/sfbirp/archive/2010/05/19/5606055.aspx# ">?
  1. C #
  2. Enterpriselibrarycontainer. Current = enterpriselibrarycontainer. createdefacontainer (readconfigsource ());
  3. Visual Basic
  4. Enterpriselibrarycontainer. Current = enterpriselibrarycontainer. createdefacontainer (readconfigsource ());

C # <br/> enterpriselibrarycontainer. current = enterpriselibrarycontainer. createdefacontainer iner (readconfigsource (); </P> <p> Visual Basic <br/> enterpriselibrarycontainer. current = enterpriselibrarycontainer. createdefacontainer iner (readconfigsource (); </P> <p>

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.