Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reset changes #64

Open
dpavsrtrl opened this issue Dec 15, 2017 · 7 comments
Open

Reset changes #64

dpavsrtrl opened this issue Dec 15, 2017 · 7 comments

Comments

@dpavsrtrl
Copy link

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

@JohanLarsson
Copy link
Member

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

@dpavsrtrl
Copy link
Author

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
Copy link
Member

JohanLarsson commented Dec 15, 2017

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
Copy link
Author

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
Copy link
Member

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
Copy link
Member

Did it work for you?

@dpavsrtrl
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants