Reputation: 1534
We know that named registration is resolved using arrays in constructor injection
// framework code
var injectionMembers = new InjectionConstructor();
...
container.RegisterType(typeof(Employee), "emp1",
new ContainerControlledLifetimeManager(), injectionMembers);
container.RegisterType(typeof(Employee), "emp2",
new ContainerControlledLifetimeManager(), injectionMembers2);
....
// user code
public class Manager
{
public Manager(Employee[] employee)
{
...
}
}
I am looking to support following, when user knows that their is only one named registration.
public class Manager
{
public Manager(Employee employee)
{
...
}
}
And to achieve this I thought if there is some way that I can do two registrations, one with name and one without name in the framework code, that could solve the problem.
I can off course do two registrations but both registrations when resolved would give different instances. Is there a way that I can do two registrations, one named and one named which when resolved return the same instance.
Thanks
Upvotes: 2
Views: 751
Reputation: 1199
Depending on what you want to achieve and if it is possible to instantiate the object at this point, you can just register the instance directly:
container.RegisterType<MyType>("name1", ...);
container.RegisterInstance<MyType>("name2", container.Resolve<MyType>("name1"));
This guarantees that the same instance is registered under both names.
Upvotes: 2