0

Recently I found that it is possible to auto-generate code for INotifyPropertyChanged with the use of CommunityToolkit. But I stubbed upon a problem.

I have a DTO class that is retrieved from the backend (and stored in shared project used by both FE and BE):

public class TrainingDayDurationDto {
    public DateOnly Date { get; set; }
    public short DurationMinutes { get; set; }

    public TrainingDayDurationDto(DateOnly date, short durationMinutes) {
        Date = date;
        DurationMinutes = durationMinutes;
    }
}

then on the frontend I map this DTO to client specific object that can have frontend specific functionality/properties added, so I have:

[INotifyPropertyChanged]
public sealed partial class TrainingDayDuration : TrainingDayDurationDto {
    [ObservableProperty] private DateOnly _date;
    [ObservableProperty] private short _durationMinutes;

    public TrainingDayDuration(DateOnly date, short durationMinutes) : base(date, durationMinutes) {
    }
}

I expected then auto-generated properties for TrainingDayDuration will override those from parent object, but it doesnt seem to be the case. Because in debugger I can see 2x Date and 2x DurationMinutes properties like:

debugger view

What is the right way to override it ? Or is there a better way how to approach this whole thing ?

Kebechet
  • 1,461
  • 15
  • 31
  • what is the point of extending `TrainingDayDurationDto` ? **if you wana separate dto from other usage then use two different classes and mapper** ... if you wana inheritance then ... don't ... just use one class and make it "INotifyPropertyChanged" at the begin ... by the way I'm just wonder how this source generator would act if you would make base class properties virtual ... – Selvin Dec 01 '22 at 15:54
  • @Selvin The virtual didnt do anything. My reasoning behind inheritance from DTOs is that the other class contains all properties like DTO+in many cases it extends it in some way (some extra methods and another properties). And I didnt want to have it in 1 class in order to separate the frontend-specific logic and transportation-only logic. – Kebechet Dec 02 '22 at 05:56
  • 1
    Optiona 0) not use inheritance and use AutoMapper ... Option A) implement `INotifyPropertyChanged` in base class ... Option B) create issue in toolkit project and ask for adding `override` when exists base property and it's `virtual` instead always giving `new` ... Option C) works fine with Fody.PropertyChanged with base `virtual` and derived `override` (as this is weaver(can change existing code) not source generator(create only new)) – Selvin Dec 02 '22 at 12:11

0 Answers0