c# - How to determine if a Property has changed through User Input -
i'm writing program gets person data database , let user modify data in view.
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 { { 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 { { return _isdirty; } } public void onpropertychanged(string propertyname) { _isdirty = true; propertychanged?.invoke(this, new propertychangedeventargs(propertyname)); } }
unfortunately, when ef builds person model, onpropertychanged method gets called isdirty return true. (just assume full properties firstname, lastname etc. in setter call onpropertychanged method. complexity reasons didn't insert whole code complex objects in person).
my main question is:
is possible determine if property got modified user input (and not ef)? there other approaches one? there best practice?
hint: i'm trying follow mvvm + repository + unit of work pattern.
edit 1
public class useradminviewmodel : baseviewmodel { #region commands private relaycommand _searchpersoncommand; public relaycommand searchpersoncommand { { return _searchpersoncommand ?? (_searchpersoncommand = new relaycommand(o => { availablepersons.clear(); foreach (var person in getpersonwithfilter(o string)) { availablepersons.add(person); } }, o => o string)); } } private relaycommand _savecommand; public relaycommand savecommand { { return _savecommand ?? (_savecommand = new relaycommand(o => { //_personunitofwork.persons.updaterange(); _personunitofwork.save(); _userunitofwork.save(); })); } } private relaycommand _deletecommand; public relaycommand deletecommand { { return _deletecommand ?? (_deletecommand = new relaycommand(o => { availablepersons.remove(o person); _personunitofwork.persons.remove(o person); }, o => o person)); } } #endregion #region unit of work private readonly ipersonunitofwork _personunitofwork; private readonly iuserunitofwork _userunitofwork; #endregion #region selected private bool _ispersonselected; public bool ispersonselected { { return _ispersonselected; } set { if (_ispersonselected != value) { this._ispersonselected = value; onpropertychanged("ispersonselected"); } } } private person _selectedperson; public person selectedperson { { 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 { { return _isuserselected; } set { if (_isuserselected != value) { this._isuserselected = value; onpropertychanged("isuserselected"); } } } private user _selecteduser; public user selecteduser { { 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(); } } }
}
Comments
Post a Comment