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.
Let checkout it out: https://github.com/quyvu01/OfX!