Experimental zero code compile time DTO generator and mapper using Source Generators in C#.
Attribute | Description | Notes |
---|---|---|
GenerateDto | Add to class to generate DTO. | If DTO name isn't supplied, by default DTOGenerator will create one called "classNameDTO". DTOGenerator can create unlimited amount of DTOs from the same class. Refer to example below. |
ExcludeProperty | Add to exclude property in DTO. | You can supply a name of a DTO to only exclude in specific ones, else it will be excluded in all DTOs. |
UseExistingDto | Add to use an existing DTO for an object that is nested in another DTO. | You can supply a specific DTO to use based off of. |
Screenshot | Link | Description |
---|---|---|
Link | This will create two DTOs "StationDTO" and "StationWithNoNameDTO". "Name" will be excluded in "StationWithNoNameDTO" and "Level" will be excluded in all. | |
Link | This will create two DTOs "WeatherForecastDTO" and "TestingWeather". "TemperatureF" will be excluded in all DTOs. "Summary" will be excluded in the "TestingWeather" DTO. "StationWithNoNameDTO" will be used in "TestingWeather" but in "WeatherForecastDTO" it will use the default "StationDTO". |
public class StationDTO
{
public string Name { get; set; }
public StationDTO Map(Station instance)
{
Name = instance.Name;
return this;
}
}
public class StationWithNoNameDTO
{
public StationWithNoNameDTO Map(Station instance)
{
return this;
}
}
public class WeatherForecastDTO
{
public DateTime Date { get; set; }
public int TemperatureC { get; set; }
public string Summary { get; set; }
public StationDTO Station { get; set; }
public WeatherForecastDTO Map(WeatherForecast instance)
{
Date = instance.Date;
TemperatureC = instance.TemperatureC;
Summary = instance.Summary;
Station = new StationDTO().Map(instance.Station);
return this;
}
}