autofac--. Registration component--3 properties and method injection

Source: Internet
Author: User

Official Document: Http://docs.autofac.org/en/latest/register/prop-method-injection.html
AUTOFAC properties and Method injection
Although constructor parameter injection is the preferred method for passing values to a component, you can also use a property or method injection to provide a value.
Property injection is injected using a writable property instead of a constructor parameter. Method injection sets the dependency relationship by calling a method.
1. Attribute Injection
If it is a lambda expression component , use an object to initialize it:
New A {B = c.resolve<b> ()});

To support circular dependencies, use an activated event handler:

New A ()). OnActivated (e = e.instance.b = E.context.resolve<b> ());
If it is a reflection component , use propertiesautowired() to modify the registration property:
Builder. Registertype<a> (). Propertiesautowired ();
If you have a specific attribute and value that needs to be connected, you can use withproperty () to modify:
Builder. Registertype<a> (). Withproperty ("PropertyName", PropertyValue);
2. Method Injection
The simplest way to call a method to set the value of a component is to use a lambda expression component and the correct invocation to activate the processing method.
Builder. Register (c + =  {varnew  myobjecttype ()  ; var dep = c.resolve<thedependency>();  Result. Setthedependency (DEP);   return result;});
If you cannot register with a lambda expression, you can add an activation event handler (activating eventhandler)
Builder  . Register<MyObjectType>()  = {    var dep = e.context.resolve< Thedependency>();    E.instance.setthedependency (DEP);  });

autofac--. Registration component--3 properties and method 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.