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
Post a Comment