Skip to content

Commit 4087b1f

Browse files
committed
refactor(JsonApiContext): begin interface separation
remove redundant IsRelationship property
1 parent 02a0082 commit 4087b1f

File tree

3 files changed

+105
-20
lines changed

3 files changed

+105
-20
lines changed

src/JsonApiDotNetCore/Builders/DocumentBuilder.cs

+7-5
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ public class DocumentBuilder : IDocumentBuilder
1313
private readonly IJsonApiContext _jsonApiContext;
1414
private readonly IContextGraph _contextGraph;
1515
private readonly IRequestMeta _requestMeta;
16-
private readonly DocumentBuilderOptions _documentBuilderOptions;
16+
private readonly DocumentBuilderOptions _documentBuilderOptions;
1717

18-
public DocumentBuilder(IJsonApiContext jsonApiContext, IRequestMeta requestMeta=null, IDocumentBuilderOptionsProvider documentBuilderOptionsProvider=null)
18+
public DocumentBuilder(IJsonApiContext jsonApiContext, IRequestMeta requestMeta = null, IDocumentBuilderOptionsProvider documentBuilderOptionsProvider = null)
1919
{
2020
_jsonApiContext = jsonApiContext;
2121
_contextGraph = jsonApiContext.ContextGraph;
@@ -107,7 +107,7 @@ public DocumentData GetData(ContextEntity contextEntity, IIdentifiable entity)
107107
Id = entity.StringId
108108
};
109109

110-
if (_jsonApiContext.IsRelationshipData)
110+
if (_jsonApiContext.IsRelationshipPath)
111111
return data;
112112

