c# - Dependency Injection with collections -
so, lets have view named myclassview vm called myclassviewmodel, , these components housed inside of imodule implementation. can register instance of myclass unitybootstrapper , instruct member of myclassviewmodel retrieve through injection.
bootstrapper
this.container.registerinstance<myclass>("myclass", new myclass()); view model
[dependency("myclass")] public myclass myclass { get; set; } simple enouigh. now, lets want go 1 step further , create view myclasscollection follows similar injection strategy, allows inject each instance of myclass myclassview, employing 1 module itemtemplate module.
how go this? thank advice.
unity happily inject named registrations given interface array of interface.
internal class mycollectionviewmodel { public mycollectionviewmodel( iitem[] theitems ) { // use theitems fill collection of itemviewmodels... } } the items have registered interface, of course:
container.registertype<iitem, myclass>( "model 1" ); container.registertype<iitem, someotherclass>( "model 2" ); btw, i'd prefer constructor injection on property injection, because you're forced fill in dependencies. property injection optional dependencies.
Comments
Post a Comment