Dekiru.DtoGenerator
0.0.1
Prefix Reserved
See the version list below for details.
dotnet add package Dekiru.DtoGenerator --version 0.0.1
NuGet\Install-Package Dekiru.DtoGenerator -Version 0.0.1
<PackageReference Include="Dekiru.DtoGenerator" Version="0.0.1" />
paket add Dekiru.DtoGenerator --version 0.0.1
#r "nuget: Dekiru.DtoGenerator, 0.0.1"
// Install Dekiru.DtoGenerator as a Cake Addin #addin nuget:?package=Dekiru.DtoGenerator&version=0.0.1 // Install Dekiru.DtoGenerator as a Cake Tool #tool nuget:?package=Dekiru.DtoGenerator&version=0.0.1
DTO Generator
A Source Generator that generates DTO classes from existing classes. Configuration is done via attributes, as described below. All generated code should be compatible with .NET Standard 2.0 or later.
Attributes
Import note: C# allows for grouping attributes:
[Foo, Bar]
. At runtime, there is no difference between this and[Foo] [Bar]
. However, the source generator uses the grouping to determine which attributes are related to each other. The attributes[Dto.When]
and[Dto.NoCopy]
affect other property attributes in the same group.[Dto.Using]
is used to add additional using directives to the generated code, and must be in the same group as a[Dto]
attribute.
All attributes (except [Dto]
) are implemented as nested classes of the [Dto]
attribute. This is to avoid cluttering the global namespace with attribute names. This also means that one can use a static using directive to simplify the attribute names:
using static Dekiru.DtoGenerator.Attributes;
using static Dekiru.DtoGenerator.Attributes.Dto;
Then the following is valid:
[Include] public string Name { get; set; }
Without the static using directive, the same code would look like this:
[Dto.Include] public string Name { get; set; }
The following attributes are used to configure the DTO generator:
Class attributes
[Dto]
Marks a class for DTO generation. A class may have multiple DTOs generated for it, each with a different set of properties. Each [Dto]
attribute must be in a separate attribute group:
OK:
[Dto("Foo", "DtoNamespace")]
[Dto("Bar", "DtoNamespace")]
Not OK:
[Dto("Foo", "DtoNamespace"), Dto("Bar", "DtoNamespace")]
The attribute has the following parameters:
- name (
string
): The name of the DTO. This is used as the class name of the generated DTO. The$
variable can be used to reference the name of the source class. - namespace (
string
): The namespace of the generated DTO. The$
variable can be used to reference the namespace of the source class. - baseClass (
string
, optional): The base class of the generated DTO. - propertyStrategy (
PropertyStrategy
, optional): The strategy to use when generating properties. Default isPropertyStrategy.Implicit
.Implicit
: Generate all properties that are not marked with[Dto.Exclude]
.Explicit
: Generate only properties that are marked with[Dto.Include]
.
[Dto.Using]
Adds any number of using directives to the generated code. This attribute must be in the same attribute group as a [Dto]
attribute. The attribute has the following parameters:
- namespaces (
params string[]
): The namespaces to add.
[Dto.Partial]
Indicates that the generated DTO should be a partial class. This attribute must be in the same attribute group as a [Dto]
attribute.
Property attributes
[Dto.Type]
Specifies a conversion function to use when copying a property. The attribute has the following parameters:
- type (
string
): The type of the property in the generated DTO. - expression (
string
): An expression that converts the property value. The expression is evaluated in the context of the source object. The expression must return a value of the same type as the property. The expression may reference the property value using the$
variable.
This attribute is only relevent when using [Dto.Type]
to specify a different type for the property.
[Dto.Exclude]
Excludes a property from the generated DTO. This attribute has no effect when the property strategy is PropertyStrategy.Explicit
.
[Dto.Include]
Includes a property in the generated DTO. This attribute has no effect when the property strategy is PropertyStrategy.Implicit
.
[Dto.NoCopy]
The default behavior of the DTO generator is to copy all other attributes of a property to the generated DTO. This attribute prevents this behavior:
[Dto.NoCopy, JsonProperty("name")]
[Dto.For]
Used to conditionally apply other property attributes. The attribute has the following parameters:
- appliesTo (
params string[]
): A list of DTO names to which the attribute applies.
[Dto.For("Foo"), Dto.Include] public string Name { get; set; }
Examples
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.