Open dpavsrtrl opened 6 years ago
Not sure what you mean, can you explain the use case and perhaps some pseudo code for the API?
Suppose I'm tracking changes in UI viewmodels, and user wants to cancel changes. How hard would it be to implement using this project? I think that changetracker probably has all information to do that.
Maybe you want something like:
public class ViewModel
{
private readonly Dto snapshot;
public ViewModel(Dto dto)
{
this.snapshot = DeepCopy(dto);
this.Editable = dto;
this.DirtyTracker = Track.IsDirty(this.Editable, this.snapshot);
}
public Dto Editable { get; }
public IDirtyTracker DirtyTracker { get; }
public void Save()
{
// Save Editable
Copy.PropertyValues(this.Editable, this.snapshot);
}
public void Cancel()
{
Copy.PropertyValues(this.snapshot, this.Editable);
}
}
Kept it short. DeepCopy
is probably a serialization roundtrip.
Note that IDirtyTracker
is IDisposable
so the class above should implement IDisposable
and dispsoe the tracker.
If you don't need to preserve references things get even simpler:
public class ViewModel
{
private Dto snapshot;
public ViewModel(Dto dto)
{
this.snapshot = DeepCopy(dto);
this.Editable = dto;
this.DirtyTracker = Track.IsDirty(this.Editable, this.snapshot);
}
public Dto Editable { get; }
public IDirtyTracker DirtyTracker { get; }
public void Save()
{
// Save Editable
this.DirtyTracker.Dispose();
this.snapshot = DeepCopy(this.Editable);
this.DirtyTracker = Track.IsDirty(this.Editable, this.snapshot);
}
public void Cancel()
{
this.DirtyTracker.Dispose();
this.Editable = DeepCopy(this.snapshot);
this.DirtyTracker = Track.IsDirty(this.Editable, this.snapshot);
}
}
Syntax probably slightly off.
Yes, that makes sense. Thank you for detailed response. I guess I'll have to rework my use of Dto objects to make them easily exchangeable.
You can make a small prototype and see that it works first, been a while since I used this lib so a bit rusty on it. Also we should add a small sample to the docs.
Did it work for you?
No, I haven't. But you answered my questions. I guess this issue can be tagged and closed.
I think machinery is in place to be able to reset or undo changes. What do you think?