This is for the CacheManager library by Michah Conrad. In his example, he creates a cache manager using the CacheFactory.Build method, as seen here:
public class WebApiApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
GlobalConfiguration.Configure(WebApiConfig.Register);
var container = new UnityContainer();
GlobalConfiguration.Configuration.DependencyResolver = new UnityDependencyResolver(container);
var cache = CacheFactory.Build("todos", settings =>
{
settings
.WithSystemRuntimeCacheHandle("inprocess");
});
container.RegisterInstance(cache);
}
}
My question, is it possible to register the ICacheManger interface so that any property dependencies of any type will be automatically created by the IoC container?
Say I have this class
public class MyClass
{
[Inject]
public ICacheManager<string> StringCacheManager { get; set; }
[Inject]
public ICacheManager<int> IntCacheManager { get; set; }
}
How could I set up my Ninject kernel to bind the generic ICacheManager interface so that it resolves using the types in MyClass?
Something like this, but that actually works:
kernel.Bind<ICacheManager<T>>().ToMethod((context) =>
{
return CacheFactory.FromConfiguration<T>("defaultCache");
});