GuOrg / Gu.State

MIT License
9 stars 3 forks source link

Reset changes #64

Open dpavsrtrl opened 6 years ago

dpavsrtrl commented 6 years ago

I think machinery is in place to be able to reset or undo changes. What do you think?

JohanLarsson commented 6 years ago

Not sure what you mean, can you explain the use case and perhaps some pseudo code for the API?

dpavsrtrl commented 6 years ago

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.

JohanLarsson commented 6 years ago

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.

dpavsrtrl commented 6 years ago

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.

JohanLarsson commented 6 years ago

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.

JohanLarsson commented 6 years ago

Did it work for you?

dpavsrtrl commented 6 years ago

No, I haven't. But you answered my questions. I guess this issue can be tagged and closed.