Skip to content

Latest commit

 

History

History
81 lines (54 loc) · 3.27 KB

prop-method-injection.rst

File metadata and controls

81 lines (54 loc) · 3.27 KB

Property and Method Injection

While constructor parameter injection is the preferred method of passing values to a component being constructed, you can also use property or method injection to provide values.

Property injection uses writeable properties rather than constructor parameters to perform injection. Method injection sets dependencies by calling a method.

Property Injection

If the component is a lambda expression component <register-registration-lambda-expression-components>, use an object initializer:

csharp

builder.Register(c => new A { B = c.Resolve<B>() });

To support circular dependencies <../advanced/circular-dependencies>, use an activated event handler <../lifetime/events>:

csharp

builder.Register(c => new A()).OnActivated(e => e.Instance.B = e.Context.Resolve<B>());

If the component is a reflection component <register-registration-reflection-components>, use the PropertiesAutowired() modifier to inject properties:

csharp

// Default behavior: inject all properties that are public and writable. builder.RegisterType<A>().PropertiesAutowired();

// Provide a delegate property selector to be more granular. This example // shows injecting all properties where the property type starts with // 'I' - one way you might "only inject interface properties." The delegate // gets the PropertyInfo describing the property to be injected and the // instance getting injected. builder.RegisterType<B>() .PropertiesAutowired( (propInfo, instance) => propInfo.PropertyType.Name.StartsWith("I"));

// Even more fancy, you can provide your own implementation of // IPropertySelector with as much functionality as you want. Don't // forget this will run on every associated resolution, so performance // is important! builder.RegisterType<C>().PropertiesAutowired(new MyCustomPropSelector());

If you have one specific property and value to wire up, you can use the WithProperty() modifier:

csharp

builder.RegisterType<A>().WithProperty("PropertyName", propertyValue);

You can also populate just the properties on an object. Do this using the InjectUnsetProperties extension on a lifetime scope, which will resolve and populate properties that are public, writable, and not yet set (null):

csharp

lifetimeScope.InjectUnsetProperties(myObject);

Method Injection

The simplest way to call a method to set a value on a component is to use a lambda expression component <register-registration-lambda-expression-components> and handle the method call right in the activator:

csharp

builder.Register(c => {

var result = new MyObjectType(); var dep = c.Resolve<TheDependency>(); result.SetTheDependency(dep); return result;

});

If you can't use a registration lambda, you can add an activating event handler <../lifetime/events>:

csharp

builder

.RegisterType<MyObjectType>() .OnActivating(e => { var dep = e.Context.Resolve<TheDependency>(); e.Instance.SetTheDependency(dep); });