Guest User

Untitled

a guest
Jul 21st, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.96 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using Autofac;
  4. using Autofac.Builder;
  5. using Autofac.Core;
  6. using Autofac.Features.Scanning;
  7.  
  8. namespace HiddenNamespace
  9. {
  10. public static class RegistrationExtensions
  11. {
  12. /// <summary>
  13. /// Specifies that a type from a scanned assembly is registered using the default interface.
  14. /// Following the convention of Type, IType. The default interface for ProductRepository will be
  15. /// IProductRepository
  16. /// </summary>
  17. /// <typeparam name="TLimit">Registration limit type.</typeparam>
  18. /// <typeparam name="TScanningActivatorData">Activator data type.</typeparam>
  19. /// <param name="registration">Registration to set service mapping on.</param>
  20. /// <returns>Registration builder allowing the registration to be configured.</returns>
  21. public static IRegistrationBuilder<TLimit, TScanningActivatorData, DynamicRegistrationStyle>
  22. AsDefaultInterface<TLimit, TScanningActivatorData>(this IRegistrationBuilder<TLimit, TScanningActivatorData, DynamicRegistrationStyle> registration)
  23. where TScanningActivatorData : ScanningActivatorData
  24. {
  25. if (registration == null) throw new ArgumentNullException("registration");
  26.  
  27. return registration.As(t => t.GetInterfaces()
  28. .Where(i => i != typeof(IDisposable))
  29. .Where(i => i.IsDefaultInterfaceFor(t))
  30. .Select(i => new TypedService(i))
  31. // ReSharper disable RedundantEnumerableCastCall
  32. .Cast<Service>());
  33. // ReSharper restore RedundantEnumerableCastCall
  34. }
  35.  
  36. /// <summary>
  37. /// Specifies that a is registered using the default interface.
  38. /// Following the convention of Type, IType. The default interface for ProductRepository will be
  39. /// IProductRepository
  40. /// </summary>
  41. /// <typeparam name="TLimit">Registration limit type.</typeparam>
  42. /// <typeparam name="TScanningActivatorData">Activator data type.</typeparam>
  43. /// <param name="registration">Registration to set service mapping on.</param>
  44. /// <returns>Registration builder allowing the registration to be configured.</returns>
  45. public static IRegistrationBuilder<TLimit, TScanningActivatorData, SingleRegistrationStyle>
  46. AsImplementedInterfaces<TLimit, TScanningActivatorData>(this IRegistrationBuilder<TLimit, TScanningActivatorData, SingleRegistrationStyle> registration)
  47. {
  48. if (registration == null) throw new ArgumentNullException("registration");
  49. return registration.As(typeof(TLimit).GetInterfaces()
  50. .Where(i=>i.IsDefaultInterfaceFor(typeof(TLimit)))
  51. .Select(i => new TypedService(i))
  52. .Cast<Service>()
  53. .ToArray());
  54. }
  55.  
  56. public static bool IsDefaultInterfaceFor(this Type interfaceType, Type type)
  57. {
  58. return interfaceType.Name == "I" + type.Name;
  59. }
  60. }
  61. }
Add Comment
Please, Sign In to add comment