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

/src/LinFu.IoC/Configuration/FluentInterfaces/PropertyInjectionLambda.cs

http://github.com/philiplaureano/LinFu
C# | 74 lines | 32 code | 9 blank | 33 comment | 0 complexity | 9c47f24934a8bcad5327457bf31f5415 MD5 | raw file
 1using System;
 2using LinFu.IoC.Configuration.Interfaces;
 3using LinFu.IoC.Interfaces;
 4
 5namespace LinFu.IoC.Configuration
 6{
 7    /// <summary>
 8    ///     Represents a fluent class that creates
 9    ///     a method that initializes a <typeparamref name="TService" />
10    ///     instance.
11    /// </summary>
12    /// <typeparam name="TService">The service type being instantiated.</typeparam>
13    internal class PropertyInjectionLambda<TService> : IPropertyInjectionLambda<TService>
14    {
15        private readonly ActionContext<TService> _context;
16
17        /// <summary>
18        ///     Initializes the class with the <paramref name="context" />.
19        /// </summary>
20        /// <param name="context">The context that will be associated with the target container.</param>
21        internal PropertyInjectionLambda(ActionContext<TService> context)
22        {
23            _context = context;
24        }
25
26
27        /// <summary>
28        ///     Initializes service instances with the given
29        ///     <paramref name="action" />.
30        /// </summary>
31        /// <param name="action">
32        ///     An <see cref="Action{TService}" /> delegate that will be used to initialize newly created
33        ///     services.
34        /// </param>
35        public void With(Action<TService> action)
36        {
37            Action<IServiceContainer, TService> adapter =
38                (container, service) => action(service);
39
40            _context.Action = adapter;
41
42            AddPostProcessor(_context);
43        }
44
45        /// <summary>
46        ///     Uses an action delegate to initialize a given service using
47        ///     the given <see cref="IServiceContainer" /> and <typeparamref name="TService" />
48        ///     instances.
49        /// </summary>
50        /// <param name="action">
51        ///     An <see cref="Func{IServiceContainer, TService}" /> delegate that will be used to initialize newly
52        ///     created services.
53        /// </param>
54        public void With(Action<IServiceContainer, TService> action)
55        {
56            _context.Action = action;
57            AddPostProcessor(_context);
58        }
59
60
61        /// <summary>
62        ///     Attaches the action associated with the <see cref="ActionContext{TService}" />
63        ///     instance to the target container embedded within the <see cref="ActionContext{TService}" />
64        ///     class itself.
65        /// </summary>
66        /// <param name="context">The context that will be associated with the target container.</param>
67        private static void AddPostProcessor(ActionContext<TService> context)
68        {
69            var targetContainer = context.Container;
70            var postProcessor = new ActionPostProcessor<TService>(context);
71            targetContainer.PostProcessors.Add(postProcessor);
72        }
73    }
74}