Search code examples
c#angulardto

How to adapt DTO (data transfer object) to my angular project?


I need to know how to adapt C# DTO's to my Angular 6 project.

For example I have this DTO:

using System;
using System.Collections.Generic;
using System.Text;

namespace TriageMedico.DTOs
{
    public class BaseDto
    {
        public int Id { get; set; }
    }
}

I need to know how to use it, in order to generate/create a model, and therefore use it to send HTTP requests.


Solution

  • Just make an interface on typescript

    export interface BaseDTO {
       id: number;
    }
    

    When you do the httpRequest to the backend you type the response as BaseDTO

    myService.subscribe( (response: BaseDTO) => { ... some code here... } );

    I don't know which (the HttpClient or Angular itself) but it is smart enough to map the properties of your C# interface to the typescript. But you need to match the names.