开发者

MVC3 ImportMany on Constructor Parameter for Exported Controller

I have a controller that is exported using MEF and loaded by the Controller factory.

    [Export(Controller)]
    public class MyController : Controller
    {

        private IRepository MyRepsoitory;

        [ImportMany]
        public IEnumerable<MyImportedItem> TestImportItems {get;set;}

        public MyController([ImportMany]IEnumberable<MyImportedItem> items, [Import]IRepository repository)
        {
            // items here is always null
            // However if I grab the container that the ControllerFactory used and tell it ComposeParts on this the TestImportItems will be filled with 50+ items
            // repository however is instantiated appropriately. 

            GlobalItems.Container.ComposeParts(this);
            //Now TestImportItems if filled but my items parameter alway null... how do I get constructor to fill

        }

    }

So MEF creates MyController but only creates the repository and sends null for the ImportMany even though it can fill the property later with the same Container.

What's also odd is if I do something that breaks one of the items the creation of MyConroller breaks in ControllerFactory.. as if it checks that is has parts for the constructor but never pushes them to the IEnumerable parameter.

What am I missing?

Obviously I have the parts available if the same Container works for .ComposingParts on (this) (and I reflected the catalog which has appropriate import/export Parts available at time of creating the Controller.

I could rewrite my class to use the filled Property but I would really like my importing constructor to get a filled collection.

UPDATE:

If I add a simple wrapper class for the import many开发者_高级运维 MEF will load the [ImportMany] parameter.

So the following will fill the IEnumerable for me...

public MyController(TestImportClass test, [Import]IRepository repository)
{
    //test.Items != null
}

public class TestImportClass
{

    public IEnumberable<MyImportedItem> Items {get;set;}

    [ImportingConstructor]
    public TestImportClass([ImportMany]IEnumberable<MyImportedItem> items)
    {
        this.Items = items;
    }
}

I am using a "Convention" system in my actual code to mark the Controller for Export. Maybe for some reason that is causing MEF to not understand the Import on initial Constructor Parameter? If that were the case though i am not sure why my IRepository always gets filled?


When you call ComposeParts, you pass objects which have already been constructed. It's not possible to call the constructor again on an existing object. (And in this case if you did you'd end up with infinite recursion). So ComposeParts doesn't satisfy constructor imports.

If your controller is pulled from the container some other way, and you put an ImportingConstructorAttribute on the constructor, the constructor imports should be satisfied.


Probably the convention system you are using doesn't support ImportMany in constructor arguments. Presumably the convention isn't applying to TestImportClass which is why the ImportMany works on that constructor.

We plan to have official convention model support in the next version of MEF, and we should be shipping a new codeplex release with a preview of this support soon.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