Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

Guidance on Two-Way Binding on a Model property #100

Closed
SSchulze1989 opened this issue Apr 27, 2022 · 6 comments · Fixed by #103
Closed

Guidance on Two-Way Binding on a Model property #100

SSchulze1989 opened this issue Apr 27, 2022 · 6 comments · Fixed by #103
Labels
type: question Questions about the library

Comments

@SSchulze1989
Copy link
Contributor

SSchulze1989 commented Apr 27, 2022

Question

Hello,
I am just starting out with blazor and your Mvvm library is already helping me a lot getting on.
I just have a question if you could explain how to best handle two-way bindings on properties that are bound from the model.
For example i have a model and a viewmodel like below.
How would i go best to integrate notification into that view model?

Cheers
Simon

Code sample

public class Model 
{
    public string Name { get; set; }
}

public class ViewModel : ViewModelBase
{
    private Model _model;

    public string Name 
    { 
        get  => _model.Name; 
        set 
        {
            _model.Name = value;
            OnPropertyChanged(nameof(Name));
        }
    }
}

Version

6.0.3

Are you using Blazor WASM or Blazor Server?

Balzor Server

@SSchulze1989 SSchulze1989 added status: triage Needs to be triaged type: question Questions about the library labels Apr 27, 2022
@SSchulze1989
Copy link
Contributor Author

SSchulze1989 commented Apr 27, 2022

To elaborate, i was looking for something less verbose than:

public string Name
    {
        get => _model.Name;
        set
        {
            _model.Name = value;
            OnPropertyChanged(nameof(Name));
        }
    }

On top of that, could the

[CallerMemberName]

attribute also be added to OnPropertyChanged Method, so i can at least just write

OnPropertyChanged()

?

@klemmchr
Copy link
Owner

Currently there is no built-in solution for this. Could you elaborate why you want a binding on a sub model? Is the model changed externally outside of the view model?

@SSchulze1989
Copy link
Contributor Author

The Model is part of my Business Logic that communicates with a REST Api. The ViewModel should enable modifying the Model and then sending a request back to the Api without having to map in between the models.

@klemmchr
Copy link
Owner

This could only be achieved by having a binding on a type that implements INotifyPropertyChanged and then listen to changed events on this end. Would be a new feature and possibly be a bit tedious to implement because it would break with current behavior. Would that suit you?

@SSchulze1989
Copy link
Contributor Author

SSchulze1989 commented Apr 27, 2022

Ah I think i did not get that across correctly.
I do not want the Model to notify on the ViewModel. The ViewModel would still invoke the notification itself but the changes should be reflected on the Model so that the Business logic can handle the requests.

So I want the Model to hold the state for the api client and the ViewModel for interaction with the component.
I updated my code example a little so maybe it gets clearer.
In the end I think I can do with what I have shown in the example if there is not a build in way to deal with it.

Would it still be possible to add [CallerMemberName] to the OnPropertyChanged() Method?

@SSchulze1989
Copy link
Contributor Author

Thank you very much for the quick response and the change.
Exactly what i needed and working like a charm!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
type: question Questions about the library
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants