Skip to content

Commit a2331cb

Browse files
maureiwisepotato
authored andcommitted
Simplify DefaultResourceService constructor (#592)
* refactor: simplify resource service constructor with IEnumerable<IQueryParameterService> * feat: FirstOrDefault<TQueryParameterService>() extension method * style: remove whitespace
1 parent 34cffeb commit a2331cb

File tree

4 files changed

+38
-28
lines changed

4 files changed

+38
-28
lines changed

src/Examples/JsonApiDotNetCoreExample/Services/CustomArticleService.cs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,20 @@
77
using JsonApiDotNetCore.Services;
88
using JsonApiDotNetCoreExample.Models;
99
using Microsoft.Extensions.Logging;
10+
using System.Collections.Generic;
1011
using System.Threading.Tasks;
1112

1213
namespace JsonApiDotNetCoreExample.Services
1314
{
1415
public class CustomArticleService : DefaultResourceService<Article>
1516
{
16-
public CustomArticleService(ISortService sortService,
17-
IFilterService filterService,
18-
IResourceRepository<Article, int> repository,
17+
public CustomArticleService(IEnumerable<IQueryParameterService> queryParameters,
1918
IJsonApiOptions options,
20-
IIncludeService includeService,
21-
ISparseFieldsService sparseFieldsService,
22-
IPageService pageService,
19+
IResourceRepository<Article, int> repository,
2320
IResourceContextProvider provider,
2421
IResourceHookExecutor hookExecutor = null,
2522
ILoggerFactory loggerFactory = null)
26-
: base(sortService, filterService, repository, options, includeService, sparseFieldsService,
27-
pageService, provider, hookExecutor, loggerFactory)
28-
{
29-
}
23+
: base(queryParameters, options, repository, provider, hookExecutor, loggerFactory) { }
3024

3125
public override async Task<Article> GetAsync(int id)
3226
{
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
using JsonApiDotNetCore.Query;
4+
5+
namespace JsonApiDotNetCore.Extensions
6+
{
7+
public static class IEnumerableExtensions
8+
{
9+
/// <summary>
10+
/// gets the first element of type <typeparamref name="TImplementedService"/> if it exists and casts the result to that.
11+
/// Returns null otherwise.
12+
/// </summary>
13+
public static TImplementedService FirstOrDefault<TImplementedService>(this IEnumerable<IQueryParameterService> data) where TImplementedService : class, IQueryParameterService
14+
{
15+
return data.FirstOrDefault(qp => qp is TImplementedService) as TImplementedService;
16+
}
17+
}
18+
}

src/JsonApiDotNetCore/Services/DefaultResourceService.cs

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using System.Threading.Tasks;
1111
using JsonApiDotNetCore.Internal.Contracts;
1212
using JsonApiDotNetCore.Query;
13+
using JsonApiDotNetCore.Extensions;
1314

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

3637
public DefaultResourceService(
37-
ISortService sortService,
38-
IFilterService filterService,
38+
IEnumerable<IQueryParameterService> queryParameters,
3939
IJsonApiOptions options,
40-
IIncludeService includeService,
41-
ISparseFieldsService sparseFieldsService,
42-
IPageService pageManager,
4340
IResourceRepository<TResource, TId> repository,
4441
IResourceContextProvider provider,
4542
IResourceHookExecutor hookExecutor = null,
4643
ILoggerFactory loggerFactory = null)
4744
{
48-
_includeService = includeService;
49-
_sparseFieldsService = sparseFieldsService;
50-
_pageManager = pageManager;
45+
_includeService = queryParameters.FirstOrDefault<IIncludeService>();
46+
_sparseFieldsService = queryParameters.FirstOrDefault<ISparseFieldsService>();
47+
_pageManager = queryParameters.FirstOrDefault<IPageService>();
48+
_sortService = queryParameters.FirstOrDefault<ISortService>();
49+
_filterService = queryParameters.FirstOrDefault<IFilterService>();
5150
_options = options;
52-
_sortService = sortService;
53-
_filterService = filterService;
5451
_repository = repository;
5552
_hookExecutor = hookExecutor;
5653
_logger = loggerFactory?.CreateLogger<DefaultResourceService<TResource, TId>>();
5754
_currentRequestResource = provider.GetResourceContext<TResource>();
5855
}
5956

57+
6058
public virtual async Task<TResource> CreateAsync(TResource entity)
6159
{
6260
entity = IsNull(_hookExecutor) ? entity : _hookExecutor.BeforeCreate(AsList(entity), ResourcePipeline.Post).SingleOrDefault();
@@ -323,12 +321,12 @@ public class DefaultResourceService<TResource> : DefaultResourceService<TResourc
323321
IResourceService<TResource>
324322
where TResource : class, IIdentifiable<int>
325323
{
326-
public DefaultResourceService(ISortService sortService, IFilterService filterService, IResourceRepository<TResource, int> repository,
327-
IJsonApiOptions options, IIncludeService includeService, ISparseFieldsService sparseFieldsService,
328-
IPageService pageManager, IResourceContextProvider provider,
329-
IResourceHookExecutor hookExecutor = null, ILoggerFactory loggerFactory = null)
330-
: base(sortService, filterService, options, includeService, sparseFieldsService, pageManager, repository, provider, hookExecutor, loggerFactory)
331-
{
332-
}
324+
public DefaultResourceService(IEnumerable<IQueryParameterService> queryParameters,
325+
IJsonApiOptions options,
326+
IResourceRepository<TResource, int> repository,
327+
IResourceContextProvider provider,
328+
IResourceHookExecutor hookExecutor = null,
329+
ILoggerFactory loggerFactory = null)
330+
: base(queryParameters, options, repository, provider, hookExecutor, loggerFactory) { }
333331
}
334332
}

test/UnitTests/Services/EntityResourceService_Tests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public async Task GetRelationshipAsync_Returns_Relationship_Value()
9797

9898
private DefaultResourceService<TodoItem> GetService()
9999
{
100-
return new DefaultResourceService<TodoItem>(null, null, _repositoryMock.Object, new JsonApiOptions(), null, null, _pgsMock.Object, _resourceGraph);
100+
return new DefaultResourceService<TodoItem>(new List<IQueryParameterService>(), new JsonApiOptions(), _repositoryMock.Object, _resourceGraph);
101101
}
102102
}
103103
}

0 commit comments

Comments
 (0)