Property Injection

Property injection is useful to highlight the dependencies of your class through it properties. This is done by specifying which properties are dependencies that must be injected through Needle.
Property Injection takes place right after Constructor Injection. This means that if you have a dependency which must be used in your class constructor, you cannot use Property Injection and you must use Constructor Injection.
To get Needle to inject a dependency in a property's value, the property must be decorated with the Dependency attribute.
internal class MockWithPropertyDependency
{
	...

        [Dependency]
        public MockWithoutDependencies Dependency { get; set; }
}

Named Dependencies

There might be some scenarios in which a property dependency has been registered with an Id in the container. To get Needle to inject the correct instance based on that Id, you need to specify the Id for the Dependency attribute, as shown in the following code.
internal class MockWithNamedPropertyDependency
{
    ...

    [Dependency("DependencyId")]
    public MockWithoutDependencies Dependency { get; set; }
}

Related Topics

Last edited Jan 21, 2011 at 1:01 AM by dschenkelman, version 1

Comments

No comments yet.