private static ILookup <string, NavigationItemAttribute> GetNavigationItemAttributes( ITypeSource typeSource, IServiceProvider serviceProvider, Func <NavigationItemAttribute, bool> filter) { var list = new List <NavigationItemAttribute>(); foreach (NavigationItemAttribute attr in typeSource .GetAssemblyAttributes <NavigationItemAttribute>()) { if (filter == null || filter(attr)) { list.Add(attr); } } foreach (var navItemType in typeSource.GetTypesWithInterface(typeof(INavigationItemSource)) .Where(x => !x.IsAbstract && !x.IsInterface)) { var navItem = (INavigationItemSource)ActivatorUtilities.CreateInstance( serviceProvider, navItemType); foreach (var item in navItem.GetItems()) { if (filter == null || filter(item)) { list.Add(item); } } } return(ByCategory(list)); }
public static IServiceCollection AddCustomRequestHandlers(this IServiceCollection collection, ITypeSource typeSource = null, Func <Type, Type, bool> predicate = null) { if (collection == null) { throw new ArgumentNullException(nameof(collection)); } typeSource ??= collection.FirstOrDefault(x => x.ServiceType == typeof(ITypeSource))?.ImplementationInstance as ITypeSource; if (typeSource == null) { throw new ArgumentNullException(nameof(typeSource)); } var interfacesToAdd = new Dictionary <Type, List <Type> >(); foreach (var type in typeSource.GetTypesWithInterface(typeof(IRequestHandler))) { if (type.IsAbstract || type.IsInterface || type.IsGenericType) { continue; } if (predicate == null || predicate(type, type)) { collection.TryAddTransient(type, type); } foreach (var intf in type.GetInterfaces()) { if (!typeof(IRequestHandler).IsAssignableFrom(intf)) { continue; } if (intf == typeof(IRequestHandler) || intf == typeof(ISaveRequestProcessor) || intf == typeof(ISaveRequestHandler) || intf == typeof(IListRequestProcessor) || intf == typeof(IListRequestHandler) || intf == typeof(IRetrieveRequestProcessor) || intf == typeof(IRetrieveRequestHandler) || intf == typeof(IDeleteRequestProcessor) || intf == typeof(IDeleteRequestHandler)) { continue; } if (intf.IsGenericType) { var genericBase = intf.GetGenericTypeDefinition(); if (genericBase == typeof(IRequestHandler <, ,>) || genericBase == typeof(IRequestHandler <>) || genericBase == typeof(IRequestType <>) || genericBase == typeof(IResponseType <>)) { continue; } } if (predicate != null && !predicate(intf, type)) { continue; } if (interfacesToAdd.TryGetValue(intf, out var list)) { list.Add(type); } else { interfacesToAdd[intf] = new List <Type> { type } }; } } foreach (var pair in interfacesToAdd) { // don't override users own registration if any var existing = collection.FirstOrDefault(x => x.ServiceType == pair.Key); if (existing != null) { continue; } if (pair.Value.Count != 1) { var defaults = pair.Value.Where(x => x.GetAttribute <DefaultHandlerAttribute>()?.Value == true).ToArray(); if (defaults.Length != 1) { if (defaults.Length == 0) { throw new InvalidProgramException($"There are multiple {pair.Key} request handler types ({string.Join(", ", pair.Value)}). " + "Please add [DefaultHandler] to one of them!"); } throw new InvalidProgramException($"There are multiple {pair.Key} request handler types with [DefaultHandler] attribute ({string.Join(", ", (IEnumerable<Type>)defaults)}). " + "Please use [DefaultHandler] on only one of them!"); } collection.AddTransient(pair.Key, defaults[0]); continue; } collection.AddTransient(pair.Key, pair.Value[0]); } return(collection); }
public virtual IEnumerable <Type> GetTypes() { return(typeSource.GetTypesWithInterface(typeof(IRequestHandler)) .Where(type => !type.IsInterface && !type.IsAbstract)); }