棱镜,连接视图和的ViewModels与团结,试图了解它 [英] Prism, connecting Views and ViewModels with Unity, trying to understand it

查看:116
本文介绍了棱镜,连接视图和的ViewModels与团结,试图了解它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


创建视图和视图模型使用Unity



使用Unity作为依赖注入容器类似于使用
MEF,两者性质和基于构造方法注入是支持
。的主要区别在于,类型通常
。在运行时不隐发现;相反,他们必须用容器注册



通常,您定义的视图模型的接口,这样的看法
型的特定的具体类型可以从视图中分离出来。对于
为例,该视图可以通过
构造函数的参数定义其上视图模型的依赖,如下图所示。 C#



 公共QuestionnaireView()
{
的InitializeComponent();
}

公共QuestionnaireView(QuestionnaireViewModel视图模型):这个()
{
this.DataContext =视图模型;
}



默认参数较少
构造是必要的,以使图在设计时
的工具,如Visual Studio和Expression Blend工作。



另外,你可以在$定义了一个只写视图模型属性b $ b观看,如下图所示。统一将实例化所需的视图模型
,并调用属性setter视图实例化后。 C#



 公共QuestionnaireView()
{
的InitializeComponent();
}

[依赖性]
公共QuestionnaireViewModel视图模型
{
集合{this.DataContext =价值; }
}



视图模型类型与团结容器中注册,如图所示
在这里。 C#



  IUnityContainer容器; 
container.RegisterType< QuestionnaireViewModel>();



的视图然后可通过容器实例,如下所示
。 C#



  IUnityContainer容器; 
VAR视图= container.Resolve< QuestionnaireView>();





  1. 如果我离开了关于注册视图模型和实例视图,只需使用任何挂钩视图模型来查看这里(使用构造函数或使用属性)的两种方法的代码的最后一部分似乎视图模型并查看它似乎一切是工作的罚款。那么,什么是需要的代码注册视图模型和实例查看?


  2. 第一个例子,挂钩查看和使用构造视图模型,并没有提及所有的团结,所以团结实际上是被用在这里?


  3. 有没有使用基于属性的注射过基于construtor注入的任何好处或者是他们完全同样的事情?


  4. 文的第一部分说:*通常情况下,您定义的视图模型的接口使视图模型的特定的具体类型可以脱钩从视图,然后给出了一个例子。然而,这个例子中只字不提接口都没有。这到底是怎么回事,我失去的东西吗?



解决方案

要回答问题1安培; 4



在您的例子中,视图模型的类型是 QuestionnaireViewModel ,这是一个具体的类。因为它是一个具体的类,当你使用 container.Resolve<解决的观点; QuestionnaireView>(),统一将通过调用实例为您的视图模型容器.Resolve< QuestionnaireViewModel方式>()幕后



在这种情况下,注册您的视图模型是多余的。但是,使用依赖注入时,你通常要使用接口而不是类工作​​,所以你的构造是这样的:

 公开QuestionnaireView(IQuestionnaireViewModel视图模型)
{
this.DataContext =视图模型;
}

现在,你的构造函数接受一个接口,而不是一个类作为参数,团结不知道你想使用该接口的实现。说句团结这一点,你需要你的视图模型注册到容器:

  container.RegisterType< IQuestionnaireViewModel,QuestionnaireViewModel>(); 



所以现在当您解决您的视图,统一将查找哪个类应该作为一个实现使用 IQuestionnaireViewModel ,看到它的 QuestionnaireViewModel 并使用它。



要回答的问题2



正在使用统一的,因为为了构造函数来获得它的参数,你需要解决使用容器的看法。如果实例自己使用新QuestionnaireView(),即会出现没有构造函数或财产注入的观点统一不被使用。



要回答的问题3



我觉得它主要是一个什么样的更舒适的一个问题,在您需要使用注射的成员。很多时候,你只是想设置在构造函数中的局部变量,而不是仅仅用于执行注射创建属性。



有关财产注射一件好事,虽然是事实上,你可以使用 container.BuildUp()方法通过使用,而不是<中创建的实例code> container.Resolve<>()。通过这种方式,你甚至可以在创建后注入会员加入性能 - 这是你不能用构造函数注入做


Creating the View and View Model Using Unity

Using Unity as your dependency injection container is similar to using MEF, and both property-based and constructor-based injection are supported. The principal difference is that the types are typically not implicitly discovered at run time; instead, they have to be registered with the container.

Typically, you define an interface on the view model so the view model's specific concrete type can be decoupled from the view. For example, the view can define its dependency on the view model via a constructor argument, as shown here. C#

public QuestionnaireView() 
{
    InitializeComponent(); 
}

public QuestionnaireView(QuestionnaireViewModel viewModel) : this() 
{
    this.DataContext = viewModel;
}

The default parameter-less constructor is necessary to allow the view to work in design-time tools, such as Visual Studio and Expression Blend.

Alternatively, you can define a write-only view model property on the view, as shown here. Unity will instantiate the required view model and call the property setter after the view is instantiated. C#

public QuestionnaireView() 
{
    InitializeComponent(); 
}

[Dependency] 
public QuestionnaireViewModel ViewModel 
{
    set { this.DataContext = value; } 
}

The view model type is registered with the Unity container, as shown here. C#

IUnityContainer container;
container.RegisterType<QuestionnaireViewModel>();

The view can then be instantiated through the container, as shown here. C#

IUnityContainer container;
var view = container.Resolve<QuestionnaireView>();

  1. If I leave out the last part of the code regarding registering the ViewModel and instantiating the View, and just use either of the two methods of hooking the ViewModel to the View here (using a constructor or using a property) it seems the ViewModel and View it seems everything is working fine. So what is the need for the code registering the ViewModel and instantiating the View?

  2. The first example, hooking the View and ViewModel using a constructor, has no mention of Unity of all, so is Unity actually being used here?

  3. Are there any advantages of using property-based injection over construtor based injection or are they exactly the same thing?

  4. The first part of the text says "*Typically, you define an interface on the view model so the view model's specific concrete type can be decoupled from the view", and then gives an example. Yet this example makes no mention of interfaces at all. What is going on here, am I missing something?

解决方案

To answer questions 1 & 4

In your example, the viewmodel is of type QuestionnaireViewModel, which is a concrete class. Since it's a concrete class, when you resolve the view using container.Resolve<QuestionnaireView>(), unity will instantiate the viewmodel for you by calling container.Resolve<QuestionnaireViewModel>() behind the scenes.

In this case, registering your viewmodel is redundant. However, when using dependency injection you usually want to work with interfaces rather than classes, so your constructor would look like this:

public QuestionnaireView(IQuestionnaireViewModel viewModel)
{
    this.DataContext = viewModel;
}

Now that your constructor receives an interface rather than a class as a parameter, Unity doesn't know which implementation of the interface you'd like to use. To tell Unity that, you need to register your viewmodel to the container:

container.RegisterType<IQuestionnaireViewModel, QuestionnaireViewModel>();

so now when you resolve your view, Unity will look up which class it should use as an implementation of IQuestionnaireViewModel, see that it's QuestionnaireViewModel and use it.

To answer question 2

Unity is being used, since in order for the constructor to get its parameters, you need to resolve the view using the container. Unity is not used if you instantiate the view yourself using new QuestionnaireView(), i.e. no constructor or property injection will occur.

To answer question 3

I think it's mostly a matter of what's more comfortable and where you need to use the injected members. A lot of times you just want to set a local variable in the constructor and not create a property just for performing the injection.

One good thing about property-injection though is the fact that you can use the container.BuildUp() method for instances that were created by using new rather than container.Resolve<>(). This way, you can inject members into properties even after creation - which is something you can't do with constructor injection.

这篇关于棱镜,连接视图和的ViewModels与团结,试图了解它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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