principlecsharpMinor
Implementing ViewModel design pattern using DI in a MVP-VM architecture
Viewed 0 times
architecturedesignmvpusingviewmodelimplementingpattern
Problem
Based on this answer: MVVM implementation using C# and XAML, I understand that my ViewModel is some kind of a wrapper for my Model.
Being a fan of DI and SOLID principles, I have made my ViewModel Model-dependent.
CustomerDetailViewModel
CustomerManagementViewModel
ViewModel
```
public abstract class ViewModel : IViewModel where M : class {
public ViewModel(M model) { Model = model; }
public virtual M Model { get { return model; } set { setModel(value); }
public event PropertyChangedEventHandler PropertyChanged;
protected virtual RaisePropertyChangedFor(Expression> propertyExpression) {
var expression = (MemberExpression)propertyExpression.Body;
if (expressionIsNoMemberExpress
Being a fan of DI and SOLID principles, I have made my ViewModel Model-dependent.
CustomerDetailViewModel
public class CustomerDetailViewModel : ViewModel {
public CustomerDetailViewModel(Customer model) : base(model) { }
public bool AllRequiredInformationHasBeenProvided {
get {
return !string.IsNullOrWhiteSpace(Email)
&& !string.IsNullOrWhiteSpace(GivenName)
&& !string.IsNullOrWhiteSpace(Surname);
}
}
public string Email { get { return Model.Email; } set { setEmail(value); } }
public string GivenName { get { return Model.GivenName; } set { setGivenName(value); } }
public string Surname { get { return Model.Surname; } set { setSurname(value); } }
private void setEmail(string email) {
Model.Email = email;
RaisePropertyChangedFor(m => m.Email);
}
private void setGivenName(string givenName) {
Model.GivenName = givenName;
RaisePropertyChangedFor(m => m.GivenName);
}
private void setSurname(string surname) {
Model.Surname= surname;
RaisePropertyChangedFor(m => m.Surname);
}
}CustomerManagementViewModel
public class CustomerManagementViewModel : ViewModel>, {
public CustomerManagementViewModel(IList model) : base(model) { }
public Customer GetCurrent(int index) { return Model.ElementAt(index); }
}ViewModel
```
public abstract class ViewModel : IViewModel where M : class {
public ViewModel(M model) { Model = model; }
public virtual M Model { get { return model; } set { setModel(value); }
public event PropertyChangedEventHandler PropertyChanged;
protected virtual RaisePropertyChangedFor(Expression> propertyExpression) {
var expression = (MemberExpression)propertyExpression.Body;
if (expressionIsNoMemberExpress
Solution
A few superficial things:
-
Your view models should be implementing
-
Standard naming convention for C# is
-
Your view models should be implementing
INotifyPropertyChanged. This will make it more obvious that your models will raise property change events. Also the WPF binding framework checks if a class implements this interface and makes use of it if it does so.-
Standard naming convention for C# is
PascalCase for method names (private or not usually makes no difference)Context
StackExchange Code Review Q#64435, answer score: 2
Revisions (0)
No revisions yet.