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.
dotnet add package Gener8
This single package includes both the Roslyn source generator and Gener8.Abstractions (attributes, enums, repository contracts). No additional install is needed for basic DTO generation.
DynamoDB repositories:
dotnet add package Gener8.Extensions.DynamoDB
MongoDB repositories:
dotnet add package Gener8.Extensions.MongoDB
// 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; }
}The set/init accessor, the required modifier, and property initializers are preserved exactly as declared on the source model. Get-only properties are excluded unless they are set via a detected constructor (positional records, or any class with a matching constructor), in which case they are emitted with init and ToModel uses constructor-style initialization.
| Feature | Attribute / Parameter |
|---|---|
| Exclude specific properties | Ignore = [nameof(Model.Prop)] |
| Map a nested type to a DTO type | [TypeMapping(typeof(Address), typeof(AddressDto))] |
| Auto-map and generate companion DTOs | DtoNamespaces = ["MyApp.Models"] |
| Suppress a single auto type mapping | [IgnoreTypeMapping(typeof(T))] |
| 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")] |
| Make non-nullable properties nullable in the DTO | ForceNullable = [nameof(Model.Prop)] |
| Mapping extension methods | Generated automatically alongside every DTO |
| Generate a repository scaffold | Repository = RepositoryType.DynamoDb, RepositoryType.MongoDb, or RepositoryType.Custom |
| Dictionary properties with type-remapped keys/values | Handled automatically; extensions use ToDictionary(...) |
See docs/features.md for detailed examples of every feature.
[FromModel(typeof(Product), Ignore = [nameof(Product.InternalCode)])]
internal partial class ProductDto { }[FromModel(typeof(Order))]
[TypeMapping(typeof(Address), typeof(AddressDto))]
internal partial class OrderDto { }[FromModel(typeof(DerivedProduct), IncludeInherited = true)]
internal partial class DerivedProductDto { }// Address { Street, City, PostCode }
[FromModel(typeof(Order), Flatten = [nameof(Order.ShippingAddress)])]
internal partial class OrderDto { }
// Emits: ShippingAddressStreet, ShippingAddressCity, ShippingAddressPostCode[FromModel(typeof(Product))]
[RenameProperty(nameof(Product.InternalSku), "Sku")]
internal partial class ProductDto { }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
// {
// [return: NotNullIfNotNull(nameof(dto))]
// public static Product? ToModel(this ProductDto? dto) => dto is null ? null : new Product { ... };
// [return: NotNullIfNotNull(nameof(model))]
// public static ProductDto? ToDto (this Product? model) => model is null ? null : new ProductDto { ... };
// }
// Usage:
ProductDto dto = product.ToDto();
Product model = dto.ToModel();Type-mapped properties generate chained calls — dto.ShippingAddress.ToModel() and model.ShippingAddress.ToDto().
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 ProductRepository.g.cs:
// internal partial class ProductRepository : Gener8.DynamoDbRepository<Product, ProductDto>
// {
// public ProductRepository(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>. These abstract bases come from the extension packages rather than generated source.
The AWSSDK.DynamoDBv2 or MongoDB.Driver package must be referenced in the consuming project for DynamoDb/MongoDb respectively. Also add Gener8.Extensions.DynamoDB or Gener8.Extensions.MongoDB which provides the abstract base classes (DynamoDbRepository<TModel,TDto>, MongoDbRepository<TModel,TDto>) and converters.
| 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 |
dotnet build
dotnet test
To build individual packages:
dotnet build src/Gener8.Abstractions/Gener8.Abstractions.csproj
dotnet build src/Gener8/Gener8.csproj
dotnet build src/Gener8.Extensions.DynamoDB/Gener8.Extensions.DynamoDB.csproj
dotnet build src/Gener8.Extensions.MongoDB/Gener8.Extensions.MongoDB.csproj