Reputation: 6865
Hi I am using Ninject IoC container. I can not convert the structuremap code to ninject.
This is Structuremap code binding
For<IProductCatalogService>().Use<ProductCatalogService>().Named("realProductCatalogService");
For<IProductCatalogService>().Use<CachedProductCatalogService>()
.Ctor<IProductCatalogService>().Is(p => p.TheInstanceNamed("realProductCatalogService"));
And I am using Ninject code like this
Kernel.Bind<IProductCatalogService>().To<ProductCatalogService>().Named("realProductCatalogService");
Kernel.Bind<IProductCatalogService>().To<CachedProductCatalogService>().Named("cachedProductCatalogService");
But this not working.
Upvotes: 0
Views: 274
Reputation: 5666
I suggest you want to inject some implementation of IProductCatalogService
into CachedProductCatalogService
, that also implements IProductCatalogService
and then use this cached implementation in the rest of the application as the default component.
With Ninject you can configure it using the .WhenParentNamed
conditional binding like this:
Kernel.Bind<IProductCatalogService>()
.To<ProductCatalogService>()
.WhenParentNamed("cached");
Kernel.Bind<IProductCatalogService>()
.To<CachedProductCatalogService>()
.Named("cached");
When there is a request for IProductCatalogService
ninject will try to resolve the conditions. If the parent component (that asked for the injection) is named "cached"
(the CachedProductCatalogService
in your case) than ninject will return ProductCatalogService
otherwise it will return CachedProductCatalogService
as the default.
Upvotes: 1