DtoHydrator 1.0.2
dotnet add package DtoHydrator --version 1.0.2
NuGet\Install-Package DtoHydrator -Version 1.0.2
<PackageReference Include="DtoHydrator" Version="1.0.2" />
<PackageVersion Include="DtoHydrator" Version="1.0.2" />
<PackageReference Include="DtoHydrator" />
paket add DtoHydrator --version 1.0.2
#r "nuget: DtoHydrator, 1.0.2"
#:package DtoHydrator@1.0.2
#addin nuget:?package=DtoHydrator&version=1.0.2
#tool nuget:?package=DtoHydrator&version=1.0.2
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:
- Set the Dividend And Divisor
- Calculate the Quotient.
This is an example intended to demonstrate how the library works.
Add a Hydrator that sets the Dividend and Divisor
Important:
- Following the Interface Segregation Principle (the I in SOLID), the hydratable value only exposes the members needed to fulfill its purpose.
- Using
IHighPriorityHydratableValue
ensures this value is hydrated first. - 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:
- Notice that
Dividend
andDivisor
are read-only properties here, meaning this hydrator only reads them to perform its calculation. - 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:
- Fork the repository.
- Create a new branch for your changes.
- Make your changes and commit them with clear messages.
- 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 | Versions 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. |
-
.NETStandard 2.1
- Microsoft.Extensions.DependencyInjection (>= 2.1.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.