Choosing between MEF and MAF (System.AddIn)

前端 未结 7 2162
伪装坚强ぢ
伪装坚强ぢ 2020-11-22 10:05

The Managed Extensibility Framework (MEF) and Managed AddIn Framework (MAF, aka System.AddIn) seem to accomplish very similar tasks. According to this Stack Overflow questio

7条回答
  •  一整个雨季
    2020-11-22 10:54

    In my opinion, the best way to discover the differences is some hands-on code. I found two MSDN walkthroughs, both with a calculator example so you can easily compare their implementations:

    MEF: Simple calculator example using MEF parts
    (Managed Extensibility Framework)

    • Shows how to build up a simple calculator using MEF technology. Does not show how to load external dlls. (But you can simply modify the example by using catalog.Catalogs.Add(new DirectoryCatalog("Plugins", "*.dll")); instead of using catalog.Catalogs.Add(new AssemblyCatalog(typeof(Program).Assembly)); and extract the calculator code and contract to separate DLL projects.)
    • MEF does not need to have a specific directory structure, it is simple and straightforward to use, even for small projects. It works with attributes, to declare what is exported, which is easy to read and understand. Example: [Export(typeof(IOperation))] [ExportMetadata("Symbol", '+')] class Add: IOperation { public int Operate(int left, int right) { return left + right; } }

    • MEF does not automatically deal with versioning

    MAF: Simple calculator with V1 and V2 version MAF plugins
    (Managed Addin Framework)

    • Shows how to build up the calculator by using a V1 plugin and then how to move over to a V2 plugin while maintaining backwards-compatibility (note: you can find the V2 version of the plugin here, the link in the original article is broken)
    • MAF enforces a specific directory structure, and it needs a lot of boilerplate code to make it work, hence I don't recommend it for small projects. Example:
      Pipeline
        AddIns
          CalcV1
          CalcV2
        AddInSideAdapters
        AddInViews
        Contracts
        HostSideAdapters
      

    Both MEF and MAF are included in the .NET Framework 4.x. If you compare the two examples you will notice that the MAF plugins have a lot more complexity compared with the MEF framework - so you need to think carefully when to use which of those frameworks.

提交回复
热议问题