c# - Using Dependency Injection for hardware abstraction -


i've been playing around dependency injection in connection hardware abstraction. so, have hardware devices want control within c# application. these devices have root device used access leaf devices. structure quite simple: leafdevice1 & 2 connected through interface1 rootdevice1, leafdevice3 connected rootdevice2 , forth.

now thought can solve issue dependency injection since "leaf" devices don't care how they're connected long they're connected specified interface. i'm wondering if dependency injection using ioc container best way it. main reason doubts is: use named dependencies time. if connect device b , c root device want make sure they're referring exact same device. also, use lot of singleton scopes since named dependency xyz should exist once.

so in situation configuring container means gluing lot of named dependencies.

as understand it, using ioc containers makes sense when want specify implementation injected. far can see, i'm using container manage specific object used where. actual implementation of said object can, of course, different, it's still more of "what used where?" not "which implementation used" question.

wouldn't better build device tree can use access devices?

bind<rootdevice>().toconstructor<rootdevice>(e => new rootdevice(serialnumber))     .insingletonscope().named("concreterootdevice");  bind<ibusmaster>().tomethod<ibusmaster>(e => e.kernel.get<rootdevice>("concreterootdevice")     .getbusmaster(0)).named("concretebus1");  bind<ibusmaster>().tomethod<ibusmaster>(e => e.kernel.get<rootdevice>("concreterootdevice")     .getbusmaster(1)).named("concretebus2");  bind<ibusmaster>().tomethod<ibusmaster>(e => e.kernel.get<rootdevice>("concreterootdevice")     .getbusmaster(2)).named("concretebus3"); bind<ibusmaster>().tomethod<ibusmaster>(e => e.kernel.get<rootdevice>("concreterootdevice")     .getbusmaster(3)).named("concretebus4");  bind<leafdevice>().toconstructor<leafdevice>(o =>     new leafdevice(o.context.kernel.get<ibusinterface>("concretebus1")))         .named("concreteleafdevice1");  bind<leafdevice>().toconstructor<leafdevice>(o =>     new leafdevice(o.context.kernel.get<ibusinterface>("concretebus1")))         .named("concreteleafdevice2");  bind<leafdevice>().toconstructor<leafdevice>(o =>     new leafdevice(o.context.kernel.get<ibusinterface>("concretebus2")))         .named("concreteleafdevice3"); 

in example leafdevices depend on abstract ibusinterface communicate actual hardware devices. root device offers several busmasters can used communicate said leaf devices.

thanks in advance

by experience worth use di container singleton scoped instance creation, in case using singleton and/or lightweight pattern heavily in application.

do not reinvent wheel, , do not repeat yourself both satisfied then.

the consideration performance. if using di container fits performance specification / expectation use it! tested, more professional , stable write scratch.


Comments

Popular posts from this blog

How has firefox/gecko HTML+CSS rendering changed in version 38? -

javascript - Complex json ng-repeat -

jquery - Cloning of rows and columns from the old table into the new with colSpan and rowSpan -