PageRenderTime 6ms CodeModel.GetById 1ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/src/LinFu.IoC/Configuration/Injectors/AutoPropertyInjector.cs

http://github.com/philiplaureano/LinFu
C# | 47 lines | 23 code | 4 blank | 20 comment | 4 complexity | 43dca02897d52880610b6bb7e4b10f5d MD5 | raw file
 1using System.Linq;
 2using System.Reflection;
 3using LinFu.IoC.Configuration.Interfaces;
 4using LinFu.IoC.Interfaces;
 5
 6namespace LinFu.IoC.Configuration
 7{
 8    /// <summary>
 9    ///     A class that automatically injects property dependencies into
10    ///     service instances.
11    /// </summary>
12    public class AutoPropertyInjector : AutoMemberInjector<PropertyInfo>
13    {
14        /// <summary>
15        ///     Injects services from the <paramref name="container" /> into the target <see cref="PropertyInfo" /> instance.
16        /// </summary>
17        /// <param name="target">The target object.</param>
18        /// <param name="property">The <see cref="PropertyInfo" /> instance that will store the service instance.</param>
19        /// <param name="resolver">
20        ///     The <see cref="IArgumentResolver" /> that will determine which arguments will be assigned to the
21        ///     target member.
22        /// </param>
23        /// <param name="additionalArguments">
24        ///     The additional arguments that were passed to the <see cref="IServiceRequestResult" />
25        ///     during the instantiation process.
26        /// </param>
27        /// <param name="container">The container that will provide the service instances.</param>
28        protected override void Inject(object target, PropertyInfo property,
29            IArgumentResolver resolver, IServiceContainer container,
30            object[] additionalArguments)
31        {
32            var setter = container.GetService<IPropertySetter>();
33            if (setter == null)
34                return;
35
36            // Determine the property value
37            var results = resolver.ResolveFrom(new[] {new NamedType(property)}, container);
38            var propertyValue = results.FirstOrDefault();
39
40            if (propertyValue == null)
41                return;
42
43            // Call the setter against the target property
44            setter.Set(target, property, propertyValue);
45        }
46    }
47}