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

Add VisualizerResult with instance of view model #1489

Open
GeertvanHorrik opened this issue Nov 22, 2019 · 5 comments
Open

Add VisualizerResult with instance of view model #1489

GeertvanHorrik opened this issue Nov 22, 2019 · 5 comments
Assignees
Milestone

Comments

@GeertvanHorrik
Copy link
Member

@GeertvanHorrik GeertvanHorrik commented Nov 22, 2019

At the moment the IUIVisualizerService returns a bool representing the result of the DialogResult (which is based on the view model result).

However, sometimes it's convenient to access the used VM in cases like this:

var result = await uiVisualizerService.ShowDialog<MyViewModel>();
if (result ?? false)
{
    // do something here with the vm
}

At the moment, one has to manually instantiate the view model and pass the instance into the IUIVisualizerService in order to use it later.

The idea is to introduce a new class:

public class VisualizerResult : VisualizerResult<IViewModel>

public class VisualizerResult<TViewModel>
    where TViewModel : IViewModel
{
    public VisualizerResult(bool? result, object dataContext}
    {

    }

    public bool? Result { get; set; }

    public object DataContext { get; }

    public TViewModel ViewModel { get { return DataContext as TViewModel; } }
}

Instead of bool?, the IUIVisualizerService will return this class and allow for the following use case:

var result = await uiVisualizerService.ShowDialog<MyViewModel>();
if (result ?? false)
{
    var vm = result.ViewModel; // MyViewModel
}

With implicit operator overloads to bool, we should be able to still use this (commonly used) code:

if (await uiVisualizerService.ShowDialog<MyViewModel>() ?? false)
{
    // do something here, but no need for vm
}
@GeertvanHorrik GeertvanHorrik self-assigned this Nov 22, 2019
@GeertvanHorrik GeertvanHorrik added this to the 6.0.0 milestone Nov 22, 2019
@ghost

This comment has been minimized.

Copy link

@ghost ghost commented Nov 22, 2019

Does this mean that the ViewModel will no longer be present in UICompletedEventArgs.DataContext when specifying a closing function during ShowAsync and ShowDialogAsync? That’s how we’ve been accessing it.

@GeertvanHorrik

This comment has been minimized.

Copy link
Member Author

@GeertvanHorrik GeertvanHorrik commented Nov 22, 2019

It will still be accessible there, but I think the UICompletedEVentArgs required a callback?

@GeertvanHorrik

This comment has been minimized.

Copy link
Member Author

@GeertvanHorrik GeertvanHorrik commented Nov 22, 2019

Basically only the return type of the methods will change (for more convenient usage).

@ghost

This comment has been minimized.

Copy link

@ghost ghost commented Nov 22, 2019

Yeah it does, both ShowAsync and ShowDialogAsync have and optional parameter to specify a Func which takes that class as a return.

We use that for 2 reasons. 1 to reduce the complexity of a method (keeps SonarQube happy) and 2 to access the ViewModel.

I don’t see any issue with the return type though but I also wonder what will happen with ViewModels that implement the disposable interface.

@GeertvanHorrik

This comment has been minimized.

Copy link
Member Author

@GeertvanHorrik GeertvanHorrik commented Nov 22, 2019

The vm's will be disposed as usual and the return object will (should) be a short-living object. Even if an object is disposed, you can still access its properties to check any required values.

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

Successfully merging a pull request may close this issue.

None yet
1 participant
You can’t perform that action at this time.