Skip to content

Streamline API surface #62

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

Merged
merged 2 commits into from
Mar 13, 2015
Merged

Conversation

csantero
Copy link
Collaborator

This is the first part of a refactor that I hope will make our library even simpler to setup.

Rationale

There are starting to become a lot of moving parts that the user has to configure to get JSONAPI.NET integrated with their project:

  • IPluralizationService
  • IModelManager
  • JsonApiFormatter
  • EnableFilteringAttribute
  • EnableSortingAttribute
  • EnumerateQueryableAsyncAttribute
  • PascalizedControllerSelector
  • ApiController-derived classes
  • IMaterializer

A lot of those components only fit together in one way, and trying to change their behavior will just break the system. For example, if you want filtering and sorting, you better remember to pass the same IModelManager instance to each of the attributes, as well as to the JsonApiFormatter. And the action filters have to be registered in the right order, since filtering should be applied before sorting (and when we introduce paging, it absolutely must take place after sorting). And then there is the async enumerator, which has to run after all the other action filters.

I felt like I could internalize a lot of stuff and only expose a simple API with sensible defaults that does all of that for you.

JsonApiConfiguration

There is now a fluent interface for configuring JSONAPI.NET called JsonApiConfiguration. This object is responsible for setting up an HttpConfiguration object the way we need it. The user can currently use this fluent interface to disable sorting and/or filtering, and to provide a pluralization service. It also lets them opt-in to the async queryable enumerator using a fluent extension method.

JsonApiQueryableAttribute

I have combined the functionality of the filtering and sorting action filters into a new aggregate attribute called JsonApiQueryableAttribute. I abstracted the old filters into new constructs called IQueryableTransforms, and I pass those transforms to the JsonApiQueryableAttribute inside JsonApiConfiguration.Apply(). When we implement pagination we can make that an IQueryableTransform as well.

Autofac

I've taken on Autofac as a library dependency, and JsonApiConfiguration uses it internally to manage the construction of our various components (JsonApiFormatter, ModelManager, JsonApiQueryableAttribute, etc.). At the moment the container I use for this purpose is not integrated with the HttpConfiguration's dependency resolver and will not interfere with controller instance DI.

Still to do

  • The JsonApiFormatter can be cleaned up to be internal and use a single constructor.
  • If we want to inject our own services onto controller instances (such as IMetadataManager), then I believe we will need to take over setting the IDependencyResolver. If we do this, however, we will be forcing the user to use Autofac for resolving controller dependencies, which sounds pretty heavy-handed. At the very least we'd have to give them access to the ContainerBuilder for customization.
  • ModelManager and IModelManager can be made internal.
  • PascalizedControllerSelector can be made internal.

Some links

Example JsonApiConfiguration
JsonApiConfiguration definition

@csantero
Copy link
Collaborator Author

@SphtKr have you gotten a chance to look at this?

@csantero csantero changed the title [WIP] streamline API surface Streamline API surface Feb 20, 2015
@csantero
Copy link
Collaborator Author

Rebased

@csantero
Copy link
Collaborator Author

I've made the following changes to this PR:

  1. All classes and interfaces that were made internal previously are now public again. This makes the new fluent configuration system completely optional.
  2. JSONAPI and JSONAPI.EntityFramework no longer have an Autofac dependency. JsonApiConfiguration instantiates everything manually now.
  3. Queryable enumeration is a first-class operation that the JsonApiQueryableAttribute performs, rather than a separate action filter. The EF-specific asynchronous transform can be specified by using the UseEntityFramework(this JsonApiConfiguration config) extension method.

For any users that don't want to use the fluent configuration, you can see an example of how to wire everything up inside the implementation of the JsonApiConfiguration class, in the Apply() method.

csantero added a commit that referenced this pull request Mar 13, 2015
@csantero csantero merged commit 284c21d into JSONAPIdotNET:0-4-0 Mar 13, 2015
@csantero csantero deleted the api-refactor branch March 13, 2015 17:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant