Skip to content

Simplify DefaultResourceService constructor #592

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 3 commits into from
Oct 23, 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 @@ -7,26 +7,20 @@
using JsonApiDotNetCore.Services;
using JsonApiDotNetCoreExample.Models;
using Microsoft.Extensions.Logging;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace JsonApiDotNetCoreExample.Services
{
public class CustomArticleService : DefaultResourceService<Article>
{
public CustomArticleService(ISortService sortService,
IFilterService filterService,
IResourceRepository<Article, int> repository,
public CustomArticleService(IEnumerable<IQueryParameterService> queryParameters,
IJsonApiOptions options,
IIncludeService includeService,
ISparseFieldsService sparseFieldsService,
IPageService pageService,
IResourceRepository<Article, int> repository,
IResourceContextProvider provider,
IResourceHookExecutor hookExecutor = null,
ILoggerFactory loggerFactory = null)
: base(sortService, filterService, repository, options, includeService, sparseFieldsService,
pageService, provider, hookExecutor, loggerFactory)
{
}
: base(queryParameters, options, repository, provider, hookExecutor, loggerFactory) { }

public override async Task<Article> GetAsync(int id)
{
Expand Down
18 changes: 18 additions & 0 deletions src/JsonApiDotNetCore/Extensions/IEnumerableExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System.Collections.Generic;
using System.Linq;
using JsonApiDotNetCore.Query;

namespace JsonApiDotNetCore.Extensions
{
public static class IEnumerableExtensions
{
/// <summary>
/// gets the first element of type <typeparamref name="TImplementedService"/> if it exists and casts the result to that.
/// Returns null otherwise.
/// </summary>
public static TImplementedService FirstOrDefault<TImplementedService>(this IEnumerable<IQueryParameterService> data) where TImplementedService : class, IQueryParameterService
{
return data.FirstOrDefault(qp => qp is TImplementedService) as TImplementedService;
}
}
}
32 changes: 15 additions & 17 deletions src/JsonApiDotNetCore/Services/DefaultResourceService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using System.Threading.Tasks;
using JsonApiDotNetCore.Internal.Contracts;
using JsonApiDotNetCore.Query;
using JsonApiDotNetCore.Extensions;

namespace JsonApiDotNetCore.Services
{
Expand All @@ -34,29 +35,26 @@ public class DefaultResourceService<TResource, TId> :
private readonly ResourceContext _currentRequestResource;

public DefaultResourceService(
ISortService sortService,
IFilterService filterService,
IEnumerable<IQueryParameterService> queryParameters,
IJsonApiOptions options,
IIncludeService includeService,
ISparseFieldsService sparseFieldsService,
IPageService pageManager,
IResourceRepository<TResource, TId> repository,
IResourceContextProvider provider,
IResourceHookExecutor hookExecutor = null,
ILoggerFactory loggerFactory = null)
{
_includeService = includeService;
_sparseFieldsService = sparseFieldsService;
_pageManager = pageManager;
_includeService = queryParameters.FirstOrDefault<IIncludeService>();
_sparseFieldsService = queryParameters.FirstOrDefault<ISparseFieldsService>();
_pageManager = queryParameters.FirstOrDefault<IPageService>();
_sortService = queryParameters.FirstOrDefault<ISortService>();
_filterService = queryParameters.FirstOrDefault<IFilterService>();
_options = options;
_sortService = sortService;
_filterService = filterService;
_repository = repository;
_hookExecutor = hookExecutor;
_logger = loggerFactory?.CreateLogger<DefaultResourceService<TResource, TId>>();
_currentRequestResource = provider.GetResourceContext<TResource>();
}


public virtual async Task<TResource> CreateAsync(TResource entity)
{
entity = IsNull(_hookExecutor) ? entity : _hookExecutor.BeforeCreate(AsList(entity), ResourcePipeline.Post).SingleOrDefault();
Expand Down Expand Up @@ -323,12 +321,12 @@ public class DefaultResourceService<TResource> : DefaultResourceService<TResourc
IResourceService<TResource>
where TResource : class, IIdentifiable<int>
{
public DefaultResourceService(ISortService sortService, IFilterService filterService, IResourceRepository<TResource, int> repository,
IJsonApiOptions options, IIncludeService includeService, ISparseFieldsService sparseFieldsService,
IPageService pageManager, IResourceContextProvider provider,
IResourceHookExecutor hookExecutor = null, ILoggerFactory loggerFactory = null)
: base(sortService, filterService, options, includeService, sparseFieldsService, pageManager, repository, provider, hookExecutor, loggerFactory)
{
}
public DefaultResourceService(IEnumerable<IQueryParameterService> queryParameters,
IJsonApiOptions options,
IResourceRepository<TResource, int> repository,
IResourceContextProvider provider,
IResourceHookExecutor hookExecutor = null,
ILoggerFactory loggerFactory = null)
: base(queryParameters, options, repository, provider, hookExecutor, loggerFactory) { }
}
}
2 changes: 1 addition & 1 deletion test/UnitTests/Services/EntityResourceService_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public async Task GetRelationshipAsync_Returns_Relationship_Value()

private DefaultResourceService<TodoItem> GetService()
{
return new DefaultResourceService<TodoItem>(null, null, _repositoryMock.Object, new JsonApiOptions(), null, null, _pgsMock.Object, _resourceGraph);
return new DefaultResourceService<TodoItem>(new List<IQueryParameterService>(), new JsonApiOptions(), _repositoryMock.Object, _resourceGraph);
}
}
}