CreationPolicy 的 MEF 导入问题
下面是我的问题的演示, 我想创建许多引用其父母的孩子。
如何编写导入属性来获取父引用而不是创建新的父实例?
public partial class MainPage : UserControl
{
[Import(typeof(Parent))]
public Parent Parent1 { get; set; }
[Import(typeof(Parent))]
public Parent Parent2 { get; set; }
public MainPage()
{
InitializeComponent();
CompositionInitializer.SatisfyImports(this);
Parent1.name = "p1";
Parent2.name = "p2";
}
}
[PartCreationPolicy(CreationPolicy.NonShared)]
[Export(typeof(Parent))]
public class Parent
{
[Import(typeof(Child))]
public Child Child1 { get; set; }
[Import(typeof(Child))]
public Child Child2 { get; set; }
public string name;
}
[PartCreationPolicy(CreationPolicy.NonShared)]
[Export(typeof(Child))]
public class Child
{
//how to write the import attribute
public Parent Parent { get; set; }
public string name;
}
below is the demo of my problem,
I'd like to create many child which have a reference to their parent.
How to write the import attribute to get the parent reference instead of create a new parent instance?
public partial class MainPage : UserControl
{
[Import(typeof(Parent))]
public Parent Parent1 { get; set; }
[Import(typeof(Parent))]
public Parent Parent2 { get; set; }
public MainPage()
{
InitializeComponent();
CompositionInitializer.SatisfyImports(this);
Parent1.name = "p1";
Parent2.name = "p2";
}
}
[PartCreationPolicy(CreationPolicy.NonShared)]
[Export(typeof(Parent))]
public class Parent
{
[Import(typeof(Child))]
public Child Child1 { get; set; }
[Import(typeof(Child))]
public Child Child2 { get; set; }
public string name;
}
[PartCreationPolicy(CreationPolicy.NonShared)]
[Export(typeof(Child))]
public class Child
{
//how to write the import attribute
public Parent Parent { get; set; }
public string name;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
MEF 最适合将单独的模块链接在一起。将本应位于同一逻辑层次结构中的对象组合起来有点矫枉过正。
我会修饰
Child
属性来设置父级:这样,每当导入满足创建
Parent
类的子级时,Child
类就会还设置了其Parent
引用。这种相互链接参考的方式是 MEF 友好且相当标准的。只是不要在两个方向上都这样做(如果您有一对一的关系),因为您会陷入无限循环。您似乎在其设计权限之外使用 MEF。在 MEF 中,对于每个合约名称,您可以有一个单例引用或多个单一类型的引用。 MEF 拥有的唯一上下文是合约名称(或合约类型的名称)。
我希望这有帮助。
MEF is best for linking together separate modules. Its a little bit overkill to combine an object that is meant to be within the same logical hierarchy.
I would dress up the
Child
property to set the parent:This way whenever imports are satisfied to create the
Parent
class's children, theChild
classes will have theirParent
references set as well. This way of interlinking references is MEF friendly and pretty standard. Just don't do it in both directions (if you have a one-to-one relationship) because you'll get an infinite loop.You seem to be using MEF outside of its designed purview. In MEF, for each contract name you can have one singleton reference or many references of a single type. The only context that MEF has is the contract name (or name of the contract type).
I hope this helps.
如果我理解你的问题,你不能在这里进行导入,因为 MEF 没有你在这里寻找的上下文来执行此操作。我的建议是在父级中导入的子级上设置 Child.Parent 属性。也许在 Child1/Child2 setter 中只需将 Parent 属性设置为此即可。
If I understand your question you cannot do an Import here because MEF does not have the context you are looking for here to do it. My suggestion would be to set the Child.Parent property on the Child that gets imported in the Parent. Perhaps in the Child1/Child2 setter just set the Parent property to this.
您可能会遇到问题,因为 MEF 中的组合机制是递归的,它会遍历所有要绘制图形的对象。您在这里所做的就是尝试将一个项目导入另一个项目,其中另一个项目将尝试导入第一种类型。您将看到的另一个问题是您指定了 NonShared 的 CreationPolicy,这意味着将为每个导入创建一个新实例。与递归问题相结合,这是一场完美的内存/性能灾难等待发生;)
我要做的就是将父级的 CreationPolicy 更改为共享,这样它就不会被再次组合,并且可以通过导入进行分配孩子身上的属性。这样做的缺点是您的 Parent1 和 Parent2 属性实际上是 Parent 的同一个实例。
You'll likely run into problems because the composition mechanism in MEF is recursive, it goes through all the objects to be graphed. What you're doing here is attempting to import one item into another, where the other will attempt to import the first type. The other issue you will be seeing is that you're specifying a CreationPolicy of NonShared, which means a new instance will be created for each of the imports. Combining that with the recursion problem, its a perfect memory/performance disaster waiting to happen ;)
What I would do, is change the Parent's CreationPolicy to Shared, that way it won't be composed a second time and can be assigned through the Import attribute in a child. The downside to that is that your Parent1 and Parent2 properties will in fact be the same instance of Parent.