zeshan321 / DTOGenerator

Experimental zero code compile time DTO generator and mapper using Source Generators in C#

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

DTOGenerator

Experimental zero code compile time DTO generator and mapper using Source Generators in C#.

Attributes

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.

Example

Screenshot Link Description
Station class Link This will create two DTOs "StationDTO" and "StationWithNoNameDTO". "Name" will be excluded in "StationWithNoNameDTO" and "Level" will be excluded in all.
WeatherForcast class 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".

Example of generated classes from above

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;
   }
}

About

Experimental zero code compile time DTO generator and mapper using Source Generators in C#

License:MIT License


Languages

Language:C# 100.0%