HamedStack.DataShaping
2.0.0
dotnet add package HamedStack.DataShaping --version 2.0.0
NuGet\Install-Package HamedStack.DataShaping -Version 2.0.0
<PackageReference Include="HamedStack.DataShaping" Version="2.0.0" />
<PackageVersion Include="HamedStack.DataShaping" Version="2.0.0" />
<PackageReference Include="HamedStack.DataShaping" />
paket add HamedStack.DataShaping --version 2.0.0
#r "nuget: HamedStack.DataShaping, 2.0.0"
#:package HamedStack.DataShaping@2.0.0
#addin nuget:?package=HamedStack.DataShaping&version=2.0.0
#tool nuget:?package=HamedStack.DataShaping&version=2.0.0
DataShaper Utility for ASP.NET Core
The DataShaper<T> service allows you to shape objects and collections dynamically by selecting specific fields at runtime. This is useful for implementing dynamic field selection in REST APIs (e.g., for performance or customization).
🔧 Features
- Dynamically select properties of an object or collection.
- Useful for building clean, efficient API responses.
- Integrates easily with ASP.NET Core's Dependency Injection (DI) system.
📦 Installation
Just add the IDataShaper<T> and DataShaper<T> classes to your project. Then register the service in the DI container.
🧩 Registering with Dependency Injection
Add this to your Startup.cs (ASP.NET Core 5) or Program.cs (ASP.NET Core 6+):
builder.Services.AddScoped(typeof(IDataShaper<>), typeof(DataShaper<>));
This allows the service to be injected anywhere using generics.
✅ Example Usage
Model
public class Person
{
public int Id { get; set; }
public string FirstName { get; set; } = default!;
public string LastName { get; set; } = default!;
public int Age { get; set; }
}
Controller or Service
public class PeopleService
{
private readonly IDataShaper<Person> _dataShaper;
public PeopleService(IDataShaper<Person> dataShaper)
{
_dataShaper = dataShaper;
}
public IEnumerable<Dictionary<string, object?>> GetPeopleShaped(IEnumerable<Person> people, string? fields)
{
return _dataShaper.ShapeData(people, fields);
}
}
Sample Usage
var people = new List<Person>
{
new Person { Id = 1, FirstName = "Alice", LastName = "Smith", Age = 30 },
new Person { Id = 2, FirstName = "Bob", LastName = "Johnson", Age = 25 }
};
var shaped = _dataShaper.ShapeData(people, "Id,FirstName");
Output:
[
{ "Id": 1, "FirstName": "Alice" },
{ "Id": 2, "FirstName": "Bob" }
]
If fields is null or empty, all properties will be included.
📝 Notes
- Property names in the
fieldsstring are case-insensitive. - If a field in the string doesn’t exist on the model, it will be ignored.
- Works best when combined with API query parameters like
?fields=Id,Name.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 is compatible. 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 is compatible. 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. |
-
net8.0
- No dependencies.
-
net9.0
- No dependencies.
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 |
|---|