John Spiegel
John Spiegel

Reputation: 1903

MEF vs. Reflection

I finally started toying around with MEF only to find it was less straightforward to solve my flagship problem.

Problem: I have a WCF service that relies on some custom algorithms each living in their own assemblies. When a method is called, part of the request's data contract will identify which algorithms are needed. For example class One and class Two each implement a NameFile() method while classes A and B each implement EncryptFile().

One call to the service's WriteFile() may need to call Two.NameFile and A.EncryptFile, while another may need One.NameFile and B.EncryptFile.

Attempts: MEF (I'm new to it) seemed a nice way to load up assemblies but what I've found so far seems as though I need to go through a number of steps to determine at runtime which of the available contract-meeting assemblies fits the need. Plain Old Reflection seems to be a more straightforward way to decide and load when the specific need is only known during a call.

Question Is MEF indeed more suited to other dynamic loading scenarios or is it just my MEF noobism?

Thanks,

John

Upvotes: 4

Views: 2644

Answers (1)

Reed Copsey
Reed Copsey

Reputation: 564403

Is MEF indeed more suited to other dynamic loading scenarios or is it just my MEF noobism?

Well, the main issue is determining, at runtime, which of the options which fulfill the contract you wish to use.

This determination will have to occur no matter what you choose - whether it's MEF, some other injection mechanism, or rolling your own version with reflection.

MEF should still make this simpler than rolling your own version, however, as it's easy to use [ImportMany] to import all IFileEncryptor (or whatever) implementations from all available assemblies. At that point, determining the correct one from the list of available should be fairly simple.

While, using reflection, you could just go grab the specific type from the specific assembly - that really defeats the purpose of doing this dynamically. MEF makes it easy to extend this service without recompilation or code changes, since you can drop a new assembly in place and have it automatically discoverable.

Upvotes: 4

Related Questions