PageRenderTime 122ms CodeModel.GetById 60ms app.highlight 3ms RepoModel.GetById 57ms app.codeStats 0ms

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

http://github.com/philiplaureano/LinFu
C# | 76 lines | 35 code | 9 blank | 32 comment | 0 complexity | a499c558c3d84d507c0733fc194cef1b MD5 | raw file
 1using System;
 2using LinFu.IoC.Factories;
 3using LinFu.IoC.Interfaces;
 4
 5namespace LinFu.IoC.Configuration
 6{
 7    /// <summary>
 8    ///     Represents a fluent class that allows
 9    ///     users to create specific types of factories.
10    /// </summary>
11    /// <typeparam name="TService">The type of service being created.</typeparam>
12    internal class GenerateFactory<TService> : IGenerateFactory<TService>
13    {
14        private readonly InjectionContext<TService> _context;
15
16        /// <summary>
17        ///     Instantiates the class using the given
18        ///     <paramref name="context" />.
19        /// </summary>
20        /// <param name="context">
21        ///     The <see cref="InjectionContext{T}" /> instance
22        ///     which will contain the information necessary to build a fluent command.
23        /// </param>
24        internal GenerateFactory(InjectionContext<TService> context)
25        {
26            _context = context;
27        }
28
29
30        /// <summary>
31        ///     Creates a singleton factory.
32        /// </summary>
33        /// <seealso cref="SingletonFactory{T}" />
34        public void AsSingleton()
35        {
36            AddFactory(adapter => new SingletonFactory<TService>(adapter));
37        }
38
39        /// <summary>
40        ///     Creates a once per thread factory.
41        /// </summary>
42        /// <seealso cref="OncePerThreadFactory{T}" />
43        public void OncePerThread()
44        {
45            AddFactory(adapter => new OncePerThreadFactory<TService>(adapter));
46        }
47
48        /// <summary>
49        ///     Creates a once per request factory.
50        /// </summary>
51        /// <seealso cref="OncePerRequestFactory{T}" />
52        public void OncePerRequest()
53        {
54            AddFactory(adapter => new OncePerRequestFactory<TService>(adapter));
55        }
56
57
58        /// <summary>
59        ///     Adds a factory to the container by using the
60        ///     <paramref name="createFactory" /> delegate to
61        ///     create the actual <see cref="IFactory{T}" />
62        ///     instance.
63        /// </summary>
64        /// <param name="createFactory">The delegate that will create the actual factory instance.</param>
65        private void AddFactory(Func<Func<IFactoryRequest, TService>,
66            IFactory<TService>> createFactory)
67        {
68            var container = _context.Container;
69            var adapter = _context.FactoryMethod.CreateAdapter();
70            var factory = createFactory(adapter);
71            var serviceName = _context.ServiceName;
72
73            container.AddFactory(serviceName, factory);
74        }
75    }
76}