MVVM &&国际奥委会子视图模型
我有一个 ViewModel,它在构造函数中采用两个相同类型的参数:
public class CustomerComparerViewModel
{
public CustomerComparerViewModel(CustomerViewModel customerViewModel1,
CustomerViewModel customerViewModel2)
{
}
}
public class CustomerViewModel
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
如果我没有使用 IOC,我可以新建视图模型并传递子视图模型。我可以将两个视图模型打包到一个类中并将其传递到构造函数中,但如果我有另一个只需要一个 CustomerViewModel 的视图模型,我将需要传递视图模型不需要的东西。
我该如何使用 IOC 来处理这个问题?顺便说一句,我正在使用 Ninject。
谢谢
I have a ViewModel, it takes two parameters in the constructor that are of the same type:
public class CustomerComparerViewModel
{
public CustomerComparerViewModel(CustomerViewModel customerViewModel1,
CustomerViewModel customerViewModel2)
{
}
}
public class CustomerViewModel
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
If I wasn't using IOC I could just new up the viewmodel and pass the sub-viewmodels in. I could package the two viewmodels into one class and pass that into the constructor but if I had another viewmodel that only needed one CustomerViewModel I would need to pass in something that the viewmodel does not need.
How do I go about dealing with this using IOC? I'm using Ninject btw.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不熟悉 Ninject,但在我看来,为了让 IoC 知道将哪些 CustomerViewModel 注入到构造函数中,您必须提前设置这些对象。使用 MEF 之类的属性和 Psuedo 代码,它可能看起来像......
I'm not familiar with Ninject, but it would seem to me that in order for the IoC to know what CustomerViewModels to Inject into your constructor you would have to setup these objects in advance. Using MEF like attributes and Psuedo code it might look something like...
以下是在 Ninject 中执行此操作的方法:
然后在您要使用它们的类的构造函数中:
Here's how to do it in Ninject:
Then in the constructor of the class you are using them in: