Autofac with open generics and type specified at runtime

The documentation states that Autofac supports open generics, and I can register and resolve in the base case as follows:

Check in:

builder.RegisterGeneric(typeof(PassThroughFlattener<>)) .As(typeof(IFlattener<>)) .ContainerScoped(); 

Resolve:

 var flattener = _container.Resolve<IFlattener<Address>>(); 

The above code works fine. However, assuming that I will not know the type provided by IFlattener before execution, I want to do something like this:

 object input = new Address(); var flattener = (IFlattener)_container.Resolve(typeof(IFlattener<>), new TypedParameter(typeof(IFlattener<>), input.GetType())); 

Is this possible with AutoFac? I got an idea from the following using StructureMap:

http://structuremap.sourceforge.net/Generics.htm

I am trying to achieve the same goal outlined in this article.

+6
dependency-injection ioc-container autofac
source share
2 answers

This is certainly possible with Autofac. At "check-in time" this is what you basically do:

  • Register Open Generic Type (PassThroughFlattener <>)
  • Register any specific types (AddressFlattener)
  • Register a method that you can use to resolve IFlattener based on the input object

At the "resolution time" you run:

  • Allow Method
  • Calling a method with input parameters to enable IFlattener implementation

Here (hopefully) a working sample:

 var openType = typeof(IFlattener<>); var builder = new ContainerBuilder(); builder.RegisterGeneric(typeof(PassThroughFlattener<>)).As(openType); builder.Register<AddressFlattener>().As<IFlattener<Address>>(); builder.Register<Func<object, IFlattener>>(context => theObject => { var concreteType = openType.MakeGenericType(theObject.GetType()); return (IFlattener) context.Resolve(concreteType, new PositionalParameter(0, theObject)); }); var c = builder.Build(); var factory = c.Resolve<Func<object, IFlattener>>(); var address = new Address(); var addressService = factory(address); Assert.That(addressService, Is.InstanceOfType(typeof(AddressFlattener))); var anything = "any other data"; var anyService = factory(anything); Assert.That(anyService, Is.InstanceOfType(typeof(PassThroughFlattener<string>))); 
+9
source share

If you do not know the type before execution, you can create it using MakeGenericType:

 var addressFlattener = _container.Resolve(typeof(IFlattener<>).MakeGenericType(typeof(Address))); 
+4
source share

All Articles