1

In my viewmodel, there is a property CurrentObject of the type SomeDomainType which is serializeable.

There is a form to edit its properties, and one of the buttons is "Commit Changes", databound to the ICommand CommitChangesCommand.

Now I expect this button to be active (via CanCommitChangesCommand(), properly wired with Josh Smith's RelayCommand) only when the object has been modified, that is, the object is "dirty".

Saying it again, what I want to ask is:

"How can I mark an object as dirty so that I could have a private bool ThatPropertyIsDirty() method to check that inside some CanExecute()?"

tshepang
  • 12,111
  • 21
  • 91
  • 136
heltonbiker
  • 26,657
  • 28
  • 137
  • 252

1 Answers1

1

From the sound of it:

  1. Add a IsDirty property onto your SomeDomainType
  2. In the setter of IsDirty raise the NotifyPropertyChanged event for IsDirty
  3. In all of the properties on your SomeDomainType that you change to make the object considered Dirty, in their setters, set IsDirty to true

This is presuming your SomeDomainType implements the INotifyPropertyChanged interface. If not, create a wrapper class around the SomeDomainType that does, and make the above changes to that wrapper.

Psytronic
  • 6,043
  • 5
  • 37
  • 56
  • Interesting. Would you say there is a typical situation where the object is considered "clean" again, for example, just after it is persisted? Should `IsDirty` property be public then? I'm very beginner in dealing with object persistence (by the way, my type implements `[Serializable]`, but I chose not to implement `INPC` on it so far). – heltonbiker Oct 10 '13 at 19:18
  • Yes, usually after your object is persisted to whatever persistence method you're using (db, flat files etc), then it's considered clean, and you'll want to set `IsDirty` to false. You'll probably want to expose `IsDirty` as public, with a private setter, as nothing needs to set it dirty, aside from the properties themselves. – Psytronic Oct 10 '13 at 19:20
  • That seems ok. Just for the record, I think I'll combine your tips with the `StatusEnum` proposed in this question (best for transaction entities, I guess). http://stackoverflow.com/questions/805505/c-sharp-marking-class-property-as-dirty – heltonbiker Oct 10 '13 at 19:22
  • No problem, whatever works for your scenario, if you need that level of distinction in your class then go for it. – Psytronic Oct 10 '13 at 19:24