joukevandermaas / saule

JSON API library for ASP.Net Web API 2.
https://joukevandermaas.github.io/saule
MIT License
76 stars 37 forks source link

Added IApiResourceProvider and IApiResourceProviderFactory so ApiReso… #243

Closed sergey-litvinov-work closed 4 years ago

sergey-litvinov-work commented 4 years ago

Hi Jouke,

here is the change that i mentioned in #241 . basically i made

    public interface IApiResourceProviderFactory
    {
        IApiResourceProvider Create(HttpRequestMessage request);
    }

    public interface IApiResourceProvider
    {
        ApiResource Resolve(object dataObject);
    }

The first one called in Saule/Http/PreprocessingDelegatingHandler.cs and it creates a specific provider based on the current http request. And then it uses created IApiResourceProvider and passes it to Saule/Serialization/ResourceGraph.cs and Saule/Serialization/ResourceSerializer.cs. And they are calling it with current data object and it returns ApiResource.

In the default implementation it uses the same request.Properties.TryGetValue(Constants.PropertyNames.ResourceDescriptor, out object resource) and then it always returns it. But potentially if consumer wants, it can have custom implementation that won't depend on ReturnsResourceAttribute and can make ApiResource based on own rules.

In our case we are creating specific resource based on the specific type.

I added 3 new unit tests Tests/Integration/JsonApiFormatterInheritenceTests.cs that validates it. And also as i changed ApiResource parameter to IApiResourceProvider in ResourceSerializer - i updated a bunch of unit tests to wrap existing ApiResource provider

I'll check it tomorrow to be sure that i didn't miss something but it should be ready for the review

joukevandermaas commented 4 years ago

@sergey-litvinov-work do you think this is now ready to be merged? I don't see anything wrong with it, but not sure if you're working on it still.

sergey-litvinov-work commented 4 years ago

@joukevandermaas yep, i think it's ready to be merged. i tested it for our case and it works great. if something would be missing i'll make another PR but for now it looks great Thanks!