Skip to content

Repository files navigation

Gener8

A C# Roslyn source generator that copies public properties from a model class into a decorated partial DTO class at compile time — no reflection, no runtime overhead.

NuGet CI

Installation

dotnet add package Gener8

The package is a development dependency — it is automatically marked PrivateAssets=all and adds no runtime reference to your project.

Quick start

// Your existing model
public class Product
{
    public required string Name { get; set; }
    public string Description { get; set; } = "";
    public decimal Price { get; set; }
    public bool InStock { get; set; }
}

// Your DTO — just add the attribute and make it partial
[FromModel(typeof(Product))]
internal partial class ProductDto { }

The generator emits at build time:

// <auto-generated/>
#nullable enable

internal partial class ProductDto
{
    public required string Name { get; set; }
    public string Description { get; set; } = "";
    public decimal Price { get; set; }
    public bool InStock { get; set; }
}

All accessor kinds (get, set, init), the required modifier, and property initializers are preserved exactly as declared on the source model.

Features

Feature Attribute / Parameter
Exclude specific properties Ignore = [nameof(Model.Prop)]
Map a nested type to a DTO type [TypeMapping(typeof(Address), typeof(AddressDto))]
Include properties from base classes IncludeInherited = true
Inline (flatten) a nested object Flatten = [nameof(Model.Prop)]
Rename a property in the output [RenameProperty("OldName", "NewName")]
Mapping extension methods Generated automatically alongside every DTO
Generate a repository scaffold Repository = RepositoryType.DynamoDb, RepositoryType.MongoDb, or RepositoryType.Custom

See docs/features.md for detailed examples of every feature.

Examples

Ignore properties

[FromModel(typeof(Product), Ignore = [nameof(Product.InternalCode)])]
internal partial class ProductDto { }

Type mapping

[FromModel(typeof(Order))]
[TypeMapping(typeof(Address), typeof(AddressDto))]
internal partial class OrderDto { }

Include inherited properties

[FromModel(typeof(DerivedProduct), IncludeInherited = true)]
internal partial class DerivedProductDto { }

Flatten a nested object

// Address { Street, City, PostCode }
[FromModel(typeof(Order), Flatten = [nameof(Order.ShippingAddress)])]
internal partial class OrderDto { }
// Emits: ShippingAddressStreet, ShippingAddressCity, ShippingAddressPostCode

Rename a property

[FromModel(typeof(Product))]
[RenameProperty(nameof(Product.InternalSku), "Sku")]
internal partial class ProductDto { }

Mapping extension methods

Every [FromModel] DTO automatically gets a companion extensions class with ToModel and ToDto methods:

[FromModel(typeof(Product))]
internal partial class ProductDto { }

// Generated automatically:
// internal static class ProductDtoExtensions
// {
//     public static Product    ToModel(this ProductDto dto)    { ... }
//     public static ProductDto ToDto  (this Product    model)  { ... }
// }

// Usage:
ProductDto dto = product.ToDto();
Product    model = dto.ToModel();

Type-mapped properties generate chained calls — dto.ShippingAddress.ToModel() and model.ShippingAddress.ToDto().

Repository scaffold

Set Repository to generate a concrete repository class that inherits from the SDK-specific abstract base provided by Gener8:

[FromModel(typeof(Product), Repository = RepositoryType.DynamoDb)]
internal partial class ProductDto { }

// Generates ProductDtoRepository.g.cs:
// internal partial class ProductDtoRepository : Gener8.DynamoDbRepository<Product, ProductDto>
// {
//     public ProductDtoRepository(IDynamoDbRepositoryContext context) : base(context) {}
//     protected override Product    ToModel(ProductDto dto)   => dto.ToModel();
//     protected override ProductDto ToDto  (Product    model) => model.ToDto();
// }

Use RepositoryType.MongoDb for a MongoDB variant — it receives an IMongoDbRepositoryContext and sets the collection name to the DTO class name automatically.

Use RepositoryType.Custom to get a partial scaffold backed by Gener8.RepositoryBase<TModel, TDto>. The constructor takes an IRepositoryContext (empty marker interface — wrap your own DB context). No CRUD methods are pre-generated; add them in a second partial class file. No extra NuGet package required.

DynamoDbRepository<TModel, TDto> implements ICompositeKeyRepository<TModel> (single- and composite-key CRUD). MongoDbRepository<TModel, TDto> implements IRepository<TModel>. All abstract bases are emitted only when at least one DTO requests them, so no SDK-type references leak into projects that do not use repositories.

The AWSSDK.DynamoDBv2 or MongoDB.Driver package must be referenced in the consuming project for DynamoDb/MongoDb respectively.

Documentation

Document Contents
Getting started Installation, project setup, first DTO
Features All features with full code examples
Attribute reference Complete API reference for every attribute and parameter
How it works Generator internals and Roslyn pipeline
Contributing Build, test, NuGet packaging, CI/CD

Building from source

dotnet build src/Gener8/Gener8.csproj
dotnet test

License

MIT

About

A dto source generator from model

Resources

Contributing

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages