如何使用依赖注入在主从视图中创建 ViewModel

发布于 2024-12-01 13:48:54 字数 1217 浏览 0 评论 0原文

我正在使用 Jounce 为我的 MVVM 构建 Silverlight 应用程序。我有一个 CustomerListViewModel (复数),其中包含 CustomerViewModel 对象(单个)的集合。

我使用 Ninject 进行依赖注入,因为我的 ViewModel 将依赖于其他类(即存储库、服务等)。

使用依赖注入相当简单,但现在我有点卡住了。当 CustomerListViewModel 加载时,它将访问数据库(它已经通过 DI 拥有其存储库)并获取 Customer 对象。这些应该传递给 CustomerViewModel。

我将如何构建这些 CustomerViewModel 对象?我一直读到服务定位器模式是一种反模式,所以这感觉是错误的:

    private void GetCustomerss()
    {
        var customers = _customerRepository.GetAll();
        IList<CustomerViewModel> customerViewModels = new List<CustomerViewModel>();
        foreach (var customer in customers)
        {
            var customerViewModel = ObjectFactory.GetInstance<CustomerViewModel>();
            customerViewModel.Model = customer;
            customerViewModel.Add(customerViewModel);
        }
        Customers = new ObservableCollection<CustomerViewModel>(customerViewModels);
    }

我怎样才能避免这种反模式?或者说事情真的没有那么糟糕吗?

这也使我的单元测试变得更加困难,因为我可以将模拟 ICustomerRepository 注入 CustomerListViewModel (在构造函数中),但是 ObjectFactory.GetInstance() 将按其应有的方式工作,并且解决 CustomerViewModel 的底层依赖关系。这将会失败,因为我还没有为这些底层依赖项设置 Ninject。

I'm building a Silverlight app using Jounce for my MVVM. I have a CustomerListViewModel (plural) which has a collection of CustomerViewModel objects (single).

I'm using Ninject for dependency injection, because my ViewModels will be depending on other classes (i.e. repositories, services, etc.).

Using the dependency injection is fairly easy, but now I'm a little stuck. When the CustomerListViewModel loads, it will go to the database (it already has its repository through DI) and get the Customer objects. These should be passed on to a CustomerViewModel.

How would I go about constructing these CustomerViewModel objects? I've always read that the Service Locator pattern is an anti-pattern, so this feels wrong:

    private void GetCustomerss()
    {
        var customers = _customerRepository.GetAll();
        IList<CustomerViewModel> customerViewModels = new List<CustomerViewModel>();
        foreach (var customer in customers)
        {
            var customerViewModel = ObjectFactory.GetInstance<CustomerViewModel>();
            customerViewModel.Model = customer;
            customerViewModel.Add(customerViewModel);
        }
        Customers = new ObservableCollection<CustomerViewModel>(customerViewModels);
    }

How could I avoid this anti-pattern? Or is it really not that bad?

This also makes my unittesting a little harder, because I can inject a mock ICustomerRepository into the CustomerListViewModel (in the constructor), but the ObjectFactory.GetInstance<CustomerViewModel>() will work as it should, and also resolve underlying dependencies of CustomerViewModel. This will then fail, because I haven't set up Ninject for these underlying dependencies.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

小伙你站住 2024-12-08 13:48:54

在这里我描述了我如何处理这种情况: http://pglazkov .blogspot.com/2011/04/mvvm-with-mef-viewmodelfactory.html。是关于MEF的,但是想法是一样的。

基本上,您可以拥有一个名为 IViewModelFactory 的单独服务,您将使用它创建子视图模型。对于单元测试,您将能够模拟该服务。

Here I described how I dealt with such scenario: http://pglazkov.blogspot.com/2011/04/mvvm-with-mef-viewmodelfactory.html. It is about MEF, but the idea is the same.

Basically you can have a separate service called IViewModelFactory, using which you will create child view models. For unit tests you will be able to mock that service.

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