c# - How to create IsDirty inside OnPropertyChange, but at the same time turn it off when entities are loaded from a database -
i’m building wpf app, , got point want able check whether entity isdirty, found solution online:
private bool isdirty; public virtual bool isdirty { { return isdirty; } set { isdirty = value; } } public event propertychangedeventhandler propertychanged = delegate { }; protected void onpropertychanged([callermembername] string propertyname = "") { onpropertychanged(true, propertyname); } protected void onpropertychanged(bool makedirty, [callermembername] string propertyname = "") { propertychanged(this, new propertychangedeventargs(propertyname)); if (makedirty) { isdirty = makedirty; } }
then can use
[required(errormessage = errormessages.descriptionrequired)] [stringlength(60, errormessage = errormessages.descriptionlength60)] public string description { { return description; } set { if (description != value) { description = value; onpropertychanged(); } } }
and seems work, anytime property changed entity dirty because onpropertychanged called. problem have when load entity (or collection of entities) database, onpropertychanged called:
someentitiescollection = new service().someentities();
then each entity comes marked dirty, , cannot find anyway around it, apart going through loop , setting isdirty false. there anyother way of achieving it?
regards
no, there isn't easy way
you should override method retrieve entities db in way sets automatically isdirty=false
on entity creation. used nhibernate interceptor , events similar not case.
another way keep copy of original values , compute dirtiness when asks it. instead of using isdirty property should create method , save initial state properties
Comments
Post a Comment