예제 #1
0
        public void CompileMultipleAssemblies()
        {
            var model     = GetCacheTemplateFromProductRepository();
            var classFile = TemplateService.GenerateCacheCode(model);
            var assembly1 = CompilerService.GenerateAssemblyFromCode(typeof(IProductRepository).Assembly, model.Name, classFile);
            var assembly2 = CompilerService.GenerateAssemblyFromCode(typeof(IProductRepository).Assembly, model.Name, classFile);

            Assert.NotEqual(assembly1.GetHashCode(), assembly2.GetHashCode());
        }
예제 #2
0
        public void CompileAssemblyFromTemplate()
        {
            var model     = GetCacheTemplateFromProductRepository();
            var classFile = TemplateService.GenerateCacheCode(model);
            var assembly  = CompilerService.GenerateAssemblyFromCode(typeof(IProductRepository).Assembly, model.Name, classFile);
            var type      = assembly.GetTypes().FirstOrDefault();

            Assert.Contains(model.Name, assembly.GetTypes().Select(x => x.Name));
        }
        public static void DecorateWithCacheDynamicService(this IServiceCollection services, InterfaceCacheConfiguration cacheConfiguration)
        {
            var serviceMatched = services.FirstOrDefault(x => x.ServiceType.Name == cacheConfiguration.Name);

            if (serviceMatched == null)
            {
                return;
            }

            var typeService   = serviceMatched.ServiceType;
            var cacheTemplate = typeService.GetCacheTemplateFromReflectionType(cacheConfiguration);
            var cacheCode     = TemplateService.GenerateCacheCode(cacheTemplate);
            var assemblyCache = CompilerService.GenerateAssemblyFromCode(typeService.Assembly, cacheTemplate.Name, cacheCode);
            var typeCache     = assemblyCache.GetTypes().FirstOrDefault();

            services.Decorate(typeService, typeCache);
        }
예제 #4
0
        public async Task DecorateServiceWithDynamicAssembly()
        {
            var configuration  = _serviceProvider.GetService <IConfiguration>();
            var cacheFormatter = _serviceProvider.GetService <ICacheSerializer>();
            var model          = GetCacheTemplateFromProductRepository();
            var classFile      = TemplateService.GenerateCacheCode(model);
            var assembly       = CompilerService.GenerateAssemblyFromCode(typeof(IProductRepository).Assembly, model.Name, classFile);
            var typeGenerated  = assembly.GetTypes().FirstOrDefault();

            var concreteRepository = new ProductRepository();
            var cacheRepository    = (IProductRepository)Activator.CreateInstance(typeGenerated, concreteRepository, configuration, cacheFormatter);

            var product1 = cacheRepository.GetProduct(1);

            product1 = cacheRepository.GetProduct(1);

            var product2 = await cacheRepository.GetProductAsync(1);

            product2 = await cacheRepository.GetProductAsync(1);

            Assert.Equal(10, product1.Id);
            Assert.Equal(10, product2.Id);
        }