Dekiru.DtoGenerator 0.2.1

Prefix Reserved
There is a newer version of this package available.
See the version list below for details.
dotnet add package Dekiru.DtoGenerator --version 0.2.1                
NuGet\Install-Package Dekiru.DtoGenerator -Version 0.2.1                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Dekiru.DtoGenerator" Version="0.2.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Dekiru.DtoGenerator --version 0.2.1                
#r "nuget: Dekiru.DtoGenerator, 0.2.1"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Dekiru.DtoGenerator as a Cake Addin
#addin nuget:?package=Dekiru.DtoGenerator&version=0.2.1

// Install Dekiru.DtoGenerator as a Cake Tool
#tool nuget:?package=Dekiru.DtoGenerator&version=0.2.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.

The DTO generator will, by design, not include properties from any base class of the source class. If you have that need, generate a DTO from the base class and have your other DTO inherit from that one.

The DTO generator will also only include public instance properties.

Attributes

The following attributes are used to configure the DTO generator:

[DtoFor<T>]

Marks a class as a DTO for the source class T. This is the attribute that triggers the DTO generation, and the only required attribute.

The most basic example:

[DtoFor<Foo>]
public partial class FooDto;

The attribute has the following parameters:

  • T: The type of the class for which the DTO is generated.
  • propertyStrategy (PropertyStrategy, optional): The strategy to use when generating properties. Default is PropertyStrategy.Implicit.
    • Implicit: Generate all properties that are not explicitly removed via any [Omit]-like attribute.
    • Explicit: Generate only properties that are explicitly included via [Pick].

Note: The target (DTO) class must be public and partial.

Note: Properties must be public and have public get and set methods to be included.

Note: If a property with the same name exists in the target and source classes, it will excluded. Such a property must at present have the same type in both classes.

Configuration attributes

The following attributes are used to configure the DTO generator:

Note: Some of these attributes have parameters that represents one or more property names. While [MyAttribute("Foo")], [MyAttribute(nameof(Bar.Foo))], or a combination of the two are valid, the source generator will produce a static nested class named _ with all eligible property names as constants.

public partial class FooDto
{
  // Properties omitted for brevity
  public static class _
  {
  	public const string FooId = nameof(FooId);
  }
}

This is useful when you want to reference the property names in a strongly typed manner, like so: [Omit(_.Foo)], and is less verbose than using nameof.


[Omit]

Omits one or more properties from the generated DTO. The attribute has the following parameters:

  • properties (params string[]): The names of the properties to omit.

    [OmitByNamespace]

    Omits all properties from the specified namespaces. The attribute has the following parameters:

    • namespaces (params string[]): The namespaces to omit. Regular expressions are supported, with a timeout of 0.1 seconds.

    [OmitByType]

    Omits all properties of the specified types. The attribute has the following parameters:

    • type (Type): The types to omit. The attribute supports unbounded generic types.

    [OmitByType<T>]

    Omits all properties of the specified type T. The attribute has no parameters.

    [OmitKeysByConvention]

    Omits all properties that are named Id or <Target>Id, with a case-insensitive match. The attribute has no parameters.

    [OmitReferenceProperties]

    Omits all properties that are reference types. The attribute has no parameters.

These attributes have no effect when the property strategy is PropertyStrategy.Explicit.


[Pick]

Only includes the specified properties in the generated DTO. The attribute has the following parameters:

  • properties (params string[]): The names of the properties to include.

This attribute has no effect when the property strategy is PropertyStrategy.Implicit.

[Using]

Adds any number of using directives to the generated code. The attribute has the following parameters:

  • namespaces (string[]): The namespaces to add.

[NoCopyMethod]

Disables the generation of the Copy method. The attribute has no parameters.

[NoCreateMethod]

Disables the generation of the Create method. The attribute has no parameters.

Member attributes

Some attributes are property specific, these are:

[ValueFor]

This attribute is only relevant for omitted properties, and is a way to ensure that their values are set. The attribute has the following parameters:

  • target (string): The name of the property to set.

The attribute can be used on a method, property, or field. They may have any visibility, but if the member is a method it must be parameter-less.

As an example, let's look at a source class with a Name property:

public class Foo
{
  public string Name { get; set; }
}

However, in our DTO we wish to separate first and last name. We can then use ValueFor to handle this:

[DtoFor<Foo>]
[Omit(_.Name)]
public class FooDto
{
  public string FirstName { get; set; }
  public string LastName { get; set; }

  [ValueFor(_.Name)] private string name => $"{FirstName} {LastName}";
}
Product 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.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.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. 
Compatible target framework(s)
Included target framework(s) (in package)
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
0.2.9 64 1/14/2025
0.2.8 133 12/12/2024
0.2.7 90 12/12/2024
0.2.6 96 12/4/2024
0.2.5 94 12/4/2024
0.2.4 81 12/2/2024
0.2.3 83 12/2/2024
0.2.2 87 12/2/2024
0.2.1 87 12/2/2024
0.2.0 90 12/2/2024
0.0.11 101 11/26/2024
0.0.10 94 11/18/2024
0.0.9 89 11/18/2024
0.0.8 104 11/12/2024
0.0.7 101 11/12/2024
0.0.6 100 11/11/2024
0.0.5 101 11/11/2024
0.0.4 100 11/8/2024
0.0.3 94 11/8/2024
0.0.2 93 11/8/2024
0.0.1 98 11/8/2024