Microsoft enterprise database 4.1 Study Notes (26) Unity dependency injection module 3

Source: Internet
Author: User

Array supported during runtime configuration

  

Class Foot {
Public Foot (IBar [] bars)
{}
}

 

 

Container. Configure <InjectedMembers> (). ConfigureInjectionFor <Foot> (new InjectionConstructor (new ResolvedArrayParameter <IBar> ());

 

 

 

 

Container. configure <InjectedMembers> (). configureInjectionFor <Foot> (new InjectionConstructor (new ResolvedArrayParameter <IBar> (typeof (IBar), new ResolvedParameter <IBar> ("some named bar"), new BarImpl ()))

 

Configure containers during running

1. Configure constructors, attributes, and method injection.

You can directly create and load configurations during running to configure the Unity container to implement the injection function. In addition to adding attributes and using configuration files, you can also use this method.

Use the InjectionConstructor, InjectionProperty, and InjectionMethod classes to combine the Configure and ConfigureInjectionFor methods to specify the dependency injection parameters for the container.

  

 

IUnityContainer container = new UnityContainer (). RegisterType <AType> (new partition (); AType aType = container. Resolve <AType> (); Assert. IsTrue (aType. DefaultConstructorCalled );

 

The following code uses the Configuration class to configure constructor injection, property injection, and method injection.

 

IUnityContainer myContainer = new UnityContainer (); myContainer. Configure <InjectedMembers> (). ConfigureInjectionFor <MyObject> (new InjectionConstructor (12, "Hello Unity! "), New InjectionProperty (" MyProperty "), new InjectionProperty (" MyStringProperty "," SomeText "), new InjectionMethod (" InitializeMe ", 42.0, new ResolvedParameter (typeof (ILogger ), "SpecialLogger ")));

 

This kind of API dynamic configuration injection provides sufficient flexibility and retains a simple and intuitive way of use.

This configuration injection API is based on the subclass of InjectionParameterValue. Of course, other types of objects can also be provided when injection is created. According to the following rules, the provided object will be translated into the InjectionParameterValue type.

1) if the object is a subclass instance of InjectionParameterValue, the injection system will use this object.

2) if the object is a Type instance, the injection system will create a ResolvedParameter as the Type returned after the injection.

3) If it is another object, the configuration API will create an InjectionParameterValue instance, and the container will generate an attribute injection.

  

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.