/src/LinFu.IoC/Factories/FunctorFactory.cs
C# | 33 lines | 17 code | 4 blank | 12 comment | 0 complexity | 5141c246e48ead824f35e2f9b4f27873 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 : IFactory 10 { 11 private readonly Func<IFactoryRequest, object> _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 = factoryMethod; 20 } 21 22 23 /// <summary> 24 /// Instantiates an object reference using the given factory method. 25 /// </summary> 26 /// <param name="request">The <see cref="IFactoryRequest" /> instance that describes the requested service.</param> 27 /// <returns>A non-null object reference that represents the service type.</returns> 28 public object CreateInstance(IFactoryRequest request) 29 { 30 return _factoryMethod(request); 31 } 32 } 33}