question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can a property depend on a property in an instance of another class?

See original GitHub issue

I’m considering having shared/global state (e.g. AllTodos) in a service (implementing INotifyPropertyChanged) which is injected into all viewmodels that need access to shared state. These viewmodels would then have their own properties which are more or less just projections of the shared state, e.g. the TodoListViewModel could have a property called FilteredTodos which depend on the injected service’s AllTodos property (as well as its own SearchTerm property or similar).

For example:

public class SharedState : INotifyPropertyChanged
{
    public IList<Project> Projects { get; set; }

    public bool IsRefreshing { get; set; }

    // etc.
}

public class TodoListViewModel : INotifyPropertyChanged
{
    // Could also be a property if necessary
    private SharedState sharedState;

    public TodoListViewModel(SharedState sharedState)
        => this.sharedState = sharedState;

    public bool IsRefreshingIndicatorVisible => this.sharedState.IsRefreshing;

    public bool IsProjectListVisible => !this.sharedState.IsRefreshing;
 
    public bool SearchTerm { get; set; } 

    public bool FilteredTodos => this.sharedState.AllTodos
        .Where(todo => todo.MatchesSearchTerm(this.SearchTerm));
}

Is it possible, using PropertyChanged.Fody, to set up dependencies correctly in this case? For example, TodoListViewModel.IsRefreshingIndicatorVisible would notify whenever SharedState.IsRefreshing changes, and TodoListViewModel.FilteredTodos would notify whenever SharedState.AllTodos or TodoListViewModel.SearchTerm changes.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
SimonCroppcommented, Aug 20, 2017

@navozenko sorry i think reference tracking of nested instances is outside the scope of this project

1reaction
SimonCroppcommented, May 5, 2018

@shtse8 happy to see a PR that show how this would work

Read more comments on GitHub >

github_iconTop Results From Across the Web

typescript - Access to class from class which is property
It is totally normal to have an instance of a class as a property of another class. What if I need to get...
Read more >
How do I create a property in a class that is a direct handle ...
Dependent property myMaster will return either immutableMaster or privateMaster depending on the immutable writeable property, which is set = ...
Read more >
How to use properties of a class in its own class and also in ...
Here my question is i have defined two classes inorder to calculate the parameters in my function which are dependend on each other,...
Read more >
Use instance attributes or class properties inside the class ...
Most Pythonic is to just use a public attribute, no properties. attribute, so the answer depends on the class and your own preferences....
Read more >
Python's property(): Add Managed Attributes to Your Classes
In this step-by-step tutorial, you'll learn how to create managed attributes, also known as properties, using Python's property() in your custom classes.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found