DtoHydrator 1.0.2

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

Dto Hydrator

More information on the Wiki

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

dotnet add package DtoHydrator --version 1.0.0

Usage

Setup the DI Container

services.AddHydrator(Assembly.GetExecutingAssembly());

Example: Use case

We will define a DTO example that has 3 properties: Dividend, Divisor and Quotient.

Then, we will use the library to perform two operations:

  1. Set the Dividend And Divisor
  2. Calculate the Quotient.

This is an example intended to demonstrate how the library works.

Add a Hydrator that sets the Dividend and Divisor

Important:

  1. Following the Interface Segregation Principle (the I in SOLID), the hydratable value only exposes the members needed to fulfill its purpose.
  2. Using IHighPriorityHydratableValue ensures this value is hydrated first.
  3. Nesting the Handler class is optional and provided here for convenience.

You can inject any scoped service in the Handler, which allows you to perform operations against a database, a cache or other services for more complex use cases

public interface IHighPriorityExample : IHighPriorityHydratableValue
{
    decimal Dividend { get; set; }

    decimal Divisor { get; set; }
        
    public class Handler : IHydratorHandler<IHighPriorityExample>
    {
        public Task HydrateAsync(IHighPriorityExample dto)
        {
            dto.Dividend = 100;
            dto.Divisor = 10;
            return Task.CompletedTask;
        }
    }
}

Add a Hydrator that calculates the Quotient

Important:

  1. Notice that Dividend and Divisor are read-only properties here, meaning this hydrator only reads them to perform its calculation.
  2. The Quotient property has a setter, indicating that this hydrator is responsible for assigning its value.
public interface ILowPriorityExample : IHydratableValue
{
    decimal Dividend { get; }

    decimal Divisor { get; }

    decimal Quotient { get; set; }
        
    public class Handler : IHydratorHandler<ILowPriorityExample>
    {
        public Task HydrateAsync(ILowPriorityExample dto)
        {
            // Divisor and Dividend has values set before by the high priority handler: HighPriorityHydraterExample.Handler
            dto.Quotient = dto.Dividend / dto.Divisor;

            return Task.CompletedTask;
        }
    }
}

Use the Hydrators to tell the library which values should be populated

The code below indicates that the DtoExample is can be hydrated with two different Hydrators, as shown above, the first one sets the Dividend and Divisor and the second one calculates the Quotient.

public class DtoExample : 
    IHighPriorityExample,
    ILowPriorityExample
{
    public decimal Dividend { get; set; }
    public decimal Divisor { get; set; }
    public decimal Quotient { get; set; }
}

Use the IHydratorService

 public class DemoUseCase
    {
        private readonly IHydratorService _hydrator;

        public DemoUseCase(IHydratorService hydrator)
        {
            _hydrator = hydrator;
        }
        
        public async Task<DtoExample> GetDtoAsync()
        {
            var dto = new DtoExample();
            await _hydrator.HydrateAsync(dto);
            return dto;
        }
    }

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 126 6/22/2025
1.0.1 110 6/22/2025
1.0.0 111 6/21/2025