DynatestSourceGenerator 0.0.4.5
dotnet add package DynatestSourceGenerator --version 0.0.4.5
NuGet\Install-Package DynatestSourceGenerator -Version 0.0.4.5
<PackageReference Include="DynatestSourceGenerator" Version="0.0.4.5" />
paket add DynatestSourceGenerator --version 0.0.4.5
#r "nuget: DynatestSourceGenerator, 0.0.4.5"
// Install DynatestSourceGenerator as a Cake Addin #addin nuget:?package=DynatestSourceGenerator&version=0.0.4.5 // Install DynatestSourceGenerator as a Cake Tool #tool nuget:?package=DynatestSourceGenerator&version=0.0.4.5
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 MapFrom(Person instance)
{
Id = instance.Id;
Name = instance.Name;
return this;
}
public Person MapTo()
{
return new Person
{
Id = Id,
Name = Name,
};
}
}
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 MapFrom(Person instance)
{
Id = instance.Id;
Name = instance.Name;
return this;
}
public Person MapTo()
{
return new Person
{
Id = Id,
Name = Name,
};
}
}
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 MapFrom(Person instance)
{
Id = instance.Id;
Name = instance.Name;
Country = new CountryDTO().MapFrom(instance.Country);
BirthDate = instance.BirthDate;
return this;
}
public Person MapTo(Person instance)
{
return new Person
{
Id = Id,
Name = Name,
Country = Country.MapTo();
BirthDate = BirthDate,
};
}
}
One can also use a customized name for the DTO, which will be utilized instead of the default name "DTO".
[UseExistingDto("CountryB")]
public Country Country {get; set; }
The above code will generate CountryB instead of CountryDTO.
public CountryB Country {get; set; }
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 MapFrom method, 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 MapFrom method to map the properties from the original class to the generated class:
var personDto = new PersonDTO().MapFrom(person);
To use the MapTo method, you can create an instance of the generated class and call its MapTo method to map the properties from the generated class to the original class:
Person person = new PersonDTO(){ Id = 1, Name = "John", BirthDate = new DateTime(1980, 1, 1) }.MapTo();
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; }
[UseExistingDTO("StationDTO > WeatherForecastDTO")]
public WeatherForecast Forecasts { 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 WeatherForecastDTO Forecasts { get; set; }
public StationDTO MapFrom(Station instance)
{
Name = instance.Name;
Level = instance.Level;
Owner = instance.Owner;
return this;
}
public Station MapTo()
{
return new Station
{
Name = Name,
Level = Level,
Owner = Owner,
};
}
}
public class StationWithOnlyLevelDTO
{
public int Level { get; set; }
public StationWithNoNameDTO MapFrom(Station instance)
{
Level = instance.Level;
return this;
}
public StationWithNoNameDTO MapTo()
{
return new Station
{
Level = Level,
};
}
}
Limitations
Currently, the source generator does not provide any error messages when performing unlawful actions such as using UseExistingDto for a string. However, this feature will be included in upcoming updates. Furthermore, if UseExistingDto is utilized as a key-value pair for a Dictionary, the source generator is incapable of handling it.
Version
Current version: 0.4.3
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- DynatestSourceGenerator.Abstractions (>= 1.0.0.1)
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 |
---|
Version 0.4.5
Features
One of the most significant improvements is that the Source Generator can now handle all types and collections, making it easier than ever to generate source code for a wide range of applications.
In addition, the Source Generator now generates methods only when they are actually used in the application, resulting in more efficient code generation and reduced code bloat. This new functionality ensures that your code is leaner and more optimized than ever before.
Overall, these enhancements make the Dynatest Source Generator an even more powerful and versatile tool for developers, helping them to create high-quality source code faster and more efficiently.
Known Issues
The inputs for the attributes are all strings, which can cause issues. We recommend verifying that the inputs for the attributes are valid before using them in your code.
Thank you for using our software! If you encounter any issues or have any feedback, please do not hesitate to reach out to our support team.