UnitGeneratorTemplate 1.0.0

dotnet add package UnitGeneratorTemplate --version 1.0.0
NuGet\Install-Package UnitGeneratorTemplate -Version 1.0.0
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="UnitGeneratorTemplate" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add UnitGeneratorTemplate --version 1.0.0
#r "nuget: UnitGeneratorTemplate, 1.0.0"
#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 UnitGeneratorTemplate as a Cake Addin
#addin nuget:?package=UnitGeneratorTemplate&version=1.0.0

// Install UnitGeneratorTemplate as a Cake Tool
#tool nuget:?package=UnitGeneratorTemplate&version=1.0.0

UnitGeneratorTemplate

UnitGeneratorTemplate is a template package to automatically generate Value Objects using UnitGenerator in bulk by describing settings in CSV.

DDD's ValueObject requires a large amount of similar code to be written.

The UnitGenerator is a library that solves those issues.

It's very well done, and it scratches an itch, but it's a lot of work to produce large quantities of it...

UnitGeneratorTemplate is a solution to that problem.

Using the T4 template and CSV, you can create arbitrary Value Objects using UnitGenerator in bulk.

Getting Started

Install UnitGenerator and UnitGeneratorTemplate from NuGet.

Install-Package UnitGenerator
Install-Package UnitGeneratorTemplate

パッケージをインストールすると、2つのファイルが追加されます。

  • UnitGenerator.csv
  • UnitGenerator.tt

Define the Unit you want to generate in UnitGenerator.csv and generate the code in UnitGenerator.tt.

The following screenshot shows a CSV opened with the "Edit csv" extension in Visual Studio Code.

alternate text is missing from this package README image

The CSV is specified as follows

Column Description Example
Name Name of struct to be generated UnitId
Type Value type int
Description Type comment of the struct to be generated Value object representing the ID of the Unit
Format ToString format {0:###,###,###}
ImplicitOperator以降 Attributes specified for UnitGenerator. Empty if not specified. If not specified, empty; if specified, other.

After editing the CSV, open the UnitGenerator.tt file and press "Ctrl+s" or press "Run Custom Tool" from the UnitGenerator.tt context menu.

A UnitGenerator.cs file will then be created under UnitGenerator.tt as shown below.

alternate text is missing from this package README image

Validate

The use of Validate is somewhat different from other options.

When using Validate, you can specify the validation rule in one of the following ways.

  1. Put the validation conditions in the Validate column of the CSV.
  2. implement your own Validate method in a partial.

Include the condition in the Validate column of the CSV.

For example, in the case of an int type Unit, if the value is less than 10, the Validate column should be as follows

Name Type Validate
UserId int value < 10

At this time, edit the UnitGenerator.tt file if you wish to change the exception class and message.

<#@ template debug="false" hostspecific="true" language="C#" #>
...
<#  
var validateException = "InvalidOperationException";
var validateExceptionMessage = "Invalid value range: {value}";
#>

Change validateException and validateExceptionMessage to any values.

Implement your own Validate method in partial

In this case, only validation is specified in the CSV as follows.

Name Type Validate
UserId int

Any string of at least 1 and no more than 5 characters may be specified.

If it is longer than 6 characters, it is assumed that an expression is specified.

The Validate method should be implemented as partial as follows

public partial struct UserId
{
    private partial void Validate()
    {
        if (5 < value) throw new InvalidOperationException($"Invalid value range: {value}");
    }
}
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. 
.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
1.0.0 249 5/19/2022