/src/LinFu.IoC/Configuration/Extensions/FluentExtensions.cs

http://github.com/philiplaureano/LinFu · C# · 102 lines · 48 code · 9 blank · 45 comment · 0 complexity · 834f5e9817c8d81aa723823bc0b02df5 MD5 · raw file

  1. using System;
  2. using LinFu.IoC.Configuration;
  3. using LinFu.IoC.Configuration.Interfaces;
  4. using LinFu.IoC.Interfaces;
  5. namespace LinFu.IoC
  6. {
  7. /// <summary>
  8. /// A class that adds fluent syntax support to <see cref="IServiceContainer" />
  9. /// instances.
  10. /// </summary>
  11. public static class FluentExtensions
  12. {
  13. /// <summary>
  14. /// Injects a <typeparamref name="TService" /> type
  15. /// into a <paramref name="container" /> using the
  16. /// given <paramref name="serviceName" />
  17. /// </summary>
  18. /// <typeparam name="TService">The type of service to inject.</typeparam>
  19. /// <param name="container">The container that will hold the actual service service instance.</param>
  20. /// <param name="serviceName">The name of the service to create.</param>
  21. /// <returns>A non-null <see cref="IUsingLambda{TService}" /> instance.</returns>
  22. public static IUsingLambda<TService> Inject<TService>(this IServiceContainer container, string serviceName)
  23. {
  24. var context = new InjectionContext<TService>
  25. {
  26. ServiceName = serviceName,
  27. Container = container
  28. };
  29. return new UsingLambda<TService>(context);
  30. }
  31. /// <summary>
  32. /// Injects a <typeparamref name="TService" /> type
  33. /// into a <paramref name="container" />.
  34. /// </summary>
  35. /// <typeparam name="TService">The type of service to inject.</typeparam>
  36. /// <param name="container">The container that will hold the actual service service instance.</param>
  37. /// <returns>A non-null <see cref="IUsingLambda{TService}" /> instance.</returns>
  38. public static IUsingLambda<TService> Inject<TService>(this IServiceContainer container)
  39. {
  40. var context = new InjectionContext<TService>
  41. {
  42. ServiceName = null,
  43. Container = container
  44. };
  45. return new UsingLambda<TService>(context);
  46. }
  47. /// <summary>
  48. /// Initializes services that match the given <typeparamref name="TService" /> type.
  49. /// </summary>
  50. /// <typeparam name="TService">The service type to initialize.</typeparam>
  51. /// <param name="container">The container that will create the service itself.</param>
  52. /// <returns>A <see cref="IPropertyInjectionLambda{T}" /> instance. This cannot be <c>null</c>.</returns>
  53. public static IPropertyInjectionLambda<TService> Initialize<TService>(this IServiceContainer container)
  54. {
  55. return container.Initialize<TService>(null);
  56. }
  57. /// <summary>
  58. /// Initializes services that match the given <paramref name="serviceName" /> and <typeparamref name="TService" />
  59. /// type.
  60. /// </summary>
  61. /// <typeparam name="TService">The service type to initialize.</typeparam>
  62. /// <param name="container">The container that will create the service itself.</param>
  63. /// <param name="serviceName">The name of the service to initialize.</param>
  64. /// <returns>A <see cref="IPropertyInjectionLambda{T}" /> instance. This cannot be <c>null</c>.</returns>
  65. public static IPropertyInjectionLambda<TService> Initialize<TService>(this IServiceContainer container,
  66. string serviceName)
  67. {
  68. var context = new ActionContext<TService>
  69. {
  70. ServiceName = serviceName,
  71. Container = container
  72. };
  73. return new PropertyInjectionLambda<TService>(context);
  74. }
  75. /// <summary>
  76. /// Converts a <see cref="Func{Type, IServiceContainer, TArgs, TService}" />
  77. /// lambda into an equivalent <see cref="Func{Type, IContainer, TArgs, TService}" />
  78. /// instance.
  79. /// </summary>
  80. /// <typeparam name="TService">The type of service to create.</typeparam>
  81. /// <param name="func">The lambda function to be converted.</param>
  82. /// <returns>
  83. /// The equivalent <see cref="Func{IFactoryRequest, TService}" />
  84. /// that delegates its calls back to the <paramref name="func" /> lambda function.
  85. /// </returns>
  86. internal static Func<IFactoryRequest, TService> CreateAdapter<TService>(
  87. this Func<IFactoryRequest, TService> func)
  88. {
  89. var adapter = func;
  90. return adapter;
  91. }
  92. }
  93. }