113113
data.Attributes = new Dictionary<string, object>();
@@ -243,7 +243,8 @@ private List<ResourceIdentifierObject> GetRelationships(IEnumerable<object> enti
243243
var relationships = new List<ResourceIdentifierObject>();
244244
foreach (var entity in entities)
245245
{
246-
relationships.Add(new ResourceIdentifierObject {
246+
relationships.Add(new ResourceIdentifierObject
247+
{
247248
Type = typeName.EntityName,
248249
Id = ((IIdentifiable)entity).StringId
249250
});
@@ -256,7 +257,8 @@ private ResourceIdentifierObject GetRelationship(object entity)
256257

257258
var typeName = _jsonApiContext.ContextGraph.GetContextEntity(objType);
258259

259-
return new ResourceIdentifierObject {
260+
return new ResourceIdentifierObject
261+
{
260262
Type = typeName.EntityName,
261263
Id = ((IIdentifiable)entity).StringId
262264
};

src/JsonApiDotNetCore/Services/EntityResourceService.cs

+1-4
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,7 @@ private async Task<T> GetWithRelationshipsAsync(TId id)
8080
}
8181

8282
public virtual async Task<object> GetRelationshipsAsync(TId id, string relationshipName)
83-
{
84-
_jsonApiContext.IsRelationshipData = true;
85-
return await GetRelationshipAsync(id, relationshipName);
86-
}
83+
=> await GetRelationshipAsync(id, relationshipName);
8784

8885
public virtual async Task<object> GetRelationshipAsync(TId id, string relationshipName)
8986
{

src/JsonApiDotNetCore/Services/IJsonApiContext.cs

+97-11
Original file line numberDiff line numberDiff line change
@@ -10,27 +10,113 @@
1010

1111
namespace JsonApiDotNetCore.Services
1212
{
13-
public interface IJsonApiContext
13+
public interface IJsonApiApplication
1414
{
1515
JsonApiOptions Options { get; set; }
16-
IJsonApiContext ApplyContext<T>(object controller);
1716
IContextGraph ContextGraph { get; set; }
18-
ContextEntity RequestEntity { get; set; }
19-
string BasePath { get; set; }
20-
QuerySet QuerySet { get; set; }
21-
bool IsRelationshipData { get; set; }
17+
}
18+
19+
public interface IQueryRequest
20+
{
2221
List<string> IncludedRelationships { get; set; }
23-
bool IsRelationshipPath { get; }
22+
QuerySet QuerySet { get; set; }
2423
PageManager PageManager { get; set; }
25-
IMetaBuilder MetaBuilder { get; set; }
26-
IGenericProcessorFactory GenericProcessorFactory { get; set; }
24+
}
25+
26+
public interface IUpdateRequest
27+
{
2728
Dictionary<AttrAttribute, object> AttributesToUpdate { get; set; }
2829
Dictionary<RelationshipAttribute, object> RelationshipsToUpdate { get; set; }
30+
}
31+
32+
public interface IJsonApiRequest : IJsonApiApplication, IUpdateRequest, IQueryRequest
33+
{
34+
/// <summary>
35+
/// The request namespace. This may be an absolute or relative path
36+
/// depending upon the configuration.
37+
/// </summary>
38+
/// <example>
39+
/// Absolute: https://example.com/api/v1
40+
///
41+
/// Relative: /api/v1
42+
/// </example>
43+
string BasePath { get; set; }
44+
45+
/// <summary>
46+
/// Stores information to set relationships for the request resource.
47+
/// These relationships must already exist and should not be re-created.
48+
/// By default, it is the responsibility of the repository to use the
49+
/// relationship pointers to persist the relationship.
50+
///
51+
/// The expected use case is POST-ing or PATCH-ing an entity with HasMany
52+
/// relaitonships:
53+
/// <code>
54+
/// {
55+
/// "data": {
56+
/// "type": "photos",
57+
/// "attributes": {
58+
/// "title": "Ember Hamster",
59+
/// "src": "http://example.com/images/productivity.png"
60+
/// },
61+
/// "relationships": {
62+
/// "tags": {
63+
/// "data": [
64+
/// { "type": "tags", "id": "2" },
65+
/// { "type": "tags", "id": "3" }
66+
/// ]
67+
/// }
68+
/// }
69+
/// }
70+
/// }
71+
/// </code>
72+
/// </summary>
73+
HasManyRelationshipPointers HasManyRelationshipPointers { get; }
74+
75+
/// <summary>
76+
/// If the request is a bulk json:api v1.1 operations request.
77+
/// This is determined by the `
78+
/// <see cref="JsonApiDotNetCore.Serialization.JsonApiDeSerializer" />` class.
79+
///
80+
/// See [json-api/1254](https://github.com/json-api/json-api/pull/1254) for details.
81+
/// </summary>
82+
bool IsBulkOperationRequest { get; set; }
83+
84+
/// <summary>
85+
/// The `<see cref="ContextEntity" />`for the target route.
86+
/// </summary>
87+
///
88+
/// <example>
89+
/// For a `GET /articles` request, `RequestEntity` will be set
90+
/// to the `Article` resource representation on the `JsonApiContext`.
91+
/// </example>
92+
ContextEntity RequestEntity { get; set; }
93+
94+
/// <summary>
95+
/// The concrete type of the controller that was activated by the MVC routing middleware
96+
/// </summary>
2997
Type ControllerType { get; set; }
98+
99+
/// <summary>
100+
/// The json:api meta data at the document level
101+
/// </summary>
30102
Dictionary<string, object> DocumentMeta { get; set; }
31-
bool IsBulkOperationRequest { get; set; }
32-
HasManyRelationshipPointers HasManyRelationshipPointers { get; }
33103

104+
/// <summary>
105+
/// If the request is on the `{id}/relationships/{relationshipName}` route
106+
/// </summary>
107+
bool IsRelationshipPath { get; }
108+
109+
[Obsolete("Use `IsRelationshipPath` instead.")]
110+
bool IsRelationshipData { get; set; }
111+
}
112+
113+
public interface IJsonApiContext : IJsonApiRequest
114+
{
115+
IJsonApiContext ApplyContext<T>(object controller);
116+
IMetaBuilder MetaBuilder { get; set; }
117+
IGenericProcessorFactory GenericProcessorFactory { get; set; }
118+
119+
[Obsolete("Use the proxied method IControllerContext.GetControllerAttribute instead.")]
34120
TAttribute GetControllerAttribute<TAttribute>() where TAttribute : Attribute;
35121
}
36122
}

0 commit comments

Comments
 (0)