tag:robotlegs.tenderapp.com,2009-10-18:/discussions/robotlegs-2/8420-injection-by-typeinterfaceRobotlegs: Discussion 2013-12-07T14:58:59Ztag:robotlegs.tenderapp.com,2009-10-18:Comment/302645382013-11-29T12:38:26Z2013-11-29T12:38:26ZInjection by type/interface<div><p>That's definitely not possible, no... Sorry :)</p>
<p>If I understand it correctly you have multiple singletons that
implement<br>
the same interface and you've used named mappings in the injector,
right?</p>
<p>E.g.:</p>
<p>map(IFoo, 'foo').toSingleton(Foo)<br>
map(IFoo, 'bar').toSingleton(Bar)</p>
<p>etc.</p>
<p>Why do you need access to all of them at once?</p></div>creynderstag:robotlegs.tenderapp.com,2009-10-18:Comment/302645382013-11-29T13:29:43Z2013-11-29T13:29:43ZInjection by type/interface<div><p>I think Thomas means something like this:<br>
<a href=
"https://github.com/ninject/ninject/wiki/Multi-injection">https://github.com/ninject/ninject/wiki/Multi-injection</a></p>
<p>which would be cool to have.</p></div>Ondina D.F.tag:robotlegs.tenderapp.com,2009-10-18:Comment/302645382013-11-29T14:05:11Z2013-11-29T14:05:11ZInjection by type/interface<div><p>@creynders Yes, spot on.</p>
<p>In fact the interface is named IEntityXmlParser and I have a
service that needs access to a range of parsers (one per entity
domain) since its loading a complex object graph, which is composed
of a variety of entities which all need their own parser.</p></div>thomas.auingertag:robotlegs.tenderapp.com,2009-10-18:Comment/302645382013-11-29T14:44:36Z2013-11-29T14:44:36ZInjection by type/interface<div><p>If you mapped them as creynders said, you can iterate over an
array of strings ('foo', 'bar'...) that correspond to the names
used for the mapping, and let the injector get all the needed
instances</p>
<p>injector.getInstance(IFoo,str)</p></div>Ondina D.F.tag:robotlegs.tenderapp.com,2009-10-18:Comment/302645382013-11-30T07:55:52Z2013-11-30T07:55:52ZInjection by type/interface<div><p>It seems to me this could be solved far more elegantly with a
factory. Since these are parser classes they don't need to persist
(I assume) and could be created as throw-away objects.<br>
I tend to avoid named injections whenever possible. (So far, I've
never needed them)</p></div>creynderstag:robotlegs.tenderapp.com,2009-10-18:Comment/302645382013-11-30T10:34:05Z2013-11-30T10:34:05ZInjection by type/interface<div><p>@creynders</p>
<p>Yes, a factory is always a good choice.</p>
<p>I was looking at IoC libraries for C# , like SimpleInjector,
ninject.., and their ability to obtain a list of all the registered
objects of a specific type.</p>
<p>I'm just curious, what do you think would speak against having a
mapAll (registerAll) and getAllInstances for multiple classes
implementing the same interface in robotlegs, besides more work
;)</p></div>Ondina D.F.tag:robotlegs.tenderapp.com,2009-10-18:Comment/302645382013-11-30T12:43:46Z2013-11-30T12:43:46ZInjection by type/interface<div><blockquote>
<p>I'm just curious, what do you think would speak against having a
mapAll (registerAll) and getAllInstances for multiple classes
implementing the same interface in robotlegs, besides more work
;)</p>
</blockquote>
<p>Nothing really, I suppose. Personally I've never had a need for
it, but<br>
that doesn't mean it wouldn't be a good idea.</p></div>creynderstag:robotlegs.tenderapp.com,2009-10-18:Comment/302645382013-12-03T09:28:10Z2013-12-03T09:28:10ZInjection by type/interface<div><p>Thanks for the discussion! The factory pattern seems to make a
lot of sense here and I will switch to that. Anyway, thanks for all
the input, helped me beyond this issue as well.</p>
<p>BTW great work on the framework!!</p></div>thomas.auingertag:robotlegs.tenderapp.com,2009-10-18:Comment/302645382013-12-05T12:48:50Z2013-12-05T12:48:50ZInjection by type/interface<div><p>No problem :)</p></div>Ondina D.F.