mvvm和模型依赖性 [英] mvvm and model-dependencies

查看:121
本文介绍了mvvm和模型依赖性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正计划编写一个更大的mvvm应用程序.我们的需求是
-许多模型类(很多不同的组件,但是结构相对简单)
-此类之间的依赖关系(属性依赖于系统不同部分中的其他依赖项)

我试图找出模型和mvvm/gui之间的分离-我们应该在哪里放置业务层?所有的依赖都应该去吗?

这是一个非常简化的示例,显示了我的想法:

A类具有属性bool prop1和int prop2.如果设置了prop1,则只能设置prop2(并具有任何含义).
如果我在gui中执行此操作,这很容易,只需将prop2的gui字段isEnabled绑定到prop1.

要在ViewModel中做到这一点比较困难,则必须将prop2的isEnabled绑定到ViewModel中的prop1上-绑定必须在viewmodel类中设置(是对还是完全错了?)

在属于该模型的业务层中如何做到这一点?还是应该在哪里接线?

另外,应该集成更复杂的依赖项. prop2可能依赖于另一个类中的prop1,或者甚至必须依赖于另一个类中prop2的值来填充组合框-这种依赖关系可以再次通过数据绑定来完成,但是gui不必命令viewmodel进行绑定或必须绑定到其viewmodel提供的值.

感谢您的帮助
manni

We''re planning to write a larger mvvm application. Our needs are
- a lot of model classes (lot''s of different components, but relatively simply structured)
- lots of dependencies between and in this classes (properties depend on lot''s of others in different parts of the system)

i''m trying to figure out the separation between the model and the mvvm/gui - where should we put the business layer? were should all the dependencies go?

Here''s a a very simplified sample to show my thinking:

class A has property bool prop1 and int prop2. prop2 should only be possible to set (and has any meaning) if prop1 is set.
If i do that in the gui, it''s easy, just bind the gui field isEnabled of prop2 to prop1.

To do that in the ViewModel is harder, the isEnabled of prop2 would than have to be wired to prop1 in the ViewModel - the binding would have to be set up in the viewmodel class (am i right or did i get it completely wrong?)

How can this be done in the business layer belonging to the model? or where should it be wired up?

additionally more complex dependencies should be integrated. prop2 may depend on prop1 in another class, or a combobox has to be filled depending on the value of prop2 in even another class - this dependency can again be done via databinding, but than the gui has to order a viewmodel to do the binding to or has to bind to the values provided by it''s viewmodel.

thanks for any help
manni

推荐答案

有几种方法可以实现此目标,具体取决于您希望逻辑插入的位置.

如果无法在没有设置Prop1的情况下设置模型上的Prop2,则Prop2的设置者应抛出一个异常来防止这种情况.
然后,为了使UI能够实现此目的,ViewModel应该公开指示此内容的属性.

伪代码示例:

There''s several ways of achieving this, depending on where you want the logic to kick in.

If Prop2 on your model can never be set without Prop1 being set then the setter for Prop2 should toss an exception preventing this.
Then, in order for the UI to realize this the ViewModel should expose a property indicating this.

Psuedo-code example:

<xaml>
    <TextBox Content="{Binding Path=Prop1}"/>
    <TextBox Content="{Binding Path=Prop2}" IsEnabled="{Binding Path=Prop2Enabled, Mode=OneWay}"/>
</xaml>


class ViewModel
{
    private Model model;

    public string Prop1
    {
        get { return model.Prop1; }
        set { model.Prop1 = value;
    }
    public string Prop2
    {
        get { return model.Prop2; }
        set { model.Prop2 = value; }
    }
    public bool Prop2Enabled
    {
        get { return model.Prop1 != null; }
    }
}


class Model
{
    private string prop2;
    public string Prop1 { get; set;}
    public string Prop2
    {
        get { return prop2; }
        set
        {
            if (Prop1 == null)
                throw new InvalidOperationException("Cannot set Prop2 while Prop1 is unset");
            prop2 = value;
        }
    }
}



希望这会有所帮助,
弗雷德里克(Fredrik)



Hope this helps,
Fredrik


这篇关于mvvm和模型依赖性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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