带有视图模型的 MVVM 继承 [英] MVVM Inheritance With View Models

查看:25
本文介绍了带有视图模型的 MVVM 继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何在 MVVM 模式中使用视图模型来处理继承.在我的应用程序中,我有一个类似于以下内容的数据模型:

I am wondering about how to approach inheritance with View Models in the MVVM pattern. In my application I have a Data Model that resembles the following:

class CustomObject
{
    public string Title { get; set; }
}

class CustomItem : CustomObject
{
    public string Description { get; set; }
}

class CustomProduct : CustomItem
{
    public double Price { get; set; }
}

在我的应用程序中,我有一个 ViewModelBase 类,然后将有以下视图模型:

In my application I have a ViewModelBase class and then was going to have the following View Models:

  • 自定义对象视图模型
  • CustomItemViewModel
  • 自定义产品视图模型

CustomObjectViewModel 的粗略实现类似于以下内容:

A rough implementation of the CustomObjectViewModel would resemble the following:

class CustomObjectViewModel : ViewModelBase
{
    private readonly CustomObject _customObject;

    public CustomObjectViewModel( CustomObject customObject )
    {
        _customObject = customObject;
    }

    public string Title
    {
        // implementation excluded for brevity
    }
}

在我看来,我的视图模型会以与我的模型相同的方式扩展自己(CustomItemViewModel 扩展 CustomObjectViewModel 等等)似乎是合乎逻辑的.但是,我注意到当我沿着继承树向下走时,我将添加对同一对象的其他引用.这对我来说似乎有些过分,我想知道如何解决这个问题,以及是否可以让它更干净.

It seems logical to me that my View Models would extend themselves in the same manner as my Model did (CustomItemViewModel extends CustomObjectViewModel and so on). However, I have noticed that as I go down the inheritance tree I'll be adding additional references to the same object. This seems rather excessive to me and was wondering how to approach this problem and if it were possible to make it much cleaner.

推荐答案

一般我会建议你不要在不同的 ViewModel 类之间进行继承,而是让它们直接从一个公共抽象基类继承.
这是为了避免引入不必要的复杂性,因为来自层次结构中较高级别的成员污染了 ViewModel 类的接口,但没有完全内聚类的主要用途.
继承带来的耦合也可能使得很难在不影响其派生类的情况下更改 ViewModel 类.

如果您的 ViewModel 类总是引用单个 Model 对象,您可以使用泛型将此规则封装到基类中:

Generally I would recommend you not to have inheritance between different ViewModel classes, but instead having them inherit directly from a common abstract base class.
This is to avoid introducing unnecessary complexity by polluting the ViewModel classes' interfaces with members that come from higher up in the hierarchy, but are not fully cohesive to the class's main purpose.
The coupling that comes with inheritance will also likely make it hard to change a ViewModel class without affecting any of its derived classes.

If your ViewModel classes always will reference a single Model object, you could use generics to encapsulate this rule into the base class:

public abstract class ViewModelBase<TModel>
{
    private readonly TModel _dataObject;

    public CustomObjectViewModel(TModel dataObject)
    {
        _dataObject = dataObject;
    }

    protected TModel DataObject { get; }
}

public class CustomObjectViewModel : ViewModelBase<CustomObject>
{
    public string Title
    {
        // implementation excluded for brevity
    }
}

public class CustomItemViewModel : ViewModelBase<CustomItem>
{
    public string Title
    {
        // implementation excluded for brevity
    }

    public string Description
    {
        // implementation excluded for brevity
    }
}

这篇关于带有视图模型的 MVVM 继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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