/// <summary> /// Auto register custom cache classes in an assembly /// </summary> /// <param name="builder">The expiration builder provided by the Expiration Service Registartion</param> /// <param name="assembly">The assembly to scan for CacheModules</param> /// <returns>The expiration builder to add your custom cache registrations</returns> public static ICacheExpirationBuilder AddCacheClasses(this ICacheExpirationBuilder builder, Assembly assembly) { var allTypes = assembly.DefinedTypes .ToArray(); var cacheType = typeof(CacheModule); foreach (var type in allTypes .Where(t => t.IsClass && !t.IsAbstract && cacheType.IsAssignableFrom(t.AsType()) ).Select(t => t.AsType())) { var iCacheType = type.GetInterfaces() .Where(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(ICache <>)) .SelectMany(i => i.GetGenericArguments()) .First(); builder.Builder.Services.AddTransient(iCacheType, type); builder.Builder.Services.AddTransient(type); builder.Builder.Services.AddTransient(cacheType, type); } return(builder); }
/// <summary> /// Auto register custom cache classes in an assembly /// </summary> /// <param name="builder">The expiration builder provided by the Expiration Service Registartion</param> /// <returns>The expiration builder to add your custom cache registrations</returns> public static ICacheExpirationBuilder AddCustomCache <TCacheType, T>(this ICacheExpirationBuilder builder) where TCacheType : CacheModule, ICache <T> { builder.Builder.Services.AddTransient <CacheModule, TCacheType>(x => x.GetService <TCacheType>()); builder.Builder.Services.AddTransient <ICache <T>, TCacheType>(x => x.GetService <TCacheType>()); return(builder); }