带有 Unity Container 的 WPF - 如何注册 ViewModel 并将其解析为 View [英] WPF with Unity Container - How to register and resolve ViewModels to Views

查看:22
本文介绍了带有 Unity Container 的 WPF - 如何注册 ViewModel 并将其解析为 View的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 WPF MVVM 应用程序中使用 Unity 容器.我没有使用过棱镜,因为它似乎很重.这是应用程序结构.我想弄清楚如何将视图解析为 ViewModel 和视图模型(服务)的依赖项.

Hi I am trying to use Unity container in WPF MVVM application. I have not used Prism as it seems to heavy. Here is the application structure. I am trying to figure out how to resolve Views to ViewModels and dependencies of the view models (services).

观看次数

MainWindow.xaml
CustomerList.xaml
CustomerDetail.xaml
BookList.xaml
BookDetail.xaml

视图模型

MainViewModel

CustomerListViewModel

BoolListViewModel

BookDetailViewModel

CustomerDetailViewModel

图书馆

ICustomerService (AddCustomer, SaveCustomer, GetCustomers, GetCustomer)

CustomerService:ICustomerService

IBookService (GetBooks, GetBook)

BookService:IBookService

IBookReserveService(Reserve, Return)

BookReserveService:IBookReserveService

  1. MainViewModel 需要引用 ICustomerService 和 IBookService

  1. MainViewModel needs reference to ICustomerService, and IBookService

CustomerListViewModel 需要引用 ICustomerService

CustomerListViewModel needs reference to ICustomerService

BoolListViewModel 需要引用 IBookService

BoolListViewModel needs reference to IBookService

BookDetailViewModel 需要引用 ICustomerService 和 IBookReserveService

BookDetailViewModel needs reference to ICustomerService, and IBookReserveService

CustomerDetailViewModel 需要引用 ICustomerService 和 IBookReserveService

CustomerDetailViewModel needs reference to ICustomerService, and IBookReserveService

我在每个视图模型中都有服务的 getter setter 属性.

I have getter setter property for services in each viewmodels.

我遇到了关于如何将依赖注入与 WPF 结合使用的问题,尤其是对于视图和视图模型.我尝试使用 Unity 在一个运行良好的控制台应用程序中进行注册和解析.但我想要一些关于如何为 WPF 应用程序完成此操作的想法.我试过注册

I am running into issues on how do I use Dependency Injection with WPF especially for Views and ViewModel. I tried with Unity to register and resolve in a console application which is working fine. But I would like some ideas on how this could be done for WPF app. I tried registering

 container.RegisterType<ICustomerService, CustomerService>()
 container.RegisterType<IBookReserveService, BookReserveService>()
 container.RegisterType<IBookService, BookService>()

并使用解决它container.Resolve();

and resolve it using container.Resolve();

但我不确定如何判断哪个视图必须使用哪个视图模型并在需要时而不是在应用程序启动时解决它们.另外,我不解决应用程序启动中的所有映射.选择菜单(选择客户查看详细信息、选择书籍查看详细信息、保存客户、预订书籍等)时应执行此操作.

But I was not sure how I could tell which view must use which view model and resolve them when required and not when the app starts. Also, I dont to resolve all mapping in the app start. It should be done when the menu (Selecting a customer to view detail, selecting a book to view detail, save customer, reserve book etc.) is selected.

我阅读的大部分内容都想使用 IView 和 IViewModel.但不确定我是否理解其中的优势.

Mostly what I read wanted to use IView and IViewModel. But not sure I understood the advantage in it.

非常感谢任何帮助.

推荐答案

这是您可以执行此操作的一种方法.首先,像这样在 Unity 中注册您的视图模型和服务:

Here's one way you can do this. First, register your view-models and services with Unity like this:

// Unity is the container
_container.RegisterType<IMainViewModel, MainViewModel>();
_container.RegisterType<IBookService, BookService>();

其次,将视图的 DataContext 设置为视图构造函数中的视图模型,如下所示:

Second, set your view's DataContext to a view-model in the view's constructor like this:

public partial class MainView:UserControl
{
   private readonly IUnityContainer _container;

   public MainView(IUnityContainer container)
        {
            InitializeComponent();
            _container = container;   
            this.DataContext = _container.Resolve<IMainViewModel>();            
        }      
}

第三,您需要将您的服务注入到您的视图模型中:

Third, you'll need to inject your services into your view-models:

public MainViewModel(ICustomerService custService, IBookService bookService ){}

还有其他方法可以使用 .config 文件等来做到这一点,但这应该足以让您开始使用,如果您需要更多,请告诉我.你问DI的优点是什么,我觉得有很多.仅举几个例子:促进组件之间的松散耦合并提高可测试性.我觉得这是良好设计/实施的关键之一.

There are other ways to do this using .config files, etc. but this should give you enough to get started, let me know if you need more. You asked what the advantages of DI are and there are many, I feel. Just to name a couple: promotes loose-coupling between your components and improved testability. I feel it's one of the lynch-pins to a good design/implementation.

更新:

Unity >=3,如果你遵循这样的命名约定,你可以跳过容器注册:

With Unity >=3, you can skip the container registration if you follow the naming convention like this:

// This will register all types with a ISample/Sample naming convention 
            container.RegisterTypes(
                AllClasses.FromLoadedAssemblies(),
                WithMappings.FromMatchingInterface,
                WithName.Default);

这篇关于带有 Unity Container 的 WPF - 如何注册 ViewModel 并将其解析为 View的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