PageRenderTime 109ms CodeModel.GetById 102ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/src/LinFu.IoC/DefaultGetServiceBehavior.cs

http://github.com/philiplaureano/LinFu
C# | 89 lines | 52 code | 10 blank | 27 comment | 4 complexity | 00887822bdf29ec1b82ff4b2944d376c MD5 | raw file
 1using LinFu.IoC.Interfaces;
 2
 3namespace LinFu.IoC
 4{
 5    /// <summary>
 6    ///     Represents the default implementation for the <see cref="IGetService" /> interface.
 7    /// </summary>
 8    public class DefaultGetServiceBehavior :
 9        IGetService
10    {
11        private readonly IServiceContainer _container;
12        private readonly ICreateInstance _creator;
13        private readonly IPostProcessor _postProcessor;
14        private readonly IPreProcessor _preProcessor;
15
16        /// <summary>
17        ///     Initializes the class with the given <paramref name="container" /> instance.
18        /// </summary>
19        /// <param name="container">The target service container.</param>
20        public DefaultGetServiceBehavior(IServiceContainer container)
21        {
22            _container = container;
23            _creator = new DefaultCreator();
24            _preProcessor = new CompositePreProcessor(container.PreProcessors);
25            _postProcessor = new CompositePostProcessor(container.PostProcessors);
26        }
27
28        /// <summary>
29        ///     Initializes the class with the given <paramref name="container" /> instance.
30        /// </summary>
31        /// <param name="container">The target service container.</param>
32        /// <param name="creator">The <see cref="ICreateInstance" /> instance responsible for instantiating service types.</param>
33        /// <param name="preProcessor">The <see cref="IPreProcessor" /> that will allow users to intercept a given service request.</param>
34        /// <param name="postProcessor">
35        ///     The <see cref="IPostProcessor" /> instance that will handle the results of a given service
36        ///     request.
37        /// </param>
38        public DefaultGetServiceBehavior(IServiceContainer container, ICreateInstance creator,
39            IPreProcessor preProcessor, IPostProcessor postProcessor)
40        {
41            _container = container;
42            _creator = creator;
43            _preProcessor = preProcessor;
44            _postProcessor = postProcessor;
45        }
46
47
48        /// <summary>
49        ///     Instantiates the service described by the <paramref name="serviceRequest" />.
50        /// </summary>
51        /// <param name="serviceRequest">
52        ///     The <see cref="IServiceRequest" /> that describes the service that needs to be
53        ///     instantiated.
54        /// </param>
55        /// <returns>A valid object reference if the service can be found; otherwise, it will return <c>null</c>.</returns>
56        public virtual object GetService(IServiceRequest serviceRequest)
57        {
58            // Allow users to intercept the instantiation process
59            if (_preProcessor != null)
60                _preProcessor.Preprocess(serviceRequest);
61
62            var factoryRequest = new FactoryRequest
63            {
64                ServiceType = serviceRequest.ServiceType,
65                ServiceName = serviceRequest.ServiceName,
66                Arguments = serviceRequest.ActualArguments,
67                Container = _container
68            };
69
70            var instance = _creator.CreateFrom(factoryRequest, serviceRequest.ActualFactory);
71
72            // Postprocess the results
73            var result = new ServiceRequestResult
74            {
75                ServiceName = serviceRequest.ServiceName,
76                ActualResult = instance,
77                Container = _container,
78                OriginalResult = instance,
79                ServiceType = serviceRequest.ServiceType,
80                AdditionalArguments = serviceRequest.ActualArguments
81            };
82
83            if (_postProcessor != null)
84                _postProcessor.PostProcess(result);
85
86            return result.ActualResult ?? result.OriginalResult;
87        }
88    }
89}