PageRenderTime 51ms CodeModel.GetById 20ms app.highlight 2ms RepoModel.GetById 28ms app.codeStats 0ms

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

http://github.com/philiplaureano/LinFu
C# | 45 lines | 25 code | 8 blank | 12 comment | 6 complexity | 5104432ebac2b574e679128bd67d6312 MD5 | raw file
 1using LinFu.IoC.Interfaces;
 2
 3namespace LinFu.IoC.Configuration
 4{
 5    /// <summary>
 6    ///     Represents a postprocessor that will execute
 7    ///     the action associated with the given <see cref="ActionContext{TService}" />
 8    ///     instance every time the target container returns a
 9    ///     service with particular service name and service type.
10    /// </summary>
11    /// <typeparam name="TService"></typeparam>
12    internal class ActionPostProcessor<TService> : IPostProcessor
13    {
14        private readonly ActionContext<TService> _context;
15
16        internal ActionPostProcessor(ActionContext<TService> context)
17        {
18            _context = context;
19        }
20
21
22        public void PostProcess(IServiceRequestResult result)
23        {
24            // Ignore any null results
25            if (result.ActualResult == null)
26                return;
27
28            // The service type must match the current service
29            if (result.ServiceType != typeof(TService))
30                return;
31
32            // The service names must be equal
33            if (result.ServiceName != _context.ServiceName)
34                return;
35
36            var service = (TService) result.ActualResult;
37            var container = result.Container;
38            var action = _context.Action;
39
40            // Execute the action associated with the
41            // context
42            action(container, service);
43        }
44    }
45}