-
-
Notifications
You must be signed in to change notification settings - Fork 120
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Preserve registration order for IEnumerable #288
Comments
I'm using the chain of responsibility pattern and I have to resolve my interface as an I've been reading seesharper/LightInject.Microsoft.DependencyInjection#3 and the related issues and I'm left confused... The behaviour I get in my unit tests is that LightInject does not resolve a type with multiple implementations in the order the implementations were registered. Any reason why? container.Register<IFoo, One>();
container.Register<IFoo, Two>("Two");
container.Register<IFoo, Three>("Three");
container.Register<IFoo, Four>("Four");
// foos will contain IFoo implementations that are not ordered in this sequence: One, Two, Three, Four
var foos = container.GetAllInstances<IFoo>(); In order to get the instances ordered in a given sequence, I ended up doing registering container.Register<One>();
container.Register<Two>();
container.Register<Three>();
container.Register<Four>();
container.Register<IEnumerable<IFoo>>(f => new IFoo[] {
f.GetInstance<One>(),
f.GetInstance<Two>(),
f.GetInstance<Three>(),
f.GetInstance<Four>()
});
// foos contains instances in the following order: One, Two, Three, Four
var foos = container.GetAllInstances<IFoo>(); Is this the the recommended way of making an |
The reason that this is not deterministic is that registrations are stored in a hash table which does not preserve the order. Items in an hash table ends up in buckets based on hash code and hence the order is lost. Your solution would work perfectly. |
Bummer. This would be useful for scenarios where the number of items are arbitrary and are added without knowing the types added before or after them, but where order is important. Would it be possible to consider using a |
No description provided.
The text was updated successfully, but these errors were encountered: