Registering DynaCaches with SimpleInjector

六眼飞鱼酱① 提交于 2019-12-11 10:27:11

问题


Hi I am attempting to use Dynacache in my solution to cache data I am returning from Database so when I use Telerik grid to filter the data page etc I dont have to go back to the DB to get the data each time.

The example on the DynaCache page shows it being used with Ninject DI as below:

kernel.Bind<IDynaCacheService>().To<MemoryCacheService>();
kernel.Bind<ITestClass>().To(Cacheable.CreateType<TestClass>());

I am using SimpleInjector as my DI container. Has anyone used Dynacache with SimpleInjector as I am having some difficulty in getting the correct syntax to Register Dynacache with SimpleInjector the same way it is shown in Ninject


回答1:


I've put together a blog post covering this now - the answer marked as correct isn't actually right - MemoryCacheService needs to be a singleton because it contains a MemoryCache instance that needs to be shared across all dependent instances.




回答2:


The Simple Injector equivalent is:

container.Register<IDynaCacheService, MemoryCacheService>();
container.Register(typeof(ITestClass), Cacheable.CreateType<TestClass>());

However, since MemoryCacheService is a framework type, you'd be better (as explained here) of making the registration using a factory delegate:

container.Register<IDynaCacheService>(() => new MemoryCacheService());
container.Register(typeof(ITestClass), Cacheable.CreateType<TestClass>());


来源:https://stackoverflow.com/questions/24261014/registering-dynacaches-with-simpleinjector

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!