Base solution for your next web application
Ends in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

Right way (pattern/Approach) to add a new ReadOnly calculated field to Client Dtos #6050


User avatar
0
jtallon created

Hi there,

I am using ASP.NETCORE and Augular.

If I want to add a new ReadOnly calculated field (TotalField) to a Dto that has been generated using the Swagger refresh.bat in the service-proxies.ts, what is the best way to do this?

  • Goal is that when the user enters a value into FieldOne and/or FieldTwo on the UI, a TotalField shows the calculation result as a label
    • i.e: TotalField = (FieldOne x FieldTwo)

Any thoughts on the right approach?

Regards John


3 Answer(s)
  • User Avatar
    0
    maliming created
    Support Team

    as far as know. nswag just can generate DTO in client. you need achieve it in client by your self.

    Use angular template-syntax to calculate it automatically and submit it to the backend. Of course, you can verify it again if you need the backend.

  • User Avatar
    0
    alexanderpilhar created

    What about this in entity class:

    [NotMapped]
    public int TotalField { get { return FieldOne * FieldTwo; } }
    

    now you can have a DTO as usual:

    public int TotalField { get; set; }
    
  • User Avatar
    0
    ryancyq created
    Support Team

    you can skip mapping for TotalField in auto mapper entity-dto or dto-entity configuration.

    see https://github.com/aspnetzero/aspnet-zero-core/blob/4180b6eccb0c02e14a17d1e383ed3906f6174cdd/aspnet-core/src/MyCompanyName.AbpZeroTemplate.Application/CustomDtoMapper.cs#L109