What is missing in MEF to compare with IoC containers?

MEF is not an IoC container . But it seems like it's almost an IoC container. It seems that I can easily make the MEF behave like an IoC container (see the example below), and not so much is missing that the MEF completely explodes the IoC container.

What are the actual functions missing in the MEF that StrucureMap, Unity, etc. should get steam?

Do you think this feaure query makes sense?

using System; using System.Collections.Generic; using System.ComponentModel.Composition; using System.ComponentModel.Composition.Hosting; using System.ComponentModel.Composition.Primitives; using System.Linq; ... private CompositionContainer container; public void Configure() { container = CompositionHost.Initialize( new AggregateCatalog( AssemblySource.Instance.Select( x => new AssemblyCatalog(x)).OfType<ComposablePartCatalog>())); var batch = new CompositionBatch(); batch.AddExportedValue<IWindowManager>(new WindowManager()); batch.AddExportedValue<IEventAggregator>(new EventAggregator()); batch.AddExportedValue(container); container.Compose(batch); } 
+6
source share
3 answers

As discussed, MEF was not built as an IoC container. However, it has a lot in common. If the MEF functionality provides enough for your IoC container needs, you can go ahead and use it as an IoC container.

While MEF probably never satisfies all the needs of an IoC container, in the next version we are making changes that I think will make it more appropriate to use it as an IoC container for more people. Here is some of them:

  • Optionally disable the failure to facilitate the diagnosis of errors in a system where you do not want anything to be rejected
  • Support for a convention model that allows you to register types that will be exported and imported, instead of adding export and import attributes for the types themselves.
  • Open general support so you can export an open movie and import a closed version.
  • Improved support for containers with scope / hierarchy, which can help in lifecycle management.

Thomas mentions interception as an important feature. We currently have no plans to include support for this out of the box. MefContrib has some support for this in the form of InterceptingCatalog, I suppose.

+3
source

What is missing in MEF to compare with IoC containers?

A way to easily diagnose problems in a composition.

For example, if you have a dependency chain A → B → C → D, and D is absent, then in accordance with the stable principle, MEF composition will simply mark A, B and C as inaccessible and try to make composition without these parts. Your error message will complain about A, not D.

In order to diagnose the problem, you will have to somehow dump the composition diagnostics (for example, using mefx ) and carefully follow the dependency path down until you find the actual problem. What a pain, but kind of works until you imagine cyclic dependencies ...

A regular IoC container does not have this problem. He will simply tell you: "Hey, you registered C for use in composition, but I cannot find my D dependency."

See also my blog post on this subject.

+4
source

I don't know about the implications of MEF, but I can say that MEF is a framework that addresses extensibility issues for applications. The focus is on enabling add-in scripts for standard software. It shares so many features with the “right IoC containers” that in the future it could become a full-fledged IoC container.

Mef was built for another purpose, and then for the IoC container. Its purpose is to provide a general framework for enabling add-on features for standard applications. From the perspective of a standard application, the add-in is essentially an unknown component .

When we use the IoC container as a tool for compiling the application, we know about the components that make up the application.

IoC containers are aimed at an untied set of services, which is good, but the developer must know about the components that he wants to compose at the time of configuring the container, when the MEF is aimed at detecting components. The only thing you should know is the abstraction that you want to use.

MEF shares so much in common with IoC containers that it’s sometimes difficult to say how we should take this into account.

I think the main disaventage of MEF:

  • poor lifecycle management compared to IoC
  • lack of interception

What are the main points when discussing IoC containers.

+3
source

Source: https://habr.com/ru/post/890751/


All Articles