OfX is an open-source, which focus on Attribute-based Data Mapping, simplifying data handling across services and enhancing maintainability.
Attribute-based Data Mapping in OfX is a feature that lets developers annotate properties in their data models with custom attributes. These attributes define how and from where data should be fetched, eliminating repetitive code and automating data retrieval.
For example, imagine a scenario where Service A needs a user’s name stored in Service B. With Attribute-based Data Mapping, Service A can define a UserName property annotated with [UserOf(nameof(UserId))]
. This tells the system to automatically retrieve the UserName based on UserId, without writing custom code each time.
Example:
public sealed class SomeDataResponse
{
public string Id { get; set; }
public string UserId { get; set; }
[UserOf(nameof(UserId), Expression = "Email")]
public string UserEmail { get; set; }
[UserOf(nameof(UserId))] public string UserName { get; set; }
...
}
The [UserOf]
annotation acts as a directive to automatically retrieve UserName
based on UserId
,you can also fetch custom fields as Email
on the User Table using Expression like [UserOf(nameof(UserId), Expression="Email")]
. This eliminates the need for manual mapping logic, freeing developers to focus on core functionality rather than data plumbing.
To install the OfX package, use the following NuGet command:
dotnet add package OfX
Or via the NuGet Package Manager:
Install-Package OfX
Add the OfX to your service configuration to register OfX:
builder.Services.AddOfX(cfg =>
{
cfg.AddAttributesContainNamespaces(typeof(WhereTheAttributeDefined).Assembly);
cfg.AddHandlersFromNamespaceContaining<SomeHandlerAssemblyMarker>(); <- Add this one when you want to self-handle the request as the example at the end of this guide. Otherwise, if you install the package OfX-gRPC, there is no need to add this one anymore!
});
Registers assemblies that contain the attributes, used by OfX for data mapping.
The Attribute should be inherited from OfXAttribute
will be scanned!
Parameters:
Assembly
: The assembly containing the (OfX) attributes.
Registers assemblies that contain handlers responsible for processing queries or commands for data retrieval.
Handlers are the execution units that resolve attributes applied to models.
If this function is not called. The default value ItemsResponse<OfXDataResponse>
is returned!
Parameters:
Type
: A marker type within the assembly that includes the handler implementations.
Example:
cfg.AddHandlersFromNamespaceContaining<SomeHandlerAssemblyMarker>();
Here, AddHandlersFromNamespaceContaining
is a type within the assembly where your handler logic resides.
Apply the attribute to your properties like this:
public sealed class SomeDataResponse
{
public string Id { get; set; }
public string UserId { get; set; }
[UserOf(nameof(UserId), Expression = "Email")]
public string UserEmail { get; set; }
[UserOf(nameof(UserId))]
public string UserName { get; set; }
// Add other properties as needed
}
Implement a handler to process data requests. For example:
public class UserRequestHandler(): IMappableRequestHandler<UserOfAttribute>
{
public async Task<ItemsResponse<OfXDataResponse>> RequestAsync(RequestContext<UserOfAttribute> request)
{
// Implement data fetching logic here (e.g., via REST, RPC, or gRPC)
}
}
Enjoy your moment!
Package Name | Description | .NET Version | Document |
---|---|---|---|
OfX | OfX core | 8.0, 9.0 | This Document |
OfX-EFCore | This is the OfX extension package using EntityFramework to fetch data | 8.0, 9.0 | ReadMe |
OfX-gRPC | OfX.gRPC is an extension package for OfX that leverages gRPC for efficient data transportation. | 8.0, 9.0 | ReadMe |