Skip to content

Commit 2e94d8e

Browse files
committed
feat: remove GetPublicAttributeName from ResourceGraph and remove dependency on ResourceGraph of controller layer
1 parent 0ced9da commit 2e94d8e

File tree

19 files changed

+412
-130
lines changed

19 files changed

+412
-130
lines changed
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using JsonApiDotNetCore.Configuration;
22
using JsonApiDotNetCore.Controllers;
3-
using JsonApiDotNetCore.Internal.Contracts;
43
using JsonApiDotNetCore.Services;
54
using JsonApiDotNetCoreExample.Models;
65

@@ -10,9 +9,8 @@ public class ArticlesController : JsonApiController<Article>
109
{
1110
public ArticlesController(
1211
IJsonApiOptions jsonApiOptions,
13-
IResourceGraph resourceGraph,
1412
IResourceService<Article> resourceService)
15-
: base(jsonApiOptions, resourceGraph, resourceService)
13+
: base(jsonApiOptions, resourceService)
1614
{ }
1715
}
1816
}
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using JsonApiDotNetCore.Configuration;
22
using JsonApiDotNetCore.Controllers;
3-
using JsonApiDotNetCore.Internal.Contracts;
43
using JsonApiDotNetCore.Services;
54
using JsonApiDotNetCoreExample.Models;
65
using Microsoft.Extensions.Logging;
@@ -11,10 +10,9 @@ public class CamelCasedModelsController : JsonApiController<CamelCasedModel>
1110
{
1211
public CamelCasedModelsController(
1312
IJsonApiOptions jsonApiOptions,
14-
IResourceGraph resourceGraph,
1513
IResourceService<CamelCasedModel> resourceService,
1614
ILoggerFactory loggerFactory)
17-
: base(jsonApiOptions, resourceGraph, resourceService, loggerFactory)
15+
: base(jsonApiOptions, resourceService, loggerFactory)
1816
{ }
1917
}
2018
}

src/Examples/JsonApiDotNetCoreExample/Controllers/PassportsController.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using JsonApiDotNetCore.Configuration;
22
using JsonApiDotNetCore.Controllers;
3-
using JsonApiDotNetCore.Internal.Contracts;
43
using JsonApiDotNetCore.Services;
54
using JsonApiDotNetCoreExample.Models;
65
using Microsoft.Extensions.Logging;
@@ -9,7 +8,10 @@ namespace JsonApiDotNetCoreExample.Controllers
98
{
109
public class PassportsController : JsonApiController<Passport>
1110
{
12-
public PassportsController(IJsonApiOptions jsonApiOptions, IResourceGraph resourceGraph, IResourceService<Passport, int> resourceService, ILoggerFactory loggerFactory = null) : base(jsonApiOptions, resourceGraph, resourceService, loggerFactory)
11+
public PassportsController(IJsonApiOptions jsonApiOptions,
12+
IResourceService<Passport, int> resourceService,
13+
ILoggerFactory loggerFactory = null)
14+
: base(jsonApiOptions, resourceService, loggerFactory)
1315
{
1416
}
1517
}
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using JsonApiDotNetCore.Configuration;
22
using JsonApiDotNetCore.Controllers;
3-
using JsonApiDotNetCore.Internal.Contracts;
43
using JsonApiDotNetCore.Services;
54
using JsonApiDotNetCoreExample.Models;
65
using Microsoft.Extensions.Logging;
@@ -11,10 +10,9 @@ public class PeopleController : JsonApiController<Person>
1110
{
1211
public PeopleController(
1312
IJsonApiOptions jsonApiOptions,
14-
IResourceGraph resourceGraph,
1513
IResourceService<Person> resourceService,
1614
ILoggerFactory loggerFactory)
17-
: base(jsonApiOptions, resourceGraph, resourceService, loggerFactory)
15+
: base(jsonApiOptions, resourceService, loggerFactory)
1816
{ }
1917
}
2018
}
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using JsonApiDotNetCore.Configuration;
22
using JsonApiDotNetCore.Controllers;
3-
using JsonApiDotNetCore.Internal.Contracts;
43
using JsonApiDotNetCore.Services;
54
using JsonApiDotNetCoreExample.Models;
65
using Microsoft.Extensions.Logging;
@@ -11,10 +10,9 @@ public class PersonRolesController : JsonApiController<PersonRole>
1110
{
1211
public PersonRolesController(
1312
IJsonApiOptions jsonApiOptions,
14-
IResourceGraph resourceGraph,
1513
IResourceService<PersonRole> resourceService,
1614
ILoggerFactory loggerFactory)
17-
: base(jsonApiOptions, resourceGraph, resourceService, loggerFactory)
15+
: base(jsonApiOptions, resourceService, loggerFactory)
1816
{ }
1917
}
2018
}

src/Examples/JsonApiDotNetCoreExample/Controllers/TodoCollectionsController.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,10 @@ public class TodoCollectionsController : JsonApiController<TodoItemCollection, G
1919

2020
public TodoCollectionsController(
2121
IJsonApiOptions jsonApiOptions,
22-
IResourceGraph resourceGraph,
2322
IDbContextResolver contextResolver,
2423
IResourceService<TodoItemCollection, Guid> resourceService,
2524
ILoggerFactory loggerFactory)
26-
: base(jsonApiOptions, resourceGraph, resourceService, loggerFactory)
25+
: base(jsonApiOptions, resourceService, loggerFactory)
2726
{
2827
_dbResolver = contextResolver;
2928
}
Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using JsonApiDotNetCore.Configuration;
22
using JsonApiDotNetCore.Controllers;
3-
using JsonApiDotNetCore.Internal.Contracts;
43
using JsonApiDotNetCore.Services;
54
using JsonApiDotNetCoreExample.Models;
65
using Microsoft.Extensions.Logging;
@@ -10,11 +9,10 @@ namespace JsonApiDotNetCoreExample.Controllers
109
public class TodoItemsController : JsonApiController<TodoItem>
1110
{
1211
public TodoItemsController(
13-
IJsonApiOptions jsonApiOPtions,
14-
IResourceGraph resourceGraph,
12+
IJsonApiOptions jsonApiOptions,
1513
IResourceService<TodoItem> resourceService,
1614
ILoggerFactory loggerFactory)
17-
: base(jsonApiOPtions, resourceGraph, resourceService, loggerFactory)
15+
: base(jsonApiOptions, resourceService, loggerFactory)
1816
{ }
1917
}
2018
}

src/Examples/JsonApiDotNetCoreExample/Controllers/TodoItemsTestController.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using JsonApiDotNetCore.Configuration;
22
using JsonApiDotNetCore.Controllers;
3-
using JsonApiDotNetCore.Internal.Contracts;
43
using JsonApiDotNetCore.Models;
54
using JsonApiDotNetCore.Services;
65
using JsonApiDotNetCoreExample.Models;
@@ -14,10 +13,9 @@ public abstract class AbstractTodoItemsController<T>
1413
{
1514
protected AbstractTodoItemsController(
1615
IJsonApiOptions jsonApiOptions,
17-
IResourceGraph resourceGraph,
1816
IResourceService<T, int> service,
1917
ILoggerFactory loggerFactory)
20-
: base(jsonApiOptions, resourceGraph, service, loggerFactory)
18+
: base(jsonApiOptions, service, loggerFactory)
2119
{ }
2220
}
2321

@@ -26,10 +24,9 @@ public class TodoItemsTestController : AbstractTodoItemsController<TodoItem>
2624
{
2725
public TodoItemsTestController(
2826
IJsonApiOptions jsonApiOptions,
29-
IResourceGraph resourceGraph,
3027
IResourceService<TodoItem> service,
3128
ILoggerFactory loggerFactory)
32-
: base(jsonApiOptions, resourceGraph, service, loggerFactory)
29+
: base(jsonApiOptions, service, loggerFactory)
3330
{ }
3431
}
3532
}
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using JsonApiDotNetCore.Configuration;
22
using JsonApiDotNetCore.Controllers;
3-
using JsonApiDotNetCore.Internal.Contracts;
43
using JsonApiDotNetCore.Services;
54
using JsonApiDotNetCoreExample.Models;
65
using Microsoft.Extensions.Logging;
@@ -11,10 +10,9 @@ public class UsersController : JsonApiController<User>
1110
{
1211
public UsersController(
1312
IJsonApiOptions jsonApiOptions,
14-
IResourceGraph resourceGraph,
1513
IResourceService<User> resourceService,
1614
ILoggerFactory loggerFactory)
17-
: base(jsonApiOptions, resourceGraph, resourceService, loggerFactory)
15+
: base(jsonApiOptions, resourceService, loggerFactory)
1816
{ }
1917
}
2018
}
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using JsonApiDotNetCore.Configuration;
22
using JsonApiDotNetCore.Controllers;
3-
using JsonApiDotNetCore.Internal.Contracts;
43
using JsonApiDotNetCore.Services;
54
using JsonApiDotNetCoreExample.Models;
65
using Microsoft.Extensions.Logging;
@@ -11,10 +10,9 @@ public class CustomTodoItemsController : JsonApiController<TodoItem>
1110
{
1211
public CustomTodoItemsController(
1312
IJsonApiOptions jsonApiOptions,
14-
IResourceGraph resourceGraph,
1513
IResourceService<TodoItem> resourceService,
1614
ILoggerFactory loggerFactory)
17-
: base(jsonApiOptions, resourceGraph, resourceService, loggerFactory)
15+
: base(jsonApiOptions, resourceService, loggerFactory)
1816
{ }
1917
}
2018
}

src/JsonApiDotNetCore/Controllers/BaseJsonApiController.cs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using System;
2+
using System.Reflection;
13
using System.Threading.Tasks;
24
using JsonApiDotNetCore.Configuration;
35
using JsonApiDotNetCore.Extensions;
@@ -24,11 +26,9 @@ public class BaseJsonApiController<T, TId>
2426
private readonly IDeleteService<T, TId> _delete;
2527
private readonly ILogger<BaseJsonApiController<T, TId>> _logger;
2628
private readonly IJsonApiOptions _jsonApiOptions;
27-
private readonly IResourceGraph _resourceGraph;
2829

