This project is read-only.

dynamic property base on model's public property

Jun 4, 2010 at 11:53 PM

Hi,

Is it possible to generate dynamic property base on model's public property and use the model as backing store.  

 

//class for model

public class PersonModel

{
    public string FirstName;
    public string LastName;
}


//===before===
public class PersonViewModel:ViewModelBase
{
    private PersonModel person;    

    public string FirstName     
    {      
        get{return person.FirstName;}
        set 
        {
           person.FirstName=value;
        }
    }

    public string LastName
    {        
        get{return person.LastName;}        
        set 
        {
            person.LastName=value;
        }
    }
 }
//===after===

public class PersonViewModel:ViewModelBase

{
    [Model]  //custom attribute to ask the ViewModelBase to generate dynamic property base on person's public property.    
    private PersonModel person;
}

 

 

Jun 5, 2010 at 2:50 AM

An interesting idea.

With the example you gave, though, I don't see any real benefit.  Those proxy properties can just be removed and person can be made public so the view can bind to person.FirstName and person.Lastname directly.  The properties, as written, do not supply any value to the View or the Model.

That is not to say I am dismissing your idea outright... lets expand on your idea.  What are some reasons why we want to create ViewModel properties that interface with the model?

Validation

We usually don't like to add view-level validation to the model, so we add it to the ViewModel instead:

    [Required(ErrorMessage = "Last Name is Required")]
    public string LastName
    {        
        get{return person.LastName;}        
        set 
        {
            person.LastName=value;
        }
    }

If we took your idea a little further, we might be able to do something like this:

    [Model, Required(Property = "LastName", ErrorMessage = "Last Name is Required")]
    protected PersonModel person;

Now, we have provided some real value to the property.

Property Translation

Another reason we might want to proxy to model properties is to format the data in one way or another.  Assuming PersonModel has a boolean on it named IsRetired, we may want to show a retired options panel.  We would do this in the ViewModel:

    public Visibility ShowRetiredPanel
    {
        get
        {
            return person.IsRetired ? Visibiliby.Visible : Visibility.Collapsed;
        }
    }

Instead, we might use the Model attribute to map that property:

    [Model, MapProperty("IsRetired", "ShowRetiredPanel", typeof(BoolToVisiibilityConverter))]
    protected PersonModel person;

To extrapolate on that idea more, we might want to only proxy the properties we specify, hiding some others from the view:

    [Model, ProxyProperty("FirstName", "IsRetired"))]
    protected PersonModel person;

 

At this point, I am just brainstorming.  What other value can we automatically map using dynamic properties like this?

Great idea.  Keep them coming :)

Jun 5, 2010 at 3:33 AM
BrianGenisio wrote:

An interesting idea.

With the example you gave, though, I don't see any real benefit.  Those proxy properties can just be removed and person can be made public so the view can bind to person.FirstName and person.Lastname directly.  The properties, as written, do not supply any value to the View or the Model.

That is not to say I am dismissing your idea outright... lets expand on your idea.  What are some reasons why we want to create ViewModel properties that interface with the model?

 I forgot to add the INotifyPropertyChange support in the code, It's the main reason that I don't like to use person.FirstName directly

 public string FirstName     
  {     
        get{return person.FirstName;}
        set 
        {
           person.FirstName=value;

           OnPropertyChanged("FirstName");
        }
}


You Validation & Property Translation ideas are great  :)