简体   繁体   中英

How to shorten code with WPF MVVM?

I don't know if this question belongs here and tell me if it doesn't but I am struggling with WPF and MVVM.

So right now I have a class Customer with about 20 properties:

public class Customer 
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Address { get; set; }
    ...
}

According to MVVM I have to duplicate all those properties inside my ViewModel for example (I only added the name property in order to make the question not too long. But consider I would add all the 20 properties of my Model to my ViewModel like the following):

public class CustomerViewModel : ViewModelBase
{
    private string _name;
    public string Name
    {
        get { return _name; }
        set
        {
            _name = value;
            OnPropertyChanged();
        }
    }
    ...
}

So you can see already just with that example I would have created hundreds of lines of code. Now If I need to save the data of the View to the database, I would do that:

private void SaveCustomer()
{
    Customer customer = new Customer();
    customer.Id = Id;
    customer.Name = Name;
    customer.Address = Address;
    ...
}

Again a lot of code. If I need to load a customer, I would need to do the same just reversed and If I need to empty all the properties, I would need to set each property = null.

In my project I did it exactly like this and I don't know if this is the right way, tell me another way if there is. But this creates hundreds of lines of code and I can't see how this is maintainable since it is so easy to loose track with that much code. Please tell me if there is another way or if my thoughts are just wrong.

There isn't any rule that says your model POCOs and view model POCOs have to be different. I would just use Customer , and have a view model own one:

public class CustomerPageViewModel
{
     public Customer Customer
     {
         //get/set
     }
}

That way any changes from the UI are already in that object and you can pass it to whatever services will persist the changes. Customer could implement INPC but if nothing but the UI is changing it that isn't necessary.

As an aside; don't measure your code quality in terms of length, sometimes you just have a lot of properties on a VM. Measure it in terms of how easy it is to maintain, extend, etc.

Wrap the Model properties on your ViewModel properties making the properties isolated from each other.

public class Customer 
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Address { get; set; }
    ...
}
public class CustomerViewModel : ViewModelBase
{
    private Customer _customer;
    public string Name
    {
        get => _customer.Name;
        set
        {
            if(_customer.Name != value)
            {
                 _customer.Name = value;
                 OnPropertyChanged();
            }
        }
    }
}

Then you can omit the Save and Load methods or just call Repository.Save / Load in them. The idea is, each property can be tested separately.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM