ResolveKeyed vs. IIndex

26 views
Skip to first unread message

Glenn MacGregor

unread,
Sep 3, 2014, 2:23:46 PM9/3/14
to aut...@googlegroups.com
Hi All,

Thank you all for the responses on my last post they really helped me get a handle on some of the intricacies of Autofac. 

In general if I am registering a set of IInterface1 implementations:

builder.RegisterType<CClass1>().Keyed<IInterface1>(Enum1)
builder.RegisterType<CClass2>().Keyed<IInterface1>(Enum2)

Should I inject a ILifetimeScope or a IIndex into my classes that need that info?

If I inject the ILifetimeScope I have the ability to register "local" types and instances which in some cases are necessary:

using (var scope = mScope.BeginLifetimeScope(b => b.RegisterInstance<ILogger>(logger) ...)
{
   scope.ResolveKeyed<IInterface1>(Enum1)  //which takes an ILogger
}

If I inject the IIndex the data of the index now has to be a Func<ILogger, IInterface1> instead of just an IInterface1 and I have to do the following:

mFactory[Enum1](logger)

From what I have read it is better to use the IIndex as it does not use the ServiceLocator pattern. Is that true even if this case?

Also is there a way to register local variables (like above) and have the IIndex (mFactory) use that scope?

Thanks for all the help!

 Glenn
Reply all
Reply to author
Forward
0 new messages