DtoHydrator 1.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package DtoHydrator --version 1.0.0
                    
NuGet\Install-Package DtoHydrator -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="DtoHydrator" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="DtoHydrator" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="DtoHydrator" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add DtoHydrator --version 1.0.0
                    
#r "nuget: DtoHydrator, 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.
#:package DtoHydrator@1.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=DtoHydrator&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=DtoHydrator&version=1.0.0
                    
Install as a Cake Tool

Dto Hydrator

Overview

A C# library that hydrates a DTO using the Separation of Concerns (SoC) principle, where each handler is responsible for hydrating a specific part of the DTO. Multiple handlers can contribute to the hydration process, ensuring modular and maintainable code.

Problem to solve

In many applications, data transfer objects (DTOs) and view-models (VMs) are build from various sources (multiple entities) and sometimes, some properties are calculated values.

This library allows you to define interfaces for each part of the DTO that needs to be hydrated, and then implement handlers that will populate those parts. Each handler can receive dependencies through dependency injection, making it easy to manage complex hydration logic.

Dependencies

  • .NET Standard 2.1

Features

  • Handler-based hydration
  • Dependency injection support

Getting Started

TODO: Add nuget here...

Usage

Add the hydrator library via dependency injection

You can pass one or more assemblies. The library will scan the assemblies for hydrator handlers.

services.AddHydrator(Assembly.GetExecutingAssembly());

Define a Hydratable value and its handler


public  interface ICustomerName : IHydratableValue
{
    public Guid CustomerId {get;}
    
    public string CustomerName {get; set;}
    
    class Handler : IHydratorHandler<CustomerName>
    {
        private readonly ICacheService _cacheService;
        // You can inject dependencies in the constructor, provided by the DI container
        public Handler(ICacheService cacheService)
        {
            _cacheService  = cacheService; 
        }
        public Task HydrateAsync(CustomerName dto)
        {
            var name = _cacheService.Customers.First(x=> x.Id == dto.CustomerId).Name;   
            dto.CustomerName = name;
        }
    }
}

Implement your HydratableValue interfaces

public class InvoiceViewModel : ICustomerName
{
     public Guid InvoiceId {get; set;}
     
     public Guid CustomerId {get; set;}
     
     public string CustomerName {get; set;} // <-- hydrated value
}

Use the service

public class InvoiceQuery
{
    private readonly IHydratorService _hydrator;
    private readonly IInvoiceRepository _invoiceRepository;
    
    public InvoiceQuery(IHydratorService hydrator, IInvoiceRepository invoiceRepository)
    {
        _hydrator = hydrator;
        _invoiceRepository = invoiceRepository;
    }
       
    public InvoiceViewModel GetInvoice(Guid invoiceId)
    {
        var invoice = _invoiceRepository.GetById(invoiceId);
        var viewModel = new InvoiceViewModel
        {
            InvoiceId = invoice.Id,
            CustomerId = invoice.CustomerId
        };
        
        // Hydrate the view model
        _hydrator.HydrateAsync(viewModel).GetAwaiter().GetResult();
        
        return viewModel;
    }
}

Important Considerations

The Hydrator Service is registered with a scoped lifetime, meaning a new instance is created for each request.

A typical pattern is to use a scoped cache service to load all necessary data up front. This cache can then be injected into each hydrator handler, enabling efficient data access and reducing repeated database queries.

Use Cases

Avoid Duplicating Logic

Often, the same value needs to be mapped in multiple locations. For example, an entity’s name, such as a customer name, might be required in invoices, orders, statements, and more.

By using this library, you can implement a single handler to hydrate the customer name and reuse it wherever needed, eliminating redundant logic.

Allow complex ViewModel building

For complex view-models with multiple nested DTOs or advanced calculations, this library enables you to break down the hydration process into several handlers. Each handler manages a specific aspect of the view-model, promoting modularity and easier maintenance.

Examples

An example demonstrating a complex view-model hydrated with EF Core and SQLite is available in the repository.

Open Source & License

This project is open source, licensed under the MIT License.

Created by Julio Cachay G. in Chattanooga, TN, USA.

Contributing

Contributions are welcome! If you would like to report a bug, suggest a feature, or submit a pull request, please follow these steps:

  1. Fork the repository.
  2. Create a new branch for your changes.
  3. Make your changes and commit them with clear messages.
  4. Open a pull request describing your changes.

For major changes, please open an issue first to discuss what you would like to change.

By contributing, you agree that your contributions will be licensed under the MIT License.

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.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen 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.

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.2 133 6/22/2025
1.0.1 111 6/22/2025
1.0.0 112 6/21/2025