RFC: Resource to Entity Mapping #112
Labels
enhancement
help wanted
RFC
Request for comments. These issues have major impact on the direction of the library.
Milestone
Uh oh!
There was an error while loading. Please reload this page.
Start Date: N/A
Status: WIP
RFC PR: N/A
Summary
Allow models to map to more than 1 resource.
Terminology
DbSet<>
on theDbContext
Motivation
Currently, there is a 1:1 relationship between the database entity and the exposed resource. There may be a need for different forms of an entity to be exposed. I see two different needs:
1
entity :N
resources (one-to-many)N
entities :1
resource (many-to-one)I believe the second need is already supported since this would require a custom implementation at the repository layer. The first however should be supported out-of-the-box and it currently is not.
Detailed Design
I think this is a relatively simple solution and that is to change the current dependency graph from:
to
This shows that mapping would take place in the service layer. An application could define a mapping that, if exists, would be used to map the final entity result (post-database call) into the resource. For this, I think it is safe to choose AutoMapper as the standard mapping tool. Users would then define their maps using the
ContextGraphBuilder
:and the controller might look like:
ContextGraphBuilder
The ContextGraphBuilder will have a dictionary of mappings:
Calling
builder.UseAutoMapper(AutoMapperConfig.GetMapper()
will create the resource maps for every entity. All ContextEntities will be assigned anEmptyMapping
which will handle calls for mappingTResource
toTEntity
whenTResource == TEntity
:As the ContextEntities are created, they will be assigned their maps:
Resource Services
In order to maintain backwards compatibility, the current resource service definitions with generic parameters should remain available. We can define an additional generic parameter overload:
A
IResourceService
implementation should then perform the mapping prior to returning the resource:Automapper IResourceMap
Unspecified Mappings
If a user defines a controller but no mapping has been defined, a 500 error will be returned.
Relationships
Mappings will be used to translate relationship names. In the following example, a request for
include=owner
would map toEntity.User
:Validation of these definitions should occur on app startup.
Constraints
The types
TResource
andTEntity
MUST implementIIdentifiable<TId>
whereTId
is of the same type for both classes:The text was updated successfully, but these errors were encountered: