统一初始化分层树视图模型 [英] Initialize hierarchical tree viewmodels with unity

查看:46
本文介绍了统一初始化分层树视图模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在调查在我们的应用程序中使用 Prism 的更改.目前我正在努力进行分层结构的初始化.

I'm investigating a change in our application to use Prism. At the moment I'm struggling with the initialization of hierarchical structures.

基本上我有一个其他类继承的基类,简化如下:

Basically I have a base class of which other classes inherit, simplified like this:

public class NodeViewModel : INodeViewModel
{
    Node Node { get; private set; }
    public ObservableCollection<INodeViewModel> ChildNodeViewModels { get; private set; }

    public NodeViewModel(IUnityContainer container, Node node)
    {
        Node = node;
        ChildNodeViewModels = new ObservableCollection<INodeViewModel>();

        foreach (Node childNode in Node.ChildNodes)
        {
            // Some initialization code
        }
    }
}

到目前为止我没有使用容器,所以我们只有 Node 参数.每个继承的类都有一个属性,我们将其用于模型到视图模型的映射.

Up to now I didn't use the container so we only had the Node argument. Each inherited class had an attribute which we used for model to viewmodel mapping.

[ModelToViewModelMapping(typeof(InheritedNode ))]
public class InheritedViewModel: NodeViewModel
{
    public InheritedViewModel(InheritedNode inheritedNode)
        : base(inheritedNode)
    {
    }
}

然后我有一个这样的初始化行:

I then had an initialization line like this:

ChildNodeViewModels.Add((INodeViewModel)System.Activator.CreateInstance(ModelToViewModelMappingDictionary.GetMappedViewModelType(childNode.GetType()), System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance, null, new object[] { childNode }, null));

我的问题是双重的.

  • 我不知道我对属性的旧技巧是否是映射的常用解决方案.我想不是,但它可以让事情以一种简单的方式保持在一起并易于管理.我不确定 viewmodellocator 是否是一个好的解决方案,而且我对它完全陌生,我还没有尝试过.
  • 如何进行初始化.Resolve 方法使用我可以以一种或另一种方式解析的类型(我的属性解决方案、viewmodellocator,...),但 Unity 的 ParameterOverride 需要一个 string parameterName.如上面的代码所示,由于parameterName 不是常量而失败,每个继承的类都有自己的parameterName.
  • I don't know if my old trick with the attribute is a common solution for the mapping. I suppose not but it keeps things together and manageable in an easy way. I'm not sure the viewmodellocator is a good solution and I'm completely new with it, I haven't tried.
  • How can the initialization be done. The Resolve method uses a type that I can resolve in one way or another (my attribute solution, viewmodellocator, ...) but Unity's ParameterOverride needs a string parameterName. As shown in the code above that fails because the parameterName is not constant, every inheritted class has its own parameterName.

提前致谢!

推荐答案

广告 1:

我喜欢你目前的选择,因为你避免以这种方式创建大量工厂.

I like your current option because you avoid creating a lot of factories this way.

广告 2:

为了统一,你必须提供参数名称,所以你必须手动反映构造函数并找到要覆盖的参数名称.然后你可以通过统一解析你的视图模型并传入模型.但是有一个问题!当使用参数覆盖解析时,您覆盖任何与覆盖中的参数名称匹配的参数,在层次结构中的任何位置,无论是什么类型.在最好的情况下,类型不匹配,你会得到一个异常,在最坏的情况下,一个错误的依赖被静默注入.祝调试愉快...

You have to provide the parameter name for unity, so you have to manually reflect the constructor and find the name of the parameter you want to override. Then you can resolve your view model through unity and pass in the model. But there's a catch! When resolving with a parameter override, you override any parameters that happen to match the parameter name in the override, anywhere in the hierarchy and no matter what type. In the best case, the types don't match and you get an exception, in the worst case, a wrong dependency is injected, silently. Have fun debugging that...

推荐:

坚持您当前的生成视图模型的方式,并在构造对象时注入依赖项.这也有缺点,因为你不能使用构造函数注入,而且你需要对容器的引用,但我更喜欢那些而不是相当不可预测的参数覆盖.

Stick with your current way of producing the view models, and inject the dependencies when the objects are constructed. This has drawbacks, too, because you cannot use constructor injection and you need a reference to the container, but I'd prefer those to the rather unpredictable parameter override.

var viewModel = Activator.CreateInstance(...);
_container.BuildUp( viewModel );

internal class MyChildViewModel
{
    [Dependency]
    public ISomeService MyDependency { get; set; }
}

这篇关于统一初始化分层树视图模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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