Skip to content

Commit a1f5d12

Browse files
committed
feat: more structual ctors EntityResourceService
1 parent 61634f1 commit a1f5d12

File tree

1 file changed

+25
-29
lines changed

1 file changed

+25
-29
lines changed

src/JsonApiDotNetCore/Services/EntityResourceService.cs

Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -47,51 +47,47 @@ public class EntityResourceService<TResource, TEntity, TId> :
4747
private readonly IResourceMapper _mapper;
4848
private readonly IResourceHookExecutor _hookExecutor;
4949

50+
5051
public EntityResourceService(
51-
IJsonApiContext jsonApiContext,
52-
IEntityRepository<TEntity, TId> entityRepository,
53-
IResourceHookExecutor hookExecutor,
54-
ILoggerFactory loggerFactory = null)
52+
IJsonApiContext jsonApiContext,
53+
IEntityRepository<TEntity, TId> entityRepository,
54+
IResourceHookExecutor hookExecutor = null,
55+
IResourceMapper mapper = null,
56+
ILoggerFactory loggerFactory = null)
5557
{
56-
// no mapper provided, TResource & TEntity must be the same type
57-
if (typeof(TResource) != typeof(TEntity))
58+
_jsonApiContext = jsonApiContext;
59+
_entities = entityRepository;
60+
61+
if (mapper == null && typeof(TResource) != typeof(TEntity))
5862
{
5963
throw new InvalidOperationException("Resource and Entity types are NOT the same. Please provide a mapper.");
6064
}
61-
62-
_jsonApiContext = jsonApiContext;
63-
_entities = entityRepository;
6465
_hookExecutor = hookExecutor;
66+
_mapper = mapper;
6567
_logger = loggerFactory?.CreateLogger<EntityResourceService<TResource, TEntity, TId>>();
6668
}
6769

70+
71+
public EntityResourceService(
72+
IJsonApiContext jsonApiContext,
73+
IEntityRepository<TEntity, TId> entityRepository,
74+
IResourceHookExecutor hookExecutor,
75+
ILoggerFactory loggerFactory = null) : this(jsonApiContext, entityRepository, hookExecutor: hookExecutor, mapper: null, loggerFactory: null)
76+
{ }
77+
6878
public EntityResourceService(
6979
IJsonApiContext jsonApiContext,
7080
IEntityRepository<TEntity, TId> entityRepository,
71-
ILoggerFactory loggerFactory = null)
72-
{
73-
// no mapper provided, TResource & TEntity must be the same type
74-
if (typeof(TResource) != typeof(TEntity))
75-
{
76-
throw new InvalidOperationException("Resource and Entity types are NOT the same. Please provide a mapper.");
77-
}
78-
79-
_jsonApiContext = jsonApiContext;
80-
_entities = entityRepository;
81-
_logger = loggerFactory?.CreateLogger<EntityResourceService<TResource, TEntity, TId>>();
82-
}
81+
ILoggerFactory loggerFactory = null) : this(jsonApiContext, entityRepository, hookExecutor: null, mapper: null, loggerFactory: loggerFactory)
82+
{ }
8383

84+
[Obsolete("Use ctor with signature (jsonApiContext, entityRepository, hookExecutor = null, mapper = null, loggerFactory = null")]
8485
public EntityResourceService(
8586
IJsonApiContext jsonApiContext,
8687
IEntityRepository<TEntity, TId> entityRepository,
87-
IResourceMapper mapper,
88-
ILoggerFactory loggerFactory = null)
89-
{
90-
_jsonApiContext = jsonApiContext;
91-
_entities = entityRepository;
92-
_logger = loggerFactory.CreateLogger<EntityResourceService<TResource, TEntity, TId>>();
93-
_mapper = mapper;
94-
}
88+
ILoggerFactory loggerFactory,
89+
IResourceMapper mapper ) : this(jsonApiContext, entityRepository, hookExecutor: null, mapper: mapper, loggerFactory: loggerFactory)
90+
{ }
9591

9692
public virtual async Task<TResource> CreateAsync(TResource resource)
9793
{

0 commit comments

Comments
 (0)