Reputation: 7059
(EDIT: After writing this question I found at least one solution so this question is self-answered. It might still benefit from a pair of eyes that knows more about IoC containers than I do.)
I have a very special scenario that smells like it could make use of an IoC container, but a technical detail makes using using existing ones difficult.
The scenario is me toying with the following separation of user interface components (actually Blazor components, so let's call them Component
s) from model classes they then bind against (let's call them UiObject
s).
All UiObject
s have a default Component<U>
(ie. where U : IUiObject
) that can bind to them (think U
= IUiCommand
to GenericButtonComponent
being a Component<IUiCommand>
, etc.), but a user interface designer can override styling and logic by adding a new SpecialButtonComponent
, also being a Component<UiCommand>
, that can then also be used with IUiCommand
. That special component could differ from the old one in html, css and even stateful logic, but it's always a drop-in replacement for the default one.
I know need some flexible way to choose the correct Component<U>
for each U
that is a IUiObject
on the basis on all sorts of things (collecting from asseblies, xml configuration, clever mechanisms to prioritize types over each other, etc.).
You'd think that this is just what IoC containers are made for, but there's a catch: Blazor expects me to deliver types, not instances, that Blazor then itself creates. I need a
Type componentType = container.ResolveType<IUiCommand>()
I've superficially looked at Autofac, Castle Windsor, Lamar, LightInject, SimpleInjector and only realized after writing this question that at least SimpleInjector can resolve a DependencyMetadata<IFoo>
that contains the resolved type - no luck with any other framework so far though.
So I'll answer my own question here, but I'd still be curious which other IoC containers have that feature.
Upvotes: 0
Views: 193
Reputation: 7059
Simple Injector can resolve a DependencyMetadata<IFoo>
that contains the resolved type like this:
var implementationType =
container.GetInstance<DependencyMetadata<IFoo>>()?.ImplementationType;
Upvotes: 1