Search code examples
autofacmef

How can one generate Autofac registration code for dependencies expressed with MEF attributes?


We have a lot of .NET Framework projects using MEF 1, i.e. having all kinds of Import/Export/... attributes.

I know I can use the Autofac.Mef extension, but I would like to move away from MEF completely and express the dependencies explicitly in Autofac. But we have thousands of classes using MEF. And hundreds of projects.

I am planning to use Autofac modules and I am thinking to auto generate module per project, where each module would register the components introduced by the respective project. Then teams could merge the modules of projects they own. Or keep them separate. Whatever makes sense to them.

So the question becomes - given a C# project using MEF as IoC is there an Autofac registration code generator capturing the same IoC semantics? We can write our own, but I feel something like that should exist already.

EDIT 1

We use MEF mostly as Dependency Injection engine. Meaning that in 99.9% of the cases the exports are classes and interfaces and they are imported into constructors and properties. We do use Lazy and open generics injection and they are fully supported by Autofac, but not by Autofac.Mef - https://autofac.readthedocs.io/en/latest/integration/mef.html#known-issues-gotchas


Solution

  • This won't be a great answer because it's impossible to prove a negative... But I'd bet such a thing doesn't exist because people just use Autofac.Mef.

    MEF registrations are kind of complex with their mixture of metadata and specified dependencies. I'd wager most folks would rather just use the Autofac.Mef stuff than try to code-generate the equivalent manual registrations.