-
-
Notifications
You must be signed in to change notification settings - Fork 158
Fluent API for building the resource graph #776
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
If we compare it with EF Core, then the ResourceTypeBuilder is equivalent to ModelBuilder, correct? In #771 PR the ResourceMapping is where you intend to do the mapping. In any case, the Builder itself is responsible for creating the mapping, but we still need a way to instruct the application where to locate the mappings. Assuming the developer will want to have the Mappings in a separate assembly, or would like to load specific mappings. In EF core there's also a clear separation between IEntityTypeConfiguration which is used for per Entity configuration, and DbContext which provides the means to apply the Configurations by calling ApplyConfiguration or ApplyConfigurationFromAssembly. Since JADNC already has a discovery mechanism which is visible from startup, I just extended it with AddResourceMapping, so we can either use AddAssembly or this method to load Mappings. |
My proposal is simpler, but it enables setup from external assemblies too: services.AddJsonApi(
options => options.Namespace = "api/v1",
resources: builder =>
{
var assembly = Assembly.Load(...);
var customMapper = assembly.GetType("CustomResourceMapper");
customMapper.Map(builder);
}); From my understanding, the fluent API is intended to override the existing, convention-based building of the resource graph. So I don't see the need for building a configuration model first, which is then separately applied to the graph. |
Same here I think... Explicit Resource maps:
or implicitly by providing an Assembly:
And the Fluent Mapping (which can reside in a separate assembly) might look like this:
Mappings are applied in the following order: Conventions, Annotations, Fluent. |
Yes, I understand what you've implemented so far, but I am proposing something simpler. Please explain the added value of your (more complex) solution, which involves extra D/I registrations, additional runtime scanning for mapping types, the requirement to have a mapping class per resource, etc. Because I do not see the need for it. |
I appreciate simplicity as well. I think your approach is better :) The Scanning is optional. I agree that having a mapping class per resource as a requirement is too much. |
Thanks. |
Sounds good. |
I don't mind, whatever works best for you. |
this looks like hard to read. Making sure that configuration of JADNC models can be done outside of the db models seems like a good split in responsibility to make. Although the porposed solution isnt exactly fluent. I'd propose more of an entity-core dbcontext builder approach |
@wisepotato can you please elaborate on this? |
This is what I got so far... Inline Configurations looks as follow:
Applying Configurations looks as follow:
The Configuration implementation looks as follow:
No Assembly Scan (so far). Is it getting closer to what we aim for? |
I cannot see your types, so I do not know how things are linked. Two concerns, though:
|
I will push the branch soon, before the PR so we can review the implementation.
|
Please read carefully and experiment a bit before throwing out so many questions, it takes very long to get anywhere this way. |
@bart-degreed I think we got something ready for review. |
Sure |
Did work on a PR for this get anywhere? |
No, the efforts were abandoned. |
Would love to see this. FWIW |
Our current thinking is that we'd like to keep Update: the following has already been implemented. |
Hi guys! I've read all topic link and I just want to remind that this feature would really be useful. Especially on large projects where are not CRUD based but using along with Domain Driven Design. From what I understand there are 2 options:
Since the first one is not recommended, we will currently asses the viability of the second option. However having an fluent api like @yaniv-yechezkel suggest, would be very helpful since it would allow to have a proper separation of concerns and would allow to move some responsibilities in the Controller itself. |
@dtila Not sure what you're trying to do, but you may want to read the first two entries at https://www.jsonapi.net/usage/common-pitfalls.html first. In JsonApiDotNetCore, putting logic in controllers is a terrible idea, we have resource definition classes for that. If you don't like the universal, attribute-based architecture, JsonApiDotNetCore probably doesn't fit your needs. |
Over time, several users have been asking for an alternative to adding attributes like [Attr] [HasOne] etc. EF Core provides both inline attributes, as well as a fluent API. This issue tracks the design of a fluent API for JsonApiDotNetCore.
First, let's zoom out a bit and see what happens currently in services.AddJsonApi():
In my opinion, adding a fluent API should be part of the last step, which currently looks like:
And I think we should replace that with support for this, similar to EF Core:
How would that work?
builder.Resource<>()
is called and the resource is not part of the graph, first it runs existing attribute scanning logicFor reference, the public API of builders could look something like this:
The text was updated successfully, but these errors were encountered: