PageRenderTime 142ms CodeModel.GetById 71ms app.highlight 2ms RepoModel.GetById 68ms app.codeStats 0ms

/src/LinFu.IoC/Factories/FunctorFactoryOfT.cs

http://github.com/philiplaureano/LinFu
C# | 48 lines | 25 code | 7 blank | 16 comment | 0 complexity | 613c5cabf064977901a99800e03b5040 MD5 | raw file
 1using System;
 2using LinFu.IoC.Interfaces;
 3
 4namespace LinFu.IoC.Factories
 5{
 6    /// <summary>
 7    ///     A class that converts a delegate into an <see cref="IFactory" /> instance.
 8    /// </summary>
 9    public class FunctorFactory<T> : IFactory<T>, IFactory
10    {
11        private readonly Func<IFactoryRequest, T> _factoryMethod;
12
13        /// <summary>
14        ///     Initializes the class with the given <paramref name="factoryMethod" />.
15        /// </summary>
16        /// <param name="factoryMethod">The delegate that will be used to instantiate a type.</param>
17        public FunctorFactory(Func<IFactoryRequest, object> factoryMethod)
18        {
19            _factoryMethod = request => (T) factoryMethod(request);
20        }
21
22        /// <summary>
23        ///     Initializes the class with the given <paramref name="factoryMethod" />.
24        /// </summary>
25        /// <param name="factoryMethod">The delegate that will be used to instantiate a type.</param>
26        public FunctorFactory(Func<IFactoryRequest, T> factoryMethod)
27        {
28            _factoryMethod = factoryMethod;
29        }
30
31
32        /// <summary>
33        ///     Instantiates an object reference using the given factory method.
34        /// </summary>
35        /// <param name="request">The <see cref="IFactoryRequest" /> instance that describes the requested service.</param>
36        /// <returns>A non-null object reference that represents the service type.</returns>
37        public object CreateInstance(IFactoryRequest request)
38        {
39            return _factoryMethod(request);
40        }
41
42
43        T IFactory<T>.CreateInstance(IFactoryRequest request)
44        {
45            return _factoryMethod(request);
46        }
47    }
48}