/src/LinFu.IoC/FactoryStorageExtensions.cs

http://github.com/philiplaureano/LinFu · C# · 83 lines · 37 code · 5 blank · 41 comment · 1 complexity · 12565e1ca9784c6f2217fcc293413735 MD5 · raw file

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using LinFu.IoC.Interfaces;
  5. namespace LinFu.IoC
  6. {
  7. /// <summary>
  8. /// An extension class that adds a few helper methods to the
  9. /// <see cref="IFactoryStorage" /> interface.
  10. /// </summary>
  11. public static class FactoryStorageExtensions
  12. {
  13. /// <summary>
  14. /// Adds a factory to the current <see cref="IFactoryStorage" /> instance.
  15. /// </summary>
  16. /// <param name="storage">The <see cref="IFactoryStorage" /> object that will store the target factory.</param>
  17. /// <param name="serviceName">The name that will be associated with the target factory.</param>
  18. /// <param name="serviceType">The service type that the factory will be able to create.</param>
  19. /// <param name="additionalParameterTypes">The list of additional parameters that this factory type will support.</param>
  20. /// <param name="factory">The <see cref="IFactory" /> instance that will create the object instance.</param>
  21. public static void AddFactory(this IFactoryStorage storage, string serviceName,
  22. Type serviceType, IEnumerable<Type> additionalParameterTypes, IFactory factory)
  23. {
  24. var info = new ServiceInfo(serviceName, serviceType, additionalParameterTypes);
  25. storage.AddFactory(info, factory);
  26. }
  27. /// <summary>
  28. /// Determines which factories should be used
  29. /// for a particular service request.
  30. /// </summary>
  31. /// <param name="storage">The <see cref="IFactoryStorage" /> object that holds the target factory.</param>
  32. /// <param name="serviceName">The name that will be associated with the target factory.</param>
  33. /// <param name="serviceType">The service type that the factory will be able to create.</param>
  34. /// <param name="additionalParameters">
  35. /// The list of additional parameter values that this factory type will use to
  36. /// instantiate the service.
  37. /// </param>
  38. /// <returns>A factory instance.</returns>
  39. public static IFactory GetFactory(this IFactoryStorage storage, string serviceName, Type serviceType,
  40. IEnumerable<object> additionalParameters)
  41. {
  42. var additionalParameterTypes = from arg in additionalParameters
  43. let argType = arg != null ? arg.GetType() : typeof(object)
  44. select argType;
  45. var info = new ServiceInfo(serviceName, serviceType, additionalParameterTypes);
  46. return storage.GetFactory(info);
  47. }
  48. /// <summary>
  49. /// Determines which factories should be used
  50. /// for a particular service request.
  51. /// </summary>
  52. /// <param name="storage">The <see cref="IFactoryStorage" /> object that holds the target factory.</param>
  53. /// <param name="serviceName">The name that will be associated with the target factory.</param>
  54. /// <param name="serviceType">The service type that the factory will be able to create.</param>
  55. /// <param name="additionalParameterTypes">The list of additional parameters that this factory type will support.</param>
  56. /// <returns>A factory instance.</returns>
  57. public static IFactory GetFactory(this IFactoryStorage storage, string serviceName, Type serviceType,
  58. IEnumerable<Type> additionalParameterTypes)
  59. {
  60. var info = new ServiceInfo(serviceName, serviceType, additionalParameterTypes);
  61. return storage.GetFactory(info);
  62. }
  63. /// <summary>
  64. /// Determines whether or not a factory exists in storage.
  65. /// </summary>
  66. /// <param name="storage">The <see cref="IFactoryStorage" /> object that holds the target factory.</param>
  67. /// <param name="serviceName">The name that will be associated with the target factory.</param>
  68. /// <param name="serviceType">The service type that the factory will be able to create.</param>
  69. /// <param name="additionalParameterTypes">The list of additional parameters that this factory type will support.</param>
  70. /// <returns>Returns <c>true</c> if the factory exists; otherwise, it will return <c>false</c>.</returns>
  71. public static bool ContainsFactory(this IFactoryStorage storage, string serviceName, Type serviceType,
  72. IEnumerable<Type> additionalParameterTypes)
  73. {
  74. var info = new ServiceInfo(serviceName, serviceType, additionalParameterTypes);
  75. return storage.ContainsFactory(info);
  76. }
  77. }
  78. }