Posts
1150
Comments
891
Trackbacks
1
How do you deal with open generics and either Autofac or StructureMap?

For the longest time, I’ve ‘hard-wired’ my registrations using StructureMap, and I’m trying to do an auto registry type of thing and it just isn’t working, because I’m old and stupid.

I’m trying both Autofac and StructureMap, here’s the Autofac version.

I have an interface:

  public interface Handles<in T> where T : ICommand

I do:

   var assembly = Assembly.GetCallingAssembly();

Then:

  builder.RegisterAssemblyTypes(assembly)
               .AsClosedTypesOf(typeof(Handles<>))
               .AsImplementedInterfaces();

This seems to work (as I can look at my registrations and see stuff), and I can successfully do things like this:

  var cmdHandler = container.Resolve<Handles<MyCommand>>();

Where MyCommand is a class that has a handler.  Resolves just fine.

However, if I try this (because I want to get all of my handlers so that I can do stuff with them in a loop):

  var group = container.Resolve(typeof(Handles<>));

I get an exception, because it says there is nothing registered.

Normally Google (and StackOverflow) is my friend, but nothing I try seems to work.

I’m hoping to have something to update here later, because I know I’ll need it again.

posted on Monday, December 22, 2014 7:26 PM Print
Comments
No comments posted yet.

Post Comment

Title *
Name *
Email
Url
Comment *  
Please add 8 and 4 and type the answer here: