realm / realm-dotnet

Realm is a mobile database: a replacement for SQLite & ORMs
https://realm.io
Apache License 2.0
1.26k stars 165 forks source link

Add support for wrapper properties #3197

Open papafe opened 1 year ago

papafe commented 1 year ago

It would be nice to support something like this:

public class Person: IRealmObject
{
    public string Name {get; set;}

    public string Surname {get; set;}

    [DependsOn(nameof(Name), nameof(Surname))]
    public string FullName
    {
        get => return $"{Name} {Surname}";
        set
        {
            var split = string.Split(value);
            Name = split[0];
            Surname = split[1];
        }
    }
}

This should:

While the first point can already be done now with some manual work from the developer, the second point cannot be implemented without our support.

Nurgo commented 1 year ago

Indeed, we really need this functionality. I don't see a clean workaround to handle notifications for dependent properties, since we don't have access to persisted property setters.

Perhaps you could take inspiration from the 'DependsOn' attribute of PropertyChanged.Fody.

nirinchev commented 1 year ago

For now you could add a partial void method that will be called when a property changes:

partial void OnPropertyChanged(string? propertyName)
{
    if (propertyName == nameof(Name) || propertyName == nameof(Surname))
    {
        RaisePropertyChanged(nameof(FullName));
    }
}
Nurgo commented 1 year ago

Indeed it works, thanks for the tip!