tags:

views:

26

answers:

1
+1  Q: 

Customizing MEF

Hey fellow coders,

I have this situation where I want to use MEF in a chess project I'm workin on. Let's say I have a class constructor as in:

public class MoveManager { private Piece _piece;

public MoveManager(Piece piece) { _piece = piece; } Mode code here... }

In this context I would have several classes that would derive from Piece like, Pawn, Rook, etc. If I put export attributes on all the classes the derive from Piece, the object being passed into the constructor is null. MEF loops through all classes the have the [Export(typeof(Piece))] and if it exceeds 1, it passes in null. So I cannot use MEF in this way. I'm going to use an Abstact Factory for getting the correct piece. Seems like the DI part of MEF can only take a single class that has the [Export(typeof(some base class))].

Can anyone shed some light on this?

Thanks,

David W.

+1  A: 

A couple clarifications if you export multiple objects under the same contract and try to import a single one MEF should throw an error not actually give you null (unless you set the AllowDefault=true option on the Import which I'm assuming you didn't). Also MEF allows for you to import all by using ImportMany into a collection.

In the context of a chess game I'm not sure what you are trying to accomplish by exporting the pieces because there are a fixed number of pieces which is not extensible, further more you need to have different numbers of each type of piece. So for at least this part of the game I'm not sure MEF or any DI framework is very useful for you. Perhaps if you can explain more of what you want to accomplish we can try to point you in the right direction.

Wes Haggard