MVVM继承随着视图模型 [英] MVVM Inheritance With View Models

查看:123
本文介绍了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:


  • CustomObjectViewModel

  • CustomItemViewModel

  • CustomProductViewModel

一个粗略的实施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类,而不会影响其任何派生类。



如果你的视图模型类总是会引用一个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天全站免登陆