/src/LinFu.AOP.Interfaces/ExceptionHandlerRegistry.cs

http://github.com/philiplaureano/LinFu · C# · 42 lines · 23 code · 4 blank · 15 comment · 3 complexity · 0c8e382a29d347ca66424d2f0b84c09a MD5 · raw file

  1. namespace LinFu.AOP.Interfaces
  2. {
  3. /// <summary>
  4. /// Represents a class that stores <see cref="IExceptionHandler" /> instances in a central location.
  5. /// </summary>
  6. public static class ExceptionHandlerRegistry
  7. {
  8. private static IExceptionHandler _handler;
  9. private static readonly object _lock = new object();
  10. /// <summary>
  11. /// Gets the <see cref="IExceptionHandler" /> instance that can handle the current exception.
  12. /// </summary>
  13. /// <param name="info">
  14. /// The <see cref="IExceptionHandlerInfo" /> instance that describes the context of the thrown
  15. /// exception.
  16. /// </param>
  17. /// <returns>An exception handler.</returns>
  18. public static IExceptionHandler GetHandler(IExceptionHandlerInfo info)
  19. {
  20. if (_handler == null)
  21. return null;
  22. if (!_handler.CanCatch(info))
  23. return null;
  24. return _handler;
  25. }
  26. /// <summary>
  27. /// Sets the <see cref="IExceptionHandler" /> instance that can handle all thrown exceptions.
  28. /// </summary>
  29. /// <param name="handler">The exception handler.</param>
  30. public static void SetHandler(IExceptionHandler handler)
  31. {
  32. lock (_lock)
  33. {
  34. _handler = handler;
  35. }
  36. }
  37. }
  38. }