AUTOFAC-Attribute Injection

Source: Internet
Author: User

Property injection differs from passing in parameters by constructor. This is done by injecting, and assigning a value to the property before the resource is freed after the class is created.

Here, I'm going to re-get some classes to illustrate this article.

 Public classclassa{Private ReadOnlyClassB B;  PublicClassA (ClassB b) { This. B =b; }     Public voidShow () {Console.WriteLine ("I am ClassA ' s instance!"); }} Public classclassb{ PublicClassA A {Get;Set; }  Public voidShow () {Console.WriteLine ("I am ClassB ' s instance!"); }} Public classclassc{
   Public string Name { get; set; }
PublicCLASSD D {Get;Set; } Public voidShow () {Console.WriteLine ("I am ClassC ' s instance!"+ Name); }} Public classclassd{ Public voidShow () {Console.WriteLine ("I am CLASSD ' s instance!"); }}

1. General methods

var New  New ClassC {D = n.resolve<classd>(), Name = "Sniper"}), builder. Registertype<ClassD>(); var container = Builder. Build (); var c = Container. Resolve<classc>(); C.show (); C.d.show ();

This method can not only inject properties, but also assign values to fields

2. The way of reflection

var New Containerbuilder (); builder. Registertype<ClassD>(); var s = Builder. Registertype<classc>().  Propertiesautowired(); var container = Builder. Build (); var c = Container. Resolve<classc>(); C.show (); C.d.show ();

It is important to note that CLASSD must also be registered because of the CLASSD format attributes in CLASSC. Why is it? Take a look at the source

In the Propertiesautowired () method, the main method is called. Properties are obtained by reflection, and the value of the property is also obtained by resolve.

Note:

Looking closely at ClassA and CLASSB, it can be found that they are cyclic dependent, then if I want to get classb, how to do? Try the first method, you will find, I went, error.

Try the second method, or an error. What about that?

var New Containerbuilder (); builder. Registertype<ClassB>().  Propertiesautowired(propertywiringoptions.  Allowcirculardependencies). singleinstance (); builder. Register (n=new ClassA (n.resolve<classb>())); var container = Builder. Build (); var b = Container. Resolve<classb>(); B.show (); B.a.show ();

or through the way of reflection, but to note that the incoming parameters and singleinstance, without adding, will be an error.

3. By name

var New Containerbuilder (); var s = Builder. Registertype<classc> (). Withproperty ("D"new  CLASSD ()); var container = Builder. Build (); var c = Container. Resolve<classc>(); C.show (); C.d.show ();

With the property name, give him a direct new instance.

4. onactivating/onactivated mode

The execution time of this method is that after the constructor is created, the resource is freed, so it can also be implemented during this period

var New Containerbuilder (); builder. Registertype<ClassC> (). Onactivating (e = E.INSTANCE.D = e.context.resolve<classd>()); builder. Registertype<ClassD>(); var container = Builder. Build (); var c = Container. Resolve<classc>(); C.show (); C.d.show ();

The onactivating here can also be exchanged for OnActivated. The realization of the scene here, can get the same results.

Reference:

AUTOFAC Summary of Usage methods

AUTOFAC components, services, automatic Assembly "second article"

AUTOFAC Documentation

Autofac-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.