I'm a new user of Autofac.
I have a factorythat needs to create a different class based on the input to the factory method, but only one of the classes that needs to be created has other dependencies.
I found this answer: Autofac Binding at Runtime
whose solution worked well.
but then I started reading about Autofac's delegate factories (http://docs.autofac.org/en/latest/advanced/delegate-factories.html)... and now I'm confused.
It appears, if you use a delegate factory, then you don't actually have to write a factory class at all?
Here is a snipped from my current factory class:
public class ExternalUserInformationProviderFactory : IExternalUserInformationProviderFactory
{
private readonly IComponentContext autofacContainer;
public ExternalUserInformationProviderFactory(IComponentContext autofacContainer)
{
this.autofacContainer = autofacContainer;
}
public IProvideExternalUserInformation GetExternalUserInformationProvider(string loginProvider)
{
switch (loginProvider)
{
case "Google":
return autofacContainer.Resolve<GoogleExternalUserInformationProvider>();
case "Twitter":
return autofacContainer.Resolve<TwitterExternalUserInformationProvider>();
}
return null;
}
}
In this example, the TwitterExternalUserInformationProvider takes a dependency in its constructor:
public class TwitterExternalUserInformationProvider : IProvideExternalUserInformation
{
private readonly ITwitterRepository twitterRepository;
public TwitterExternalUserInformationProvider(ITwitterRepository twitterRepository)
{
this.twitterRepository = twitterRepository;
}
}
and the GoogleExternalUserInformationProvider takes no constructor args at all.
here is how I have this factory wired up in Startup.cs (I'm using asp.net core):
var containerBuilder = new ContainerBuilder();
containerBuilder.Register<IExternalUserInformationProviderFactory>(c => new ExternalUserInformationProviderFactory(c.Resolve<IComponentContext>()));
containerBuilder.RegisterType<TwitterExternalUserInformationProvider>();
containerBuilder.RegisterType<GoogleExternalUserInformationProvider>();
Autofac is smart enough to resolve the ITwitterRepository dependency for me, which is really cool.
Based on this current implementation, is it possible for me to use a delegate factory and get rid of the ExternalUserInformationProviderFactory altogether?
I'm curious.
Thanks