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

http://github.com/philiplaureano/LinFu · C# · 74 lines · 32 code · 9 blank · 33 comment · 0 complexity · 9c47f24934a8bcad5327457bf31f5415 MD5 · raw file

  1. using System;
  2. using LinFu.IoC.Configuration.Interfaces;
  3. using LinFu.IoC.Interfaces;
  4. namespace LinFu.IoC.Configuration
  5. {
  6. /// <summary>
  7. /// Represents a fluent class that creates
  8. /// a method that initializes a <typeparamref name="TService" />
  9. /// instance.
  10. /// </summary>
  11. /// <typeparam name="TService">The service type being instantiated.</typeparam>
  12. internal class PropertyInjectionLambda<TService> : IPropertyInjectionLambda<TService>
  13. {
  14. private readonly ActionContext<TService> _context;
  15. /// <summary>
  16. /// Initializes the class with the <paramref name="context" />.
  17. /// </summary>
  18. /// <param name="context">The context that will be associated with the target container.</param>
  19. internal PropertyInjectionLambda(ActionContext<TService> context)
  20. {
  21. _context = context;
  22. }
  23. /// <summary>
  24. /// Initializes service instances with the given
  25. /// <paramref name="action" />.
  26. /// </summary>
  27. /// <param name="action">
  28. /// An <see cref="Action{TService}" /> delegate that will be used to initialize newly created
  29. /// services.
  30. /// </param>
  31. public void With(Action<TService> action)
  32. {
  33. Action<IServiceContainer, TService> adapter =
  34. (container, service) => action(service);
  35. _context.Action = adapter;
  36. AddPostProcessor(_context);
  37. }
  38. /// <summary>
  39. /// Uses an action delegate to initialize a given service using
  40. /// the given <see cref="IServiceContainer" /> and <typeparamref name="TService" />
  41. /// instances.
  42. /// </summary>
  43. /// <param name="action">
  44. /// An <see cref="Func{IServiceContainer, TService}" /> delegate that will be used to initialize newly
  45. /// created services.
  46. /// </param>
  47. public void With(Action<IServiceContainer, TService> action)
  48. {
  49. _context.Action = action;
  50. AddPostProcessor(_context);
  51. }
  52. /// <summary>
  53. /// Attaches the action associated with the <see cref="ActionContext{TService}" />
  54. /// instance to the target container embedded within the <see cref="ActionContext{TService}" />
  55. /// class itself.
  56. /// </summary>
  57. /// <param name="context">The context that will be associated with the target container.</param>
  58. private static void AddPostProcessor(ActionContext<TService> context)
  59. {
  60. var targetContainer = context.Container;
  61. var postProcessor = new ActionPostProcessor<TService>(context);
  62. targetContainer.PostProcessors.Add(postProcessor);
  63. }
  64. }
  65. }