c# - NancyFx and TinyIoc -
i new dependency injection.i having problems understanding how use nancyfx bootstrapper , tinyioc. want reference dependency can not understatnd how it. can not seem find correct info anywhere or misunderstanding it.this code:
public class startup1 { public void configuration(iappbuilder app) { app.usenancy(); } }
my interface:
public interface iperson { string getname(); }
my implementation:
public class person : iperson { public string name { get; set; } public string getname() { return name; } }
my program
static void main(string[] args) { person person = new person(); person.name = "bill"; string uri = "http://localhost:8080/"; using (webapp.start<startup1>(uri)) { console.writeline("started"); console.readkey(); console.writeline("stopping"); } }
my nancy bootstrapper:
public class mynancyboot : defaultnancybootstrapper { protected override void configureapplicationcontainer(tinyioccontainer container) { //base.configureapplicationcontainer(container); container.register<iperson>(new person()); } }
my nancymodule:
public class mynancymodule : nancymodule { public mynancymodule(iperson person) { get["/"] = _ => { var x = tinyioccontainer.current.resolve<iperson>(); return "ok"; }; } }
this return error in mynancymodule.
var x = tinyioccontainer.current.resolve<iperson>();
ok how person mynancymodule?
i want reference dependency can not understatnd how it.
i think there couple of things @ here.
you mistakenly thinking need somehow pass in, on controller constructor, type may want return get["/"] controller operation. not case. if 1 of service operations happens return person type, not need inject person type constructor. rather like:
get["/"] = _ => { var x = new list<person>(); return response.asjson(x); };
obviously service operation of limited use, return empty list of type person.
so need dependency gets persons. eg:
interface igetpersons { list<person> getall(); }
in order used inside controller, tiny ioc need know it. can register type tiny ioc.
container.register<igetpersons>();
this brings onto second thing struggling with, how tiny ioc works.
after add implicit registration in nancy bootstrapper, tiny ioc scan assemblies , find implementation of igetpersons automatically! (this assuming there single implementation. if more 1 need more work that's story).
this magic allows modify controller thus:
public mynancymodule(igetpersons persongetter) { get["/"] = _ => { var x = list<person> persongetter.getall(); // look, don't need resolve manually! return response.asjson(x); }; }
tiny ioc take care of resolving injected type igetpersons implementation, free use through controller.
lets client makes http post win form app running nancy service. how data nancy service win form.
have @ following: http://weblogs.asp.net/rweigelt/10235654
the author using managed extensibility framework (mef) make changes assume kind of singleton provided framework:
container.register(mefcontainer.getexportedvalue<iremoteaccess>());
Comments
Post a Comment