DynatestSourceGenerator 0.0.3.8
dotnet add package DynatestSourceGenerator --version 0.0.3.8
NuGet\Install-Package DynatestSourceGenerator -Version 0.0.3.8
<PackageReference Include="DynatestSourceGenerator" Version="0.0.3.8" />
paket add DynatestSourceGenerator --version 0.0.3.8
#r "nuget: DynatestSourceGenerator, 0.0.3.8"
// Install DynatestSourceGenerator as a Cake Addin #addin nuget:?package=DynatestSourceGenerator&version=0.0.3.8 // Install DynatestSourceGenerator as a Cake Tool #tool nuget:?package=DynatestSourceGenerator&version=0.0.3.8
Dynatest Source Generator
Dynatest Source Generator is a package that generates source code based on attributes added to your C# code. This package can help automate the creation of Data Transfer Objects (DTOs) or other code structures by generating code from attributes.
Installation
You can install Dynatest Source Generator using the following command in the Package Manager Console:
Install-Package DynatestSourceGenerator
Or, you can install it using the .NET CLI:
dotnet add package DynatestSourceGenerator
Usage
To use Dynatest Source Generator, you need to add the [GenerateDto] attribute to the classes that you want to generate code for. The attribute takes optional arguments to specify the name of the generated class. For example:
[GenerateDto]
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
}
The above code will generate a new class called PersonDTO that has the same properties as the Person class.
public class PersonDto
{
public int Id { get; set; }
public string Name { get; set; }
public PersonDto Map(Person instance)
{
Id = instance.Id;
Name = instance.Name;
return this;
}
}
Excluding Properties
You can exclude properties from the generated class by adding the [ExcludeProperty] attribute to the properties that you want to exclude. For example:
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
[ExcludeProperty]
public DateTime BirthDate { get; set; }
}
The above code will exclude the BirthDate property from the generated class.
public class PersonDto
{
public int Id { get; set; }
public string Name { get; set; }
public PersonDto Map(Person instance)
{
Id = instance.Id;
Name = instance.Name;
return this;
}
}
Using existing DTO
One can utilize an already generated DTO from a different class by applying the [UseExistingDto] attribute to the respective property, thus indicating the preference to use the existing DTO version. It is important to note that this approach is only applicable to classes that already have a DTO version generated. An illustrative instance is provided below for better comprehension:
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
[UseExistingDto]
public Country Country {get; set; }
public DateTime BirthDate { get; set; }
}
The above code will use the DTO version of Country in the generated class.
public class PersonDto
{
public int Id { get; set; }
public string Name { get; set; }
public CountryDto Country {get; set; }
public DateTime BirthDate { get; set; }
public PersonDto Map(Person instance)
{
Id = instance.Id;
Name = instance.Name;
Country = new StationDTO().Map(instance.Country);
BirthDate = instance.BirthDate;
return this;
}
}
Attributes
Attribute | Description | Notes |
---|---|---|
GenerateDto | Incorporate this attribute to generate a DTO for a class. | If no DTO name is provided, the DTOGenerator will automatically create one called "classNameDTO". Moreover, the DTOGenerator can create an unlimited number of DTOs from the same class. |
ExcludeProperty | Add this attribute to exclude a particular property in DTO. | You can specify the name of a DTO to exclude a property in specific DTOs; otherwise, it will be excluded in all DTOs. |
UseExistingDto | Use this attribute to incorporate an existing DTO for an object nested in another DTO. | You can specify a particular DTO to utilize based on. |
Using the Generated Class
To use the generated class, you first need to create an instance of the original class:
var person = new Person { Id = 1, Name = "John", BirthDate = new DateTime(1980, 1, 1) };
Then, you can create an instance of the generated class and call its Map method to map the properties from the original class to the generated class:
var personDto = new PersonDTO().Map(person);
Advanced Usage
One has the alternative to produce numerous Data Transfer Objects (DTOs) simultaneously, a task that can be accomplished with ease by including parentheses and appending parameter strings, as demonstrated in the following instance:
[GenerateDto("StationDTO", "StationWithOnlyLevelDTO")]
public class Station
{
[ExcludeProperty("StationWithOnlyLevelDTO")]
public string Name { get; set; }
public int Level { get; set; }
[ExcludeProperty("StationWithOnlyLevelDTO")]
public string Owner { get; set; }
}
The code presented below displays the resulting classes, revealing that StationWithOnlyLevelDTO solely encompasses a Level property, whereas StationDTO comprehends all properties derived from the Station class.
public class StationDTO
{
[ExcludeProperty("StationWithOnlyLevelDTO")]
public string Name { get; set; }
public int Level { get; set; }
[ExcludeProperty("StationWithOnlyLevelDTO")]
public string Owner { get; set; }
public StationDTO Map(Station instance)
{
Name = instance.Name;
Level = instance.Level;
Owner = instance.Owner;
return this;
}
}
public class StationWithOnlyLevelDTO
{
public int Level { get; set; }
public StationWithNoNameDTO Map(Station instance)
{
Level = instance.Level;
return this;
}
}
Limitations
The Dynatest Source Generator exclusively generates classes that have public properties. It does not provide support for generating code for classes lacking public properties.
Version
Current version: 0.0.1
Learn more about Target Frameworks and .NET Standard.
-
.NETStandard 2.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|