MVVM &&国际奥委会子视图模型

发布于 2024-09-02 02:27:00 字数 574 浏览 7 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

晨光如昨 2024-09-09 02:27:00

我不熟悉 Ninject,但在我看来,为了让 IoC 知道将哪些 CustomerViewModel 注入到构造函数中,您必须提前设置这些对象。使用 MEF 之类的属性和 Psuedo 代码,它可能看起来像......

[Export()]
public class CustomerSelectorViewModel
{
    [Export("CustomerA")]
    public class CustomerViewModel FirstSelection {get;set;}

    [Export("CustomerB")]
    public class CustomerViewModel SecondSelection {get;set;} 
}

[Export()]
public class CustomerComparerViewModel
{
    [ImportingConstructor]
    public CustomerComparerViewModel([Import("CustomerA")]CustomerViewModel customerViewModel1, [Import("CustomerB")]CustomerViewModel customerViewModel2)
    {

    }
}

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...

[Export()]
public class CustomerSelectorViewModel
{
    [Export("CustomerA")]
    public class CustomerViewModel FirstSelection {get;set;}

    [Export("CustomerB")]
    public class CustomerViewModel SecondSelection {get;set;} 
}

[Export()]
public class CustomerComparerViewModel
{
    [ImportingConstructor]
    public CustomerComparerViewModel([Import("CustomerA")]CustomerViewModel customerViewModel1, [Import("CustomerB")]CustomerViewModel customerViewModel2)
    {

    }
}
请帮我爱他 2024-09-09 02:27:00

以下是在 Ninject 中执行此操作的方法:

Container.Bind<CustomerViewModel>().ToSelf().WhenTargetHas<CustomerA>();
Container.Bind<CustomerViewModel>().ToSelf().WhenTargetHas<CustomerB>();

然后在您要使用它们的类的构造函数中:

public class CustomerComparerViewModel
{
    public CustomerComparerViewModel([CustomerA]CustomerViewModel customerA,
                                     [CustomerB]CustomerViewModel customerB)
    {

    }
}

Here's how to do it in Ninject:

Container.Bind<CustomerViewModel>().ToSelf().WhenTargetHas<CustomerA>();
Container.Bind<CustomerViewModel>().ToSelf().WhenTargetHas<CustomerB>();

Then in the constructor of the class you are using them in:

public class CustomerComparerViewModel
{
    public CustomerComparerViewModel([CustomerA]CustomerViewModel customerA,
                                     [CustomerB]CustomerViewModel customerB)
    {

    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文