Skip to content

Commit 7961841

Browse files
committed
refactor(jsonapi-context): use IHttpContextAccessor
1 parent 5d173c0 commit 7961841

File tree

6 files changed

+24
-29
lines changed

6 files changed

+24
-29
lines changed

src/JsonApiDotNetCore/Controllers/JsonApiController.cs

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
using JsonApiDotNetCore.Models;
66
using JsonApiDotNetCore.Services;
77
using Microsoft.AspNetCore.Mvc;
8-
using Microsoft.AspNetCore.Routing;
98
using Microsoft.Extensions.Logging;
109
using Newtonsoft.Json;
1110

@@ -34,7 +33,7 @@ public JsonApiController(
3433
IEntityRepository<T, TId> entityRepository,
3534
ILoggerFactory loggerFactory)
3635
{
37-
_jsonApiContext = jsonApiContext;
36+
_jsonApiContext = jsonApiContext.ApplyContext<T>();
3837
_entities = entityRepository;
3938

4039
_logger = loggerFactory.CreateLogger<JsonApiController<T, TId>>();
@@ -53,8 +52,6 @@ public JsonApiController(
5352
[HttpGet]
5453
public virtual IActionResult Get()
5554
{
56-
ApplyContext();
57-
5855
var entities = _entities.Get();
5956

6057
entities = ApplyQuery(entities);
@@ -65,8 +62,6 @@ public virtual IActionResult Get()
6562
[HttpGet("{id}")]
6663
public virtual async Task<IActionResult> GetAsync(TId id)
6764
{
68-
ApplyContext();
69-
7065
var entity = await _entities.GetAsync(id);
7166

7267
if (entity == null)
@@ -78,8 +73,6 @@ public virtual async Task<IActionResult> GetAsync(TId id)
7873
[HttpGet("{id}/{relationshipName}")]
7974
public virtual async Task<IActionResult> GetRelationshipAsync(TId id, string relationshipName)
8075
{
81-
ApplyContext();
82-
8376
relationshipName = _jsonApiContext.ContextGraph
8477
.GetRelationshipName<T>(relationshipName);
8578

@@ -103,8 +96,6 @@ public virtual async Task<IActionResult> GetRelationshipAsync(TId id, string rel
10396
[HttpPost]
10497
public virtual async Task<IActionResult> PostAsync([FromBody] T entity)
10598
{
106-
ApplyContext();
107-
10899
if (entity == null)
109100
return BadRequest();
110101

@@ -116,8 +107,6 @@ public virtual async Task<IActionResult> PostAsync([FromBody] T entity)
116107
[HttpPatch("{id}")]
117108
public virtual async Task<IActionResult> PatchAsync(TId id, [FromBody] T entity)
118109
{
119-
ApplyContext();
120-
121110
if (entity == null)
122111
return BadRequest();
123112

@@ -135,8 +124,6 @@ public virtual async Task<IActionResult> PatchAsync(TId id, [FromBody] T entity)
135124
[HttpDelete("{id}")]
136125
public virtual async Task<IActionResult> DeleteAsync(TId id)
137126
{
138-
ApplyContext();
139-
140127
var wasDeleted = await _entities.DeleteAsync(id);
141128

142129
if (!wasDeleted)
@@ -151,19 +138,12 @@ public virtual async Task<IActionResult> DeleteAsync(TId id)
151138
// return Ok("Delete Id/relationship");
152139
// }
153140

154-
private void ApplyContext()
155-
{
156-
var routeData = HttpContext.GetRouteData();
157-
_jsonApiContext.RequestEntity = _jsonApiContext.ContextGraph.GetContextEntity(typeof(T));
158-
_jsonApiContext.ApplyContext(HttpContext);
159-
}
160-
161141
private IQueryable<T> ApplyQuery(IQueryable<T> entities)
162142
{
163143
if(!HttpContext.Request.Query.Any())
164144
return entities;
165145

166-
var querySet = new QuerySet<T>( _jsonApiContext);
146+
var querySet = new QuerySet<T>(_jsonApiContext);
167147

168148
entities = _entities.Filter(entities, querySet.Filter);
169149

src/JsonApiDotNetCore/Extensions/ServiceProviderExtensions.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using JsonApiDotNetCore.Formatters;
33
using JsonApiDotNetCore.Internal;
44
using JsonApiDotNetCore.Services;
5+
using Microsoft.AspNetCore.Http;
56
using Microsoft.AspNetCore.Mvc;
67
using Microsoft.EntityFrameworkCore;
78
using Microsoft.Extensions.DependencyInjection;
@@ -30,7 +31,9 @@ public static void AddJsonApiInternals<TContext>(this IServiceCollection service
3031
services.AddScoped(typeof(IEntityRepository<,>), typeof(DefaultEntityRepository<,>));
3132

3233
services.AddSingleton<IContextGraph>(contextGraph);
33-
services.AddSingleton<IJsonApiContext, JsonApiContext>();
34+
services.AddSingleton<IJsonApiContext,JsonApiContext>();
35+
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
36+
3437
services.AddScoped<JsonApiRouteHandler>();
3538
}
3639

src/JsonApiDotNetCore/Serialization/JsonApiSerializer.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public static string Serialize(object entity, IJsonApiContext jsonApiContext)
1818
private static string _serializeDocuments(object entity, IJsonApiContext jsonApiContext)
1919
{
2020
var documentBuilder = new DocumentBuilder(jsonApiContext);
21+
2122
var entities = entity as IEnumerable<IIdentifiable>;
2223
var documents = documentBuilder.Build(entities);
2324
return JsonConvert.SerializeObject(documents);

src/JsonApiDotNetCore/Services/IJsonApiContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace JsonApiDotNetCore.Services
55
{
66
public interface IJsonApiContext
77
{
8-
void ApplyContext(HttpContext context);
8+
IJsonApiContext ApplyContext<T>();
99
IContextGraph ContextGraph { get; set; }
1010
ContextEntity RequestEntity { get; set; }
1111
string BasePath { get; set; }

src/JsonApiDotNetCore/Services/JsonApiContext.cs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,32 @@ namespace JsonApiDotNetCore.Services
66
{
77
public class JsonApiContext : IJsonApiContext
88
{
9-
public JsonApiContext(IContextGraph contextGraph)
9+
private IHttpContextAccessor _httpContextAccessor;
10+
public JsonApiContext(
11+
IContextGraph contextGraph,
12+
IHttpContextAccessor httpContextAccessor)
1013
{
1114
ContextGraph = contextGraph;
15+
_httpContextAccessor = httpContextAccessor;
1216
}
1317

1418
public IContextGraph ContextGraph { get; set; }
1519
public ContextEntity RequestEntity { get; set; }
1620
public string BasePath { get; set; }
1721
public IQueryCollection Query { get; set; }
1822

19-
public void ApplyContext(HttpContext context)
23+
public IJsonApiContext ApplyContext<T>()
2024
{
25+
var context = _httpContextAccessor.HttpContext;
26+
27+
RequestEntity = ContextGraph.GetContextEntity(typeof(T));
28+
29+
Query = context.Request.Query;
30+
2131
var linkBuilder = new LinkBuilder(this);
2232
BasePath = linkBuilder.GetBasePath(context, RequestEntity.EntityName);
23-
Query = context.Request.Query;
24-
}
33+
34+
return this;
35+
}
2536
}
2637
}

src/JsonApiDotNetCoreExample/Controllers/TodoItemsController.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace JsonApiDotNetCoreExample.Controllers
1111
public class TodoItemsController : JsonApiController<TodoItem>
1212
{
1313
public TodoItemsController(
14-
IJsonApiContext jsonApiContext,
14+
IJsonApiContext jsonApiContext,
1515
IEntityRepository<TodoItem> entityRepository,
1616
ILoggerFactory loggerFactory)
1717
: base(jsonApiContext, entityRepository, loggerFactory)

0 commit comments

Comments
 (0)