Skip to content

Separation of concerns ResourceGraph #586

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 17 commits into from
Oct 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@ public class ArticlesController : JsonApiController<Article>
{
public ArticlesController(
IJsonApiOptions jsonApiOptions,
IResourceGraph resourceGraph,
IResourceService<Article> resourceService)
: base(jsonApiOptions, resourceGraph, resourceService)
: base(jsonApiOptions, resourceService)
{ }
}
}
3 changes: 1 addition & 2 deletions src/Examples/GettingStarted/Controllers/PeopleController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@ public class PeopleController : JsonApiController<Person>
{
public PeopleController(
IJsonApiOptions jsonApiOptions,
IResourceGraph resourceGraph,
IResourceService<Person> resourceService)
: base(jsonApiOptions, resourceGraph, resourceService)
: base(jsonApiOptions, resourceService)
{ }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace GettingStarted.ResourceDefinitionExample
{
public class ModelDefinition : ResourceDefinition<Model>
{
public ModelDefinition(IResourceGraph graph) : base(graph)
public ModelDefinition(IContextEntityProvider provider) : base(provider)
{
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ public class ModelsController : JsonApiController<Model>
{
public ModelsController(
IJsonApiOptions jsonApiOptions,
IResourceGraph resourceGraph,
IResourceService<Model> resourceService)
: base(jsonApiOptions, resourceGraph, resourceService)
: base(jsonApiOptions, resourceService)
{ }
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using JsonApiDotNetCore.Configuration;
using JsonApiDotNetCore.Controllers;
using JsonApiDotNetCore.Internal.Contracts;
using JsonApiDotNetCore.Services;
using JsonApiDotNetCoreExample.Models;

Expand All @@ -10,9 +9,8 @@ public class ArticlesController : JsonApiController<Article>
{
public ArticlesController(
IJsonApiOptions jsonApiOptions,
IResourceGraph resourceGraph,
IResourceService<Article> resourceService)
: base(jsonApiOptions, resourceGraph, resourceService)
: base(jsonApiOptions, resourceService)
{ }
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using JsonApiDotNetCore.Configuration;
using JsonApiDotNetCore.Controllers;
using JsonApiDotNetCore.Internal.Contracts;
using JsonApiDotNetCore.Services;
using JsonApiDotNetCoreExample.Models;
using Microsoft.Extensions.Logging;
Expand All @@ -11,10 +10,9 @@ public class CamelCasedModelsController : JsonApiController<CamelCasedModel>
{
public CamelCasedModelsController(
IJsonApiOptions jsonApiOptions,
IResourceGraph resourceGraph,
IResourceService<CamelCasedModel> resourceService,
ILoggerFactory loggerFactory)
: base(jsonApiOptions, resourceGraph, resourceService, loggerFactory)
: base(jsonApiOptions, resourceService, loggerFactory)
{ }
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using JsonApiDotNetCore.Configuration;
using JsonApiDotNetCore.Controllers;
using JsonApiDotNetCore.Internal.Contracts;
using JsonApiDotNetCore.Services;
using JsonApiDotNetCoreExample.Models;
using Microsoft.Extensions.Logging;
Expand All @@ -9,7 +8,10 @@ namespace JsonApiDotNetCoreExample.Controllers
{
public class PassportsController : JsonApiController<Passport>
{
public PassportsController(IJsonApiOptions jsonApiOptions, IResourceGraph resourceGraph, IResourceService<Passport, int> resourceService, ILoggerFactory loggerFactory = null) : base(jsonApiOptions, resourceGraph, resourceService, loggerFactory)
public PassportsController(IJsonApiOptions jsonApiOptions,
IResourceService<Passport, int> resourceService,
ILoggerFactory loggerFactory = null)
: base(jsonApiOptions, resourceService, loggerFactory)
{
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using JsonApiDotNetCore.Configuration;
using JsonApiDotNetCore.Controllers;
using JsonApiDotNetCore.Internal.Contracts;
using JsonApiDotNetCore.Services;
using JsonApiDotNetCoreExample.Models;
using Microsoft.Extensions.Logging;
Expand All @@ -11,10 +10,9 @@ public class PeopleController : JsonApiController<Person>
{
public PeopleController(
IJsonApiOptions jsonApiOptions,
IResourceGraph resourceGraph,
IResourceService<Person> resourceService,
ILoggerFactory loggerFactory)
: base(jsonApiOptions, resourceGraph, resourceService, loggerFactory)
: base(jsonApiOptions, resourceService, loggerFactory)
{ }
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using JsonApiDotNetCore.Configuration;
using JsonApiDotNetCore.Controllers;
using JsonApiDotNetCore.Internal.Contracts;
using JsonApiDotNetCore.Services;
using JsonApiDotNetCoreExample.Models;
using Microsoft.Extensions.Logging;
Expand All @@ -11,10 +10,9 @@ public class PersonRolesController : JsonApiController<PersonRole>
{
public PersonRolesController(
IJsonApiOptions jsonApiOptions,
IResourceGraph resourceGraph,
IResourceService<PersonRole> resourceService,
ILoggerFactory loggerFactory)
: base(jsonApiOptions, resourceGraph, resourceService, loggerFactory)
: base(jsonApiOptions, resourceService, loggerFactory)
{ }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,10 @@ public class TodoCollectionsController : JsonApiController<TodoItemCollection, G

public TodoCollectionsController(
IJsonApiOptions jsonApiOptions,
IResourceGraph resourceGraph,
IDbContextResolver contextResolver,
IResourceService<TodoItemCollection, Guid> resourceService,
ILoggerFactory loggerFactory)
: base(jsonApiOptions, resourceGraph, resourceService, loggerFactory)
: base(jsonApiOptions, resourceService, loggerFactory)
{
_dbResolver = contextResolver;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using JsonApiDotNetCore.Configuration;
using JsonApiDotNetCore.Controllers;
using JsonApiDotNetCore.Internal.Contracts;
using JsonApiDotNetCore.Services;
using JsonApiDotNetCoreExample.Models;
using Microsoft.Extensions.Logging;
Expand All @@ -10,11 +9,10 @@ namespace JsonApiDotNetCoreExample.Controllers
public class TodoItemsController : JsonApiController<TodoItem>
{
public TodoItemsController(
IJsonApiOptions jsonApiOPtions,
IResourceGraph resourceGraph,
IJsonApiOptions jsonApiOptions,
IResourceService<TodoItem> resourceService,
ILoggerFactory loggerFactory)
: base(jsonApiOPtions, resourceGraph, resourceService, loggerFactory)
: base(jsonApiOptions, resourceService, loggerFactory)
{ }
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using JsonApiDotNetCore.Configuration;
using JsonApiDotNetCore.Controllers;
using JsonApiDotNetCore.Models;
using JsonApiDotNetCore.Services;
Expand All @@ -13,26 +14,30 @@ namespace JsonApiDotNetCoreExample.Controllers
public class TodoItemsCustomController : CustomJsonApiController<TodoItem>
{
public TodoItemsCustomController(
IJsonApiOptions options,
IResourceService<TodoItem> resourceService,
ILoggerFactory loggerFactory)
: base(resourceService, loggerFactory)
: base(options, resourceService, loggerFactory)
{ }
}

public class CustomJsonApiController<T>
: CustomJsonApiController<T, int> where T : class, IIdentifiable<int>
{
public CustomJsonApiController(
IJsonApiOptions options,
IResourceService<T, int> resourceService,
ILoggerFactory loggerFactory)
: base(resourceService, loggerFactory)
{ }
: base(options, resourceService, loggerFactory)
{
}
}

public class CustomJsonApiController<T, TId>
: ControllerBase where T : class, IIdentifiable<TId>
{
private readonly ILogger _logger;
private readonly IJsonApiOptions _options;
private readonly IResourceService<T, TId> _resourceService;

protected IActionResult Forbidden()
Expand All @@ -41,11 +46,13 @@ protected IActionResult Forbidden()
}

public CustomJsonApiController(
IJsonApiOptions options,
IResourceService<T, TId> resourceService,
ILoggerFactory loggerFactory)
{
_options = options;
_resourceService = resourceService;
_logger = loggerFactory.CreateLogger<JsonApiDotNetCore.Controllers.JsonApiController<T, TId>>();
_logger = loggerFactory.CreateLogger<JsonApiController<T, TId>>();
}

public CustomJsonApiController(
Expand Down Expand Up @@ -95,8 +102,8 @@ public virtual async Task<IActionResult> PostAsync([FromBody] T entity)
if (entity == null)
return UnprocessableEntity();

//if (!_jsonApiContext.Options.AllowClientGeneratedIds && !string.IsNullOrEmpty(entity.StringId))
// return Forbidden();
if (_options.AllowClientGeneratedIds && !string.IsNullOrEmpty(entity.StringId))
return Forbidden();

entity = await _resourceService.CreateAsync(entity);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using JsonApiDotNetCore.Configuration;
using JsonApiDotNetCore.Controllers;
using JsonApiDotNetCore.Internal.Contracts;
using JsonApiDotNetCore.Models;
using JsonApiDotNetCore.Services;
using JsonApiDotNetCoreExample.Models;
Expand All @@ -14,10 +13,9 @@ public abstract class AbstractTodoItemsController<T>
{
protected AbstractTodoItemsController(
IJsonApiOptions jsonApiOptions,
IResourceGraph resourceGraph,
IResourceService<T, int> service,
ILoggerFactory loggerFactory)
: base(jsonApiOptions, resourceGraph, service, loggerFactory)
: base(jsonApiOptions, service, loggerFactory)
{ }
}

Expand All @@ -26,10 +24,9 @@ public class TodoItemsTestController : AbstractTodoItemsController<TodoItem>
{
public TodoItemsTestController(
IJsonApiOptions jsonApiOptions,
IResourceGraph resourceGraph,
IResourceService<TodoItem> service,
ILoggerFactory loggerFactory)
: base(jsonApiOptions, resourceGraph, service, loggerFactory)
: base(jsonApiOptions, service, loggerFactory)
{ }
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using JsonApiDotNetCore.Configuration;
using JsonApiDotNetCore.Controllers;
using JsonApiDotNetCore.Internal.Contracts;
using JsonApiDotNetCore.Services;
using JsonApiDotNetCoreExample.Models;
using Microsoft.Extensions.Logging;
Expand All @@ -11,10 +10,9 @@ public class UsersController : JsonApiController<User>
{
public UsersController(
IJsonApiOptions jsonApiOptions,
IResourceGraph resourceGraph,
IResourceService<User> resourceService,
ILoggerFactory loggerFactory)
: base(jsonApiOptions, resourceGraph, resourceService, loggerFactory)
: base(jsonApiOptions, resourceService, loggerFactory)
{ }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace JsonApiDotNetCoreExample.Resources
{
public class ArticleResource : ResourceDefinition<Article>
{
public ArticleResource(IResourceGraph graph) : base(graph) { }
public ArticleResource(IResourceGraph resourceGraph) : base(resourceGraph) { }

public override IEnumerable<Article> OnReturn(HashSet<Article> entities, ResourcePipeline pipeline)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace JsonApiDotNetCoreExample.Resources
{
public abstract class LockableResource<T> : ResourceDefinition<T> where T : class, IIsLockable, IIdentifiable
{
protected LockableResource(IResourceGraph graph) : base(graph) { }
protected LockableResource(IResourceGraph resourceGraph) : base(resourceGraph) { }

protected void DisallowLocked(IEnumerable<T> entities)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace JsonApiDotNetCoreExample.Resources
{
public class PassportResource : ResourceDefinition<Passport>
{
public PassportResource(IResourceGraph graph) : base(graph)
public PassportResource(IResourceGraph resourceGraph) : base(resourceGraph)
{
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace JsonApiDotNetCoreExample.Resources
{
public class PersonResource : LockableResource<Person>, IHasMeta
{
public PersonResource(IResourceGraph graph) : base(graph) { }
public PersonResource(IResourceGraph resourceGraph) : base(resourceGraph) { }

public override IEnumerable<Person> BeforeUpdate(IDiffableEntityHashSet<Person> entities, ResourcePipeline pipeline)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace JsonApiDotNetCoreExample.Resources
{
public class TagResource : ResourceDefinition<Tag>
{
public TagResource(IResourceGraph graph) : base(graph) { }
public TagResource(IResourceGraph resourceGraph) : base(resourceGraph) { }

public override IEnumerable<Tag> BeforeCreate(IEntityHashSet<Tag> affected, ResourcePipeline pipeline)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace JsonApiDotNetCoreExample.Resources
{
public class TodoResource : LockableResource<TodoItem>
{
public TodoResource(IResourceGraph graph) : base(graph) { }
public TodoResource(IResourceGraph resourceGraph) : base(resourceGraph) { }

public override void BeforeRead(ResourcePipeline pipeline, bool isIncluded = false, string stringId = null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace JsonApiDotNetCoreExample.Resources
{
public class UserResource : ResourceDefinition<User>
{
public UserResource(IResourceGraph graph, IFieldsExplorer fieldExplorer) : base(fieldExplorer, graph)
public UserResource(IResourceGraph resourceGraph) : base(resourceGraph)
{
HideFields(u => u.Password);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ public CustomArticleService(ISortService sortService,
IIncludeService includeService,
ISparseFieldsService sparseFieldsService,
IPageService pageService,
IResourceGraph resourceGraph,
IContextEntityProvider provider,
IResourceHookExecutor hookExecutor = null,
ILoggerFactory loggerFactory = null)
: base(sortService, filterService, repository, options, includeService, sparseFieldsService,
pageService, resourceGraph, hookExecutor, loggerFactory)
pageService, provider, hookExecutor, loggerFactory)
{
}

Expand Down
18 changes: 10 additions & 8 deletions src/Examples/JsonApiDotNetCoreExample/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,16 @@ public virtual IServiceProvider ConfigureServices(IServiceCollection services)
services
.AddSingleton<ILoggerFactory>(loggerFactory)
.AddDbContext<AppDbContext>(options => options.UseNpgsql(GetDbConnectionString()), ServiceLifetime.Transient)
.AddJsonApi(options => {
options.Namespace = "api/v1";
options.DefaultPageSize = 5;
options.IncludeTotalRecordCount = true;
options.EnableResourceHooks = true;
options.LoaDatabaseValues = true;
},
discovery => discovery.AddCurrentAssembly());
.AddJsonApi(
options =>
{
options.Namespace = "api/v1";
options.DefaultPageSize = 5;
options.IncludeTotalRecordCount = true;
options.EnableResourceHooks = true;
options.LoaDatabaseValues = true;
},
discovery => discovery.AddCurrentAssembly());

return services.BuildServiceProvider();
}
Expand Down
Loading