0

I'm trying to register to implementations of same interface using named instances

kernel.Bind<IRepository>().To<CachedRepository>().InSingletonScope();
kernel.Bind<IRepository>().To<DbRepository>().InSingletonScope().Named("db");

the idea, is that if I not specify the name then the CachedRepository gets created, if I need a DB oriented one then I'd use the Named attribute, but this miserable fails when a simple object would get created

public class TripManagerController : Controller
    {
        [Inject]
        public IRepository Repository { get; set; } // default Cached repo must be created

        public TripManagerController()
        {
            ViewBag.LogedEmail = "test@test.com";
        }
}

the error is

Error activating IRepository More than one matching bindings are available. Activation path: 2) Injection of dependency IRepository into parameter repository of constructor of type TripManagerController 1) Request for TripManagerController

Suggestions: 1) Ensure that you have defined a binding for IRepository only once.

Is there a way to achieve what I want without creating a new interface for BD oriented repositories? Thx

Eugen
  • 2,934
  • 2
  • 26
  • 47

2 Answers2

0

The [Named] attribute as shown in the wiki should work.

BTW stay away from anything other than ctor injection!

Ruben Bartelink
  • 59,778
  • 26
  • 187
  • 249
0

It would seem you cannot do what you're trying, I've just come across the same issue and as well as finding your question I also found this one where the author of Ninject Remo Gloor replied.

https://stackoverflow.com/a/4051391/495964

While Remo didn't explicitly say it couldn't be done his answer was to name both bindings (or use custom attribute binding, amounting the same thing).

Community
  • 1
  • 1
James Skimming
  • 4,991
  • 4
  • 26
  • 32