|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using Microsoft.Extensions.DependencyInjection; |
| 5 | +using Umbraco.Cms.Core.DependencyInjection; |
| 6 | +using Umbraco.Cms.Core.Events; |
| 7 | + |
| 8 | +namespace Umbraco.Cms.Core.Extensions |
| 9 | +{ |
| 10 | + public static class UmbracoBuilderExtensions |
| 11 | + { |
| 12 | + /// <summary> |
| 13 | + /// Registers all <see cref="INotificationHandler{TNotification}"/> within an assembly |
| 14 | + /// </summary> |
| 15 | + /// <param name="self"><see cref="IUmbracoBuilder"/></param> |
| 16 | + /// <typeparam name="T">Type contained within the targeted assembly</typeparam> |
| 17 | + /// <returns></returns> |
| 18 | + public static IUmbracoBuilder AddNotificationsFromAssembly<T>(this IUmbracoBuilder self) |
| 19 | + { |
| 20 | + AddNotificationHandlers<T>(self); |
| 21 | + AddAsyncNotificationHandlers<T>(self); |
| 22 | + |
| 23 | + return self; |
| 24 | + } |
| 25 | + |
| 26 | + private static void AddNotificationHandlers<T>(IUmbracoBuilder self) |
| 27 | + { |
| 28 | + var notificationHandlers = GetNotificationHandlers<T>(); |
| 29 | + foreach (var notificationHandler in notificationHandlers) |
| 30 | + { |
| 31 | + var handlerImplementations = GetNotificationHandlerImplementations<T>(notificationHandler); |
| 32 | + foreach (var implementation in handlerImplementations) |
| 33 | + { |
| 34 | + RegisterNotificationHandler(self, implementation, notificationHandler); |
| 35 | + } |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + private static List<Type> GetNotificationHandlers<T>() => |
| 40 | + typeof(T).Assembly.GetTypes() |
| 41 | + .Where(x => x.IsAssignableToGenericType(typeof(INotificationHandler<>))) |
| 42 | + .ToList(); |
| 43 | + |
| 44 | + private static List<Type> GetNotificationHandlerImplementations<T>(Type handlerType) => |
| 45 | + handlerType |
| 46 | + .GetInterfaces() |
| 47 | + .Where(x => x.IsGenericType && x.GetGenericTypeDefinition() == typeof(INotificationHandler<>)) |
| 48 | + .ToList(); |
| 49 | + |
| 50 | + private static void AddAsyncNotificationHandlers<T>(IUmbracoBuilder self) |
| 51 | + { |
| 52 | + var notificationHandlers = GetAsyncNotificationHandlers<T>(); |
| 53 | + foreach (var notificationHandler in notificationHandlers) |
| 54 | + { |
| 55 | + var handlerImplementations = GetAsyncNotificationHandlerImplementations<T>(notificationHandler); |
| 56 | + foreach (var handler in handlerImplementations) |
| 57 | + { |
| 58 | + RegisterNotificationHandler(self, handler, notificationHandler); |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + private static List<Type> GetAsyncNotificationHandlers<T>() => |
| 64 | + typeof(T).Assembly.GetTypes() |
| 65 | + .Where(x => x.IsAssignableToGenericType(typeof(INotificationAsyncHandler<>))) |
| 66 | + .ToList(); |
| 67 | + |
| 68 | + private static List<Type> GetAsyncNotificationHandlerImplementations<T>(Type handlerType) => |
| 69 | + handlerType |
| 70 | + .GetInterfaces() |
| 71 | + .Where(x => x.IsGenericType && x.GetGenericTypeDefinition() == typeof(INotificationAsyncHandler<>)) |
| 72 | + .ToList(); |
| 73 | + |
| 74 | + private static void RegisterNotificationHandler(IUmbracoBuilder self, Type notificationHandlerType, Type implementingHandlerType) |
| 75 | + { |
| 76 | + var descriptor = new UniqueServiceDescriptor(notificationHandlerType, implementingHandlerType, ServiceLifetime.Transient); |
| 77 | + if (!self.Services.Contains(descriptor)) |
| 78 | + { |
| 79 | + self.Services.Add(descriptor); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + private static bool IsAssignableToGenericType(this Type givenType, Type genericType) |
| 84 | + { |
| 85 | + var interfaceTypes = givenType.GetInterfaces(); |
| 86 | + |
| 87 | + foreach (var it in interfaceTypes) |
| 88 | + { |
| 89 | + if (it.IsGenericType && it.GetGenericTypeDefinition() == genericType) |
| 90 | + { |
| 91 | + return true; |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + if (givenType.IsGenericType && givenType.GetGenericTypeDefinition() == genericType) |
| 96 | + { |
| 97 | + return true; |
| 98 | + } |
| 99 | + |
| 100 | + var baseType = givenType.BaseType; |
| 101 | + return baseType != null && IsAssignableToGenericType(baseType, genericType); |
| 102 | + } |
| 103 | + } |
| 104 | +} |
0 commit comments