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

http://github.com/philiplaureano/LinFu · C# · 45 lines · 25 code · 8 blank · 12 comment · 6 complexity · 5104432ebac2b574e679128bd67d6312 MD5 · raw file

  1. using LinFu.IoC.Interfaces;
  2. namespace LinFu.IoC.Configuration
  3. {
  4. /// <summary>
  5. /// Represents a postprocessor that will execute
  6. /// the action associated with the given <see cref="ActionContext{TService}" />
  7. /// instance every time the target container returns a
  8. /// service with particular service name and service type.
  9. /// </summary>
  10. /// <typeparam name="TService"></typeparam>
  11. internal class ActionPostProcessor<TService> : IPostProcessor
  12. {
  13. private readonly ActionContext<TService> _context;
  14. internal ActionPostProcessor(ActionContext<TService> context)
  15. {
  16. _context = context;
  17. }
  18. public void PostProcess(IServiceRequestResult result)
  19. {
  20. // Ignore any null results
  21. if (result.ActualResult == null)
  22. return;
  23. // The service type must match the current service
  24. if (result.ServiceType != typeof(TService))
  25. return;
  26. // The service names must be equal
  27. if (result.ServiceName != _context.ServiceName)
  28. return;
  29. var service = (TService) result.ActualResult;
  30. var container = result.Container;
  31. var action = _context.Action;
  32. // Execute the action associated with the
  33. // context
  34. action(container, service);
  35. }
  36. }
  37. }