2930
public BaseJsonApiController(
3031
IJsonApiOptions jsonApiOptions,
31-
IResourceGraph resourceGraphManager,
3232
IResourceService<T, TId> resourceService,
3333
ILoggerFactory loggerFactory)
3434
{
@@ -41,7 +41,6 @@ public BaseJsonApiController(
4141
_logger = new Logger<BaseJsonApiController<T, TId>>(new LoggerFactory());
4242
}
4343
_jsonApiOptions = jsonApiOptions;
44-
_resourceGraph = resourceGraphManager;
4544
_getAll = resourceService;
4645
_getById = resourceService;
4746
_getRelationship = resourceService;
@@ -84,7 +83,6 @@ public BaseJsonApiController(
8483
/// <param name="delete"></param>
8584
public BaseJsonApiController(
8685
IJsonApiOptions jsonApiOptions,
87-
IResourceGraph resourceGraph,
8886
IGetAllService<T, TId> getAll = null,
8987
IGetByIdService<T, TId> getById = null,
9088
IGetRelationshipService<T, TId> getRelationship = null,
@@ -94,7 +92,6 @@ public BaseJsonApiController(
9492
IUpdateRelationshipService<T, TId> updateRelationships = null,
9593
IDeleteService<T, TId> delete = null)
9694
{
97-
_resourceGraph = resourceGraph;
9895
_jsonApiOptions = jsonApiOptions;
9996
_getAll = getAll;
10097
_getById = getById;
@@ -156,9 +153,7 @@ public virtual async Task<IActionResult> PostAsync([FromBody] T entity)
156153
return Forbidden();
157154

158155
if (_jsonApiOptions.ValidateModelState && !ModelState.IsValid)
159-
{
160-
return UnprocessableEntity(ModelStateExtensions.ConvertToErrorCollection<T>(ModelState, _resourceGraph));
161-
}
156+
return UnprocessableEntity(ModelStateExtensions.ConvertToErrorCollection<T>(ModelState, GetAssociatedResource()));
162157

163158
entity = await _create.CreateAsync(entity);
164159

@@ -172,9 +167,7 @@ public virtual async Task<IActionResult> PatchAsync(TId id, [FromBody] T entity)
172167
return UnprocessableEntity();
173168

174169
if (_jsonApiOptions.ValidateModelState && !ModelState.IsValid)
175-
{
176-
return UnprocessableEntity(ModelStateExtensions.ConvertToErrorCollection<T>(ModelState, _resourceGraph));
177-
}
170+
return UnprocessableEntity(ModelStateExtensions.ConvertToErrorCollection<T>(ModelState, GetAssociatedResource()));
178171

179172
var updatedEntity = await _update.UpdateAsync(id, entity);
180173

@@ -199,6 +192,13 @@ public virtual async Task<IActionResult> DeleteAsync(TId id)
199192
return NotFound();
200193
return NoContent();
201194
}
195+
196+
internal Type GetAssociatedResource()
197+
{
198+
return GetType().GetMethod(nameof(GetAssociatedResource), BindingFlags.Instance | BindingFlags.NonPublic)
199+
.DeclaringType
200+
.GetGenericArguments()[0];
201+
}
202202
}
203203
public class BaseJsonApiController<T>
204204
: BaseJsonApiController<T, int>
@@ -218,7 +218,6 @@ public BaseJsonApiController(
218218

219219
public BaseJsonApiController(
220220
IJsonApiOptions jsonApiOptions,
221-
IResourceGraph resourceGraph,
222221
IGetAllService<T, int> getAll = null,
223222
IGetByIdService<T, int> getById = null,
224223
IGetRelationshipService<T, int> getRelationship = null,
@@ -227,6 +226,6 @@ public BaseJsonApiController(
227226
IUpdateService<T, int> update = null,
228227
IUpdateRelationshipService<T, int> updateRelationships = null,
229228
IDeleteService<T, int> delete = null
230-
) : base(jsonApiOptions, resourceGraph, getAll, getById, getRelationship, getRelationships, create, update, updateRelationships, delete) { }
229+
) : base(jsonApiOptions, getAll, getById, getRelationship, getRelationships, create, update, updateRelationships, delete) { }
231230
}
232231
}

src/JsonApiDotNetCore/Controllers/JsonApiController.cs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,13 @@ public class JsonApiController<T, TId> : BaseJsonApiController<T, TId> where T :
2020
/// <param name="loggerFactory"></param>
2121
public JsonApiController(
2222
IJsonApiOptions jsonApiOptions,
23-
IResourceGraph resourceGraph,
2423
IResourceService<T, TId> resourceService,
2524
ILoggerFactory loggerFactory = null)
26-
: base(jsonApiOptions, resourceGraph, resourceService, loggerFactory = null)
25+
: base(jsonApiOptions, resourceService, loggerFactory = null)
2726
{ }
2827

2928
public JsonApiController(
3029
IJsonApiOptions jsonApiOptions,
31-
IResourceGraph resourceGraph,
3230
IGetAllService<T, TId> getAll = null,
3331
IGetByIdService<T, TId> getById = null,
3432
IGetRelationshipService<T, TId> getRelationship = null,
@@ -37,7 +35,7 @@ public JsonApiController(
3735
IUpdateService<T, TId> update = null,
3836
IUpdateRelationshipService<T, TId> updateRelationships = null,
3937
IDeleteService<T, TId> delete = null
40-
) : base(jsonApiOptions, resourceGraph, getAll, getById, getRelationship, getRelationships, create, update, updateRelationships, delete) { }
38+
) : base(jsonApiOptions, getAll, getById, getRelationship, getRelationships, create, update, updateRelationships, delete) { }
4139

4240
[HttpGet]
4341
public override async Task<IActionResult> GetAsync() => await base.GetAsync();
@@ -87,16 +85,14 @@ public class JsonApiController<T> : JsonApiController<T, int> where T : class, I
8785
/// <param name="loggerFactory"></param>
8886
public JsonApiController(
8987
IJsonApiOptions jsonApiOptions,
90-
IResourceGraph resourceGraph,
9188
IResourceService<T, int> resourceService,
9289
ILoggerFactory loggerFactory = null
9390
)
94-
: base(jsonApiOptions, resourceGraph, resourceService, loggerFactory)
91+
: base(jsonApiOptions, resourceService, loggerFactory)
9592
{ }
9693

9794
public JsonApiController(
9895
IJsonApiOptions jsonApiOptions,
99-
IResourceGraph resourceGraph,
10096
IGetAllService<T, int> getAll = null,
10197
IGetByIdService<T, int> getById = null,
10298
IGetRelationshipService<T, int> getRelationship = null,
@@ -105,7 +101,7 @@ public JsonApiController(
105101
IUpdateService<T, int> update = null,
106102
IUpdateRelationshipService<T, int> updateRelationships = null,
107103
IDeleteService<T, int> delete = null
108-
) : base(jsonApiOptions, resourceGraph, getAll, getById, getRelationship, getRelationships, create, update, updateRelationships, delete) { }
104+
) : base(jsonApiOptions, getAll, getById, getRelationship, getRelationships, create, update, updateRelationships, delete) { }
109105

110106

111107
}

src/JsonApiDotNetCore/Extensions/ModelStateExtensions.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,24 @@
11
using System;
2+
using System.Reflection;
23
using JsonApiDotNetCore.Internal;
3-
using JsonApiDotNetCore.Internal.Contracts;
4+
using JsonApiDotNetCore.Models;
45
using Microsoft.AspNetCore.Mvc.ModelBinding;
56
using Microsoft.EntityFrameworkCore.Internal;
67

78
namespace JsonApiDotNetCore.Extensions
89
{
910
public static class ModelStateExtensions
1011
{
11-
public static ErrorCollection ConvertToErrorCollection<T>(this ModelStateDictionary modelState, IResourceGraph resourceGraph)
12+
public static ErrorCollection ConvertToErrorCollection<T>(this ModelStateDictionary modelState, Type resourceType)
1213
{
1314
ErrorCollection collection = new ErrorCollection();
1415
foreach (var entry in modelState)
1516
{
1617
if (entry.Value.Errors.Any() == false)
1718
continue;
1819

19-
var attrName = resourceGraph.GetPublicAttributeName<T>(entry.Key);
20+
var targetedProperty = resourceType.GetProperty(entry.Key);
21+
var attrName = targetedProperty.GetCustomAttribute<AttrAttribute>().PublicAttributeName;
2022

2123
foreach (var modelError in entry.Value.Errors)
2224
{

src/JsonApiDotNetCore/Internal/Contracts/IResourceGraph.cs

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,6 @@ public interface IResourceGraph : IContextEntityProvider
1212
{
1313
RelationshipAttribute GetInverseRelationship(RelationshipAttribute relationship);
1414

15-
/// <summary>
16-
/// Get the internal navigation property name for the specified public
17-
/// relationship name.
18-
/// </summary>
19-
/// <param name="relationshipName">The public relationship name specified by a <see cref="HasOneAttribute" /> or <see cref="HasManyAttribute" /></param>
20-
/// <example>
21-
/// <code>
22-
/// _graph.GetRelationshipName&lt;TodoItem&gt;("achieved-date");
23-
/// // returns "AchievedDate"
24-
/// </code>
25-
/// </example>
26-
string GetRelationshipName<TParent>(string relationshipName);
27-
28-
/// <summary>
29-
/// Get the public attribute name for a type based on the internal attribute name.
30-
/// </summary>
31-
/// <param name="internalAttributeName">The internal attribute name for a <see cref="AttrAttribute" />.</param>
32-
string GetPublicAttributeName<TParent>(string internalAttributeName);
33-
3415
/// <summary>
3516
/// Was built against an EntityFrameworkCore DbContext ?
3617
/// </summary>

0 commit comments

Comments
 (0)