我正在编写一个从数据库中获取人员数据的程序,让用户修改视图中的数据。
public class Person : BaseModel {
public int Id { get; set; }
#region Required Properties
[Required]
public string Firstname { get; set; }
[Required]
public string Lastname { get; set; }
[Required]
public virtual Department Department { get; set; }
#region Not Mapped
[NotMapped]
public override bool IsDirty {
get {
return _isDirty ||
Department.IsDirty;
}
}
#endregion
public override string ToString() {
return $"{Firstname} {Lastname} ({Department.ToString()})";
}
}
-
public class BaseModel : INotifyPropertyChanged {
public event PropertyChangedEventHandler PropertyChanged;
[NotMapped]
internal bool _isDirty;
public virtual bool IsDirty {
get {
return _isDirty;
}
}
public void OnPropertyChanged(string propertyName) {
_isDirty = true;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
不幸的是,当EF构建Person Model时,会调用OnPropertyChanged方法,因此IsDirty将始终返回true。 (假设为名字,姓氏等的完整属性,其中setter将调用OnPropertyChanged方法。出于复杂性原因,我没有将所有复杂对象插入到我的整个代码中。)
我的主要问题是:
是否可以确定某个属性是否已被用户输入(而非EF)修改? 还有其他方法吗?有最好的做法吗?
提示:我试图遵循MVVM +存储库+工作单元模式。
修改1
public class UserAdminViewModel : BaseViewModel {
#region Commands
private RelayCommand _searchPersonCommand;
public RelayCommand SearchPersonCommand {
get {
return _searchPersonCommand ?? (_searchPersonCommand = new RelayCommand(o => {
AvailablePersons.Clear();
foreach (var person in GetPersonWithFilter(o as string)) {
AvailablePersons.Add(person);
}
}, o => o is string));
}
}
private RelayCommand _saveCommand;
public RelayCommand SaveCommand {
get {
return _saveCommand ?? (_saveCommand = new RelayCommand(o => {
//_personUnitOfWork.Persons.UpdateRange();
_personUnitOfWork.Save();
_userUnitOfWork.Save();
}));
}
}
private RelayCommand _deleteCommand;
public RelayCommand DeleteCommand {
get {
return _deleteCommand ?? (_deleteCommand = new RelayCommand(o => {
AvailablePersons.Remove(o as Person);
_personUnitOfWork.Persons.Remove(o as Person);
}, o => o is Person));
}
}
#endregion
#region Unit of Work
private readonly IPersonUnitOfWork _personUnitOfWork;
private readonly IUserUnitOfWork _userUnitOfWork;
#endregion
#region Selected
private bool _isPersonSelected;
public bool IsPersonSelected {
get {
return _isPersonSelected;
}
set {
if (_isPersonSelected != value) {
this._isPersonSelected = value;
OnPropertyChanged("IsPersonSelected");
}
}
}
private Person _selectedPerson;
public Person SelectedPerson {
get {
return _selectedPerson;
}
set {
if (SelectedPerson != value) {
_selectedPerson = value;
IsPersonSelected = value != null;
SelectedUser = null;
OnPropertyChanged("SelectedPerson");
DeleteCommand.OnCanExecuteChanged();
if(value != null)
PersonsUsers = new ObservableCollection<User>(GetPersonsUsers(value));
}
}
}
private bool _isUserSelected;
public bool IsUserSelected {
get {
return _isUserSelected;
}
set {
if (_isUserSelected != value) {
this._isUserSelected = value;
OnPropertyChanged("IsUserSelected");
}
}
}
private User _selectedUser;
public User SelectedUser {
get {
return _selectedUser;
}
set {
if (SelectedUser != value) {
_selectedUser = value;
IsUserSelected = value != null;
OnPropertyChanged("SelectedUser");
}
}
}
#endregion
#region Property Lists
public ObservableCollection<Person> AvailablePersons { get; private set; }
public ObservableCollection<User> PersonsUsers { get; private set; }
public ObservableCollection<Status> AvailableStatus { get; private set; }
#endregion
public UserAdminViewModel(IPersonUnitOfWork personUnitOfWork, IUserUnitOfWork userUnitOfWork) {
_personUnitOfWork = personUnitOfWork;
_userUnitOfWork = userUnitOfWork;
AvailablePersons = new ObservableCollection<Person>();
PersonsUsers = new ObservableCollection<User>();
AvailableStatus = new ObservableCollection<Status>(_personUnitOfWork.Status.GetAll());
foreach (var person in personUnitOfWork.Persons.GetAll()) {
AvailablePersons.Add(person);
}
}
public IEnumerable<User> GetPersonsUsers(Person person) {
return _userUnitOfWork.Users.Get(o => o.Person.Id.Equals(person.Id));
}
private IEnumerable<Person> GetPersonWithFilter(string filter) {
if (!string.IsNullOrWhiteSpace(filter)) {
return _personUnitOfWork.Persons.Get(
p => p.Firstname.Contains(filter) ||
p.Lastname.Contains(filter) ||
p.Department.Abbreviation.Contains(filter) ||
p.Department.Costcenter.Contains(filter) ||
p.Department.Division.Abbreviation.Contains(filter) ||
p.Department.Division.PayrollArea.Contains(filter) ||
p.Department.Description.Contains(filter) ||
p.Department.Division.Description.Contains(filter) ||
p.Users.Any(u => u.UserID.Contains(filter)));
} else {
return _personUnitOfWork.Persons.GetAll();
}
}
}
}