I'm using Autofac to create one of my classes inside of a static factory method Create
. This code is working and I haven't noticed any side effects yet. Admittedly, I've only used it in unit-tests so far but I'm wondering whether there could be any issues with it later when I take it into production code?
public static ICommandLineExecutor Create(ILogger logger)
{
var containerBuilder = new ContainerBuilder();
containerBuilder
.RegisterType<CommandLineTokenizer>()
.As<ICommandLineTokenizer>();
containerBuilder
.RegisterType<CommandLineParser>()
.As<ICommandLineParser>();
containerBuilder
.RegisterType<CommandParameterFactory>()
.WithParameter(new TypedParameter(typeof(TypeConverter), CommandParameterFactory.DefaultConverter))
.As<ICommandParameterFactory>();
containerBuilder
.RegisterType<CommandLineExecutor>()
.WithParameter(new TypedParameter(typeof(ILogger), logger))
.As<ICommandLineExecutor>();
using (var container = containerBuilder.Build())
using (var scope = container.BeginLifetimeScope())
{
return scope.Resolve<ICommandLineExecutor>();
}
}