Registering DynaCaches with SimpleInjector

2019-07-25 07:20发布

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

2条回答
Lonely孤独者°
2楼-- · 2019-07-25 07:48

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>());
查看更多
三岁会撩人
3楼-- · 2019-07-25 07:54

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.

查看更多
登录 后发表回答