Registering DynaCaches with SimpleInjector

115 Views Asked by At

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

There are 2 best solutions below

0
Mike Goatly On BEST ANSWER

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.

0
Steven On

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>());