Sto tentando di associare due classi concrete a un'interfaccia. Quale comando dovrei usare in Ninject per farlo? Quello che sto cercando di fare è legare due classi concrete a una base di interfaccia sul nome del controller. È possibile? Suppongo che in ninject usi il. Quando vuoi dare il condizionale ma non ci sono tutorial là fuori dove ti mostrano come usare il. Quando si fa il ninject.Ninject to bind su controller diversi
5
A
risposta
8
Ecco alcuni esempi. Scopri il progetto di origine Ninject e il suo sottoprogetto Test per vari esempi di utilizzo, questa è la migliore documentazione per questo, specialmente dal momento che i documenti non sono ancora stati aggiornati per la v2.
// usage of WhenClassHas attribute
Bind<IRepository>().To<XmlDefaultRepository>().WhenClassHas<PageAttribute>().WithConstructorArgument("contentType", ContentType.Page);
// usage of WhenInjectedInto
Bind<IRepository>().To<XmlDefaultRepository>().WhenInjectedInto(typeof(ServicesController));
Bind<IRepository>().To<XmlDefaultRepository>().WhenInjectedInto(typeof(PageController)).WithConstructorArgument("contentType", ContentType.Page);
Bind<IRepository>().To<XmlDefaultRepository>().WhenInjectedInto(typeof(WidgetZoneController)).WithConstructorArgument("contentType", ContentType.WidgetZone);
// you can also do this
Bind<IRepository>().To<PageRepository>().WhenInjectedInto(typeof(PageController)).WithConstructorArgument("contentType", ContentType.Page);
Bind<IRepository>().To<WidgetZoneRepository>().WhenInjectedInto(typeof(WidgetZoneController)).WithConstructorArgument("contentType", ContentType.WidgetZone);
// or this if you don't need any parameters to your constructor
Bind<IRepository>().To<PageRepository>().WhenInjectedInto(typeof(PageController));
Bind<IRepository>().To<WidgetZoneRepository>().WhenInjectedInto(typeof(WidgetZoneController));
// usage of ToMethod()
Bind<HttpContextBase>().ToMethod(context => new HttpContextWrapper(HttpContext.Current));
HTH
Problemi correlati
- 1. Ninject Bind Quando antenato del tipo T
- 2. Ninject sintassi per "Bind" con più argomenti
- 3. MvvmCross Bind to UIButton.TitleLabel.Text
- 4. Ninject: Contesto entità per controller
- 5. Bind Collection to StackPanel
- 6. Bind Service to BroadcastReceiver
- 7. Bind dictionary to repeater
- 8. ListBox TwoWay bind to SelectedItem
- 9. Bind to pgcrypto da python
- 10. std :: bind to std :: function?
- 11. bind date to command object in Grails
- 12. Ninject + MVC3 non si sta iniettando nel controller
- 13. Ninject multicasting
- 14. Bind Xaml Radio button to boolean
- 15. iniezione Proprietà a controller Base utilizzando Ninject 2
- 16. Winform Bind Enum to Radio Buttons
- 17. MvvmCross Checkbox bind to command android xml
- 18. WPF Bind Window Title to ViewModel Property
- 19. Bind scroll Event To Dynamic DIV?
- 20. Ninject Binding Attribute to Filter with Constructor Argomenti
- 21. MVC Pass ViewBag to Controller
- 22. Ninject 2 e MVC 2.0
- 23. Sostituisci Ninject con Simple Injector
- 24. Post JSON to Codeigniter controller
- 25. Ninject: parametro Costruttore
- 26. attacchi contestuali con Ninject 2.0
- 27. DI ninject funzionante per controller ma non vista modello
- 28. Quartz.NET, NH ISession & Ninject Scope
- 29. Quale DLL è il Bind (Of T) di Ninject che si trova in
- 30. Ninject: sintassi del collegamento Singleton?
provo il comando WhenInjectedInto(), ma ancora non funziona per me. Se il tuo controller ha parametri hai davvero bisogno di aggiungere WithConstructorArgument()? – Ganator
No, il controller ha solo un costruttore che accetta IRepository, ma l'implementazione IRepository (nel mio caso XmlDefaultRepository) ha il costruttore che accetta il parametro contentType di tipo string, che è l'esempio di WithConstructorArgument(). – mare
Nota: questi quando i metodi ...() e With ...() sono concatenabili, è possibile interrompere WhenInjectedInto(). E, sì, WhenInjectedInto() funziona per me subito, molto semplice, a patto che anche l'implementazione del modello di repository sia semplice. Puoi pubblicare il codice per l'interfaccia del tuo repository e la sua implementazione e lasciarci vedere. Inserisci anche il codice da global.asax.cs in cui hai impostato DI. – mare