Factory methods in Ninject C# -


i have interface called ibreakfastprovider, has several implementations. code decides 1 use based off id. uses factory method, this:

public ibreakfastprovider getbreakfastprovider(int id) {     switch (id)     {         case 1:             return new cornflakeprovider(new somedependency());         case 2:             return new muesliprovider(new someotherdependency());         case 3:             return new toastprovider();         default:             throw new applicationexception("unknown provider id.");      } } 

each implementation of ibreakfast provider can have own dependencies. how method replaced use ninject work out implementation use based of integer id?

you can register each type name. then, when need resolve, pass id ninject:

// use better naming though :) kernel.bind<ibreakfastprovider>().to<cornflakeprovider>().named("1"); 

and when resolve:

var breakfastprovider = resolutionroot.get<ifoo>(id); 

Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -