private CacheTemplate GetCacheTemplateFromProductRepository() { var type = typeof(IProductRepository); var model = new CacheTemplate(type); var getProduct = type.GetMethod(nameof(IProductRepository.GetProduct)); var getProductAsync = type.GetMethod(nameof(IProductRepository.GetProductAsync)); var getProductConfig = new MethodCacheConfiguration() { Name = getProduct.Name, ExpirationSeconds = 60, KeyTemplate = "product_{id}" }; var getProductAsyncConfig = new MethodCacheConfiguration() { Name = getProduct.Name, ExpirationSeconds = 60, KeyTemplate = "product_async_{id}" }; model.AddMethod(new CacheMethodTemplate(getProduct, getProductConfig)); model.AddMethod(new CacheMethodTemplate(getProductAsync, getProductAsyncConfig)); return(model); }
private CacheTemplate GetCacheTemplateFromGroupRepository() { var type = typeof(IGroupRepository); var model = new CacheTemplate(type); var getGroup = type.GetMethod(nameof(IGroupRepository.GetGroup)); var getGroupConfig = new MethodCacheConfiguration() { Name = getGroup.Name, ExpirationSeconds = 60, KeyTemplate = "group_{id}" }; model.AddMethod(new CacheMethodTemplate(getGroup, getGroupConfig)); return(model); }
public static CacheTemplate GetCacheTemplateFromReflectionType(this Type type, InterfaceCacheConfiguration cacheConfiguration) { if (type == null) { throw new Exception("Service type is no defined."); } if (!type.IsInterface) { throw new Exception("Only service interfaces are allowed for cache code generation."); } var cacheTemplate = new CacheTemplate(type); foreach (var method in type.GetAllMethods()) { var methodConfiguration = cacheConfiguration.Methods.FirstOrDefault(x => x.Name == method.Name); var cacheMethod = new CacheMethodTemplate(method, methodConfiguration); cacheTemplate.AddMethod(cacheMethod); } ; foreach (var property in type.GetProperties()) { cacheTemplate.AddProperty(property); } var methodNamesCacheTemplate = cacheTemplate.Methods.Select(m => m.Name); var methodNamesCacheConfiguration = cacheConfiguration.Methods.Select(m => m.Name); var methodsNotMatched = methodNamesCacheConfiguration?.Where(x => !methodNamesCacheTemplate?.Contains(x) ?? false); if (methodsNotMatched?.Any() ?? false) { var methodName = methodsNotMatched.FirstOrDefault(); throw new Exception($"Method '{methodName}' not found in interface {type.Name}."); } return(cacheTemplate); }