Click here to Skip to main content
15,908,013 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Posted
Comments
PumbaPumba 1-Oct-10 5:39am    
@manni1981 could get your question well. The best thing you need to do is there are many open source initiative projects found in sites like codeproject or code plex. Download the projects and analyse them. Some useful links are

Good place to start from here http://msdn.microsoft.com/en-us/magazine/dd419663.aspx
manni1981 1-Oct-10 6:35am    
thx,i'm in the middle of reading that :)

1 solution

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:

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

C#
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; }
    }
}

C#
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;
        }
    }
}


Hope this helps,
Fredrik
 
Share this answer
 
Comments
manni1981 1-Oct-10 7:21am    
yeah, with the time i think i get the hang of mvvm, at least a tiny little bit :) thanks for the help
so the model makes sure it is legal, whereas the viewmodel really specifies all properties that the view needs and binds to. so additional validation and checks can be in the viewmodel, but there mostly are some in the model as well

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900