MVC3 ImportMany 导出控制器的构造函数参数
我有一个使用 MEF 导出并由控制器工厂加载的控制器。
[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
}
}
因此,MEF 创建 MyController,但仅创建存储库并为 ImportMany 发送 null,即使它稍后可以使用相同的 Container 填充该属性。
同样奇怪的是,如果我做的事情破坏了 ControllerFactory 中 MyConroller 的创建中断的项目之一。就好像它检查是否有构造函数的部分,但从不将它们推送到 IEnumerable 参数。
我缺少什么?
显然,如果同一个容器适用于(此)上的 .CompositingParts,我就有可用的部件(并且我反映了在创建控制器时具有可用的适当导入/导出部件的目录。
我可以重写我的类以使用填充的属性,但是我真的希望我的导入构造函数能够获得一个填充的集合
更新:
如果我为导入添加一个简单的包装类,许多 MEF 将加载 [ImportMany] 参数,
因此以下内容将为我填充 IEnumerable...
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;
}
}
我正在使用。我的实际代码中是否有一个“约定”系统来标记导出控制器。也许由于某种原因导致 MEF 无法理解初始构造函数参数上的导入?如果是这种情况,但我不确定为什么我的 IRepository 总是被填满。 ?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您调用 ComposeParts 时,您传递已经构造的对象。无法在现有对象上再次调用构造函数。 (在这种情况下,如果你这样做,你最终会得到无限递归)。所以 ComposeParts 不满足构造函数导入。
如果您的控制器以其他方式从容器中拉出,并且您在构造函数上放置了 ImportingConstructorAttribute,则应该满足构造函数导入。
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.
您使用的约定系统可能不支持构造函数参数中的 ImportMany 。据推测,该约定不适用于 TestImportClass,这就是 ImportMany 在该构造函数上工作的原因。
我们计划在 MEF 的下一版本中提供官方约定模型支持,并且我们应该很快就会发布一个新的 Codeplex 版本,并提供此支持的预览版。
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.