Skip to content

Commit f2e411c

Browse files
committed
style: rename Entity to Resource
1 parent d038582 commit f2e411c

24 files changed

+50
-68
lines changed

src/JsonApiDotNetCore/Builders/ResourceGraphBuilder.cs

+5-5
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public IResourceGraph Build()
3838

3939
private void SetResourceLinksOptions(ResourceContext resourceContext)
4040
{
41-
var attribute = (LinksAttribute)resourceContext.EntityType.GetCustomAttribute(typeof(LinksAttribute));
41+
var attribute = (LinksAttribute)resourceContext.ResourceType.GetCustomAttribute(typeof(LinksAttribute));
4242
if (attribute != null)
4343
{
4444
resourceContext.RelationshipLinks = attribute.RelationshipLinks;
@@ -69,12 +69,12 @@ public IResourceGraphBuilder AddResource(Type entityType, Type idType, string pl
6969

7070
private ResourceContext GetEntity(string pluralizedTypeName, Type entityType, Type idType) => new ResourceContext
7171
{
72-
EntityName = pluralizedTypeName,
73-
EntityType = entityType,
72+
ResourceName = pluralizedTypeName,
73+
ResourceType = entityType,
7474
IdentityType = idType,
7575
Attributes = GetAttributes(entityType),
7676
Relationships = GetRelationships(entityType),
77-
ResourceType = GetResourceDefinitionType(entityType)
77+
ResourceDefinitionType = GetResourceDefinitionType(entityType)
7878
};
7979

8080

@@ -234,7 +234,7 @@ private string GetResourceNameFromDbSetProperty(PropertyInfo property, Type reso
234234

235235
private void AssertEntityIsNotAlreadyDefined(Type entityType)
236236
{
237-
if (_entities.Any(e => e.EntityType == entityType))
237+
if (_entities.Any(e => e.ResourceType == entityType))
238238
throw new InvalidOperationException($"Cannot add entity type {entityType} to context resourceGraph, there is already an entity of that type configured.");
239239
}
240240
}

src/JsonApiDotNetCore/Hooks/Execution/HookExecutorHelper.cs

+1-10
Original file line numberDiff line numberDiff line change
@@ -97,23 +97,14 @@ public HashSet<TEntity> LoadDbValues<TEntity>(IEnumerable<TEntity> entities, Res
9797
return new HashSet<TEntity>(dbValues);
9898
}
9999

100-
101100
public bool ShouldLoadDbValues(Type entityType, ResourceHook hook)
102101
{
103102
var discovery = GetHookDiscovery(entityType);
104-
105103
if (discovery.DatabaseValuesDisabledHooks.Contains(hook))
106-
{
107104
return false;
108-
}
109105
if (discovery.DatabaseValuesEnabledHooks.Contains(hook))
110-
{
111106
return true;
112-
}
113-
else
114-
{
115-
return _options.LoaDatabaseValues;
116-
}
107+
return _options.LoaDatabaseValues;
117108
}
118109

119110
bool ShouldExecuteHook(DependentType entityType, ResourceHook hook)

src/JsonApiDotNetCore/Internal/Contracts/IContextEntityProvider.cs renamed to src/JsonApiDotNetCore/Internal/Contracts/IResourceContextProvider.cs

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
using System;
2-
using System.Collections.Generic;
3-
using System.Linq.Expressions;
4-
using JsonApiDotNetCore.Internal;
52
using JsonApiDotNetCore.Models;
63

74
namespace JsonApiDotNetCore.Internal.Contracts
@@ -14,7 +11,7 @@ public interface IResourceContextProvider
1411
/// <summary>
1512
/// Gets all registered context entities
1613
/// </summary>
17-
ResourceContext[] GetContextEntities();
14+
ResourceContext[] GetResourceContexts();
1815

1916
/// <summary>
2017
/// Get the resource metadata by the DbSet property name

src/JsonApiDotNetCore/Internal/InverseRelationships.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@ public void Resolve()
4545
{
4646
DbContext context = _resolver.GetContext();
4747

48-
foreach (ResourceContext ce in _provider.GetContextEntities())
48+
foreach (ResourceContext ce in _provider.GetResourceContexts())
4949
{
50-
IEntityType meta = context.Model.FindEntityType(ce.EntityType);
50+
IEntityType meta = context.Model.FindEntityType(ce.ResourceType);
5151
if (meta == null) continue;
5252
foreach (var attr in ce.Relationships)
5353
{

src/JsonApiDotNetCore/Internal/ContextEntity.cs renamed to src/JsonApiDotNetCore/Internal/ResourceContext.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ public class ResourceContext
1111
/// <summary>
1212
/// The exposed resource name
1313
/// </summary>
14-
public string EntityName { get; set; }
14+
public string ResourceName { get; set; }
1515

1616
/// <summary>
1717
/// The data model type
1818
/// </summary>
19-
public Type EntityType { get; set; }
19+
public Type ResourceType { get; set; }
2020

2121
/// <summary>
2222
/// The identity member type
@@ -27,7 +27,7 @@ public class ResourceContext
2727
/// The concrete <see cref="ResourceDefinition{T}"/> type.
2828
/// We store this so that we don't need to re-compute the generic type.
2929
/// </summary>
30-
public Type ResourceType { get; set; }
30+
public Type ResourceDefinitionType { get; set; }
3131

3232
/// <summary>
3333
/// Exposed resource attributes.

src/JsonApiDotNetCore/Internal/ResourceGraph.cs

+5-11
Original file line numberDiff line numberDiff line change
@@ -13,28 +13,25 @@ namespace JsonApiDotNetCore.Internal
1313
public class ResourceGraph : IResourceGraph
1414
{
1515
internal List<ValidationResult> ValidationResults { get; }
16-
private List<ResourceContext> _entities { get; }
16+
private List<ResourceContext> _resources { get; }
1717

1818
public ResourceGraph(List<ResourceContext> entities, List<ValidationResult> validationResults = null)
1919
{
20-
_entities = entities;
20+
_resources = entities;
2121
ValidationResults = validationResults;
2222
}
2323

2424
/// <inheritdoc />
25-
public ResourceContext[] GetContextEntities() => _entities.ToArray();
26-
25+
public ResourceContext[] GetResourceContexts() => _resources.ToArray();
2726
/// <inheritdoc />
2827
public ResourceContext GetResourceContext(string entityName)
29-
=> _entities.SingleOrDefault(e => string.Equals(e.EntityName, entityName, StringComparison.OrdinalIgnoreCase));
30-
28+
=> _resources.SingleOrDefault(e => string.Equals(e.ResourceName, entityName, StringComparison.OrdinalIgnoreCase));
3129
/// <inheritdoc />
3230
public ResourceContext GetResourceContext(Type entityType)
33-
=> _entities.SingleOrDefault(e => e.EntityType == entityType);
31+
=> _resources.SingleOrDefault(e => e.ResourceType == entityType);
3432
/// <inheritdoc />
3533
public ResourceContext GetResourceContext<TResource>() where TResource : class, IIdentifiable
3634
=> GetResourceContext(typeof(TResource));
37-
3835
/// <inheritdoc/>
3936
public List<IResourceField> GetFields<T>(Expression<Func<T, dynamic>> selector = null) where T : IIdentifiable
4037
{
@@ -65,7 +62,6 @@ public List<RelationshipAttribute> GetRelationships(Type type)
6562
{
6663
return GetResourceContext(type).Relationships.ToList();
6764
}
68-
6965
/// <inheritdoc />
7066
public RelationshipAttribute GetInverse(RelationshipAttribute relationship)
7167
{
@@ -144,7 +140,5 @@ private enum FieldFilterType
144140
Attribute,
145141
Relationship
146142
}
147-
148-
149143
}
150144
}

src/JsonApiDotNetCore/Middleware/DefaultTypeMatchFilter.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public void OnActionExecuting(ActionExecutingContext context)
3333

3434
throw new JsonApiException(409,
3535
$"Cannot '{context.HttpContext.Request.Method}' type '{deserializedType.Name}' "
36-
+ $"to '{expectedJsonApiResource?.EntityName}' endpoint.",
36+
+ $"to '{expectedJsonApiResource?.ResourceName}' endpoint.",
3737
detail: "Check that the request payload type matches the type expected by this endpoint.");
3838
}
3939
}

src/JsonApiDotNetCore/Middleware/RequestMiddleware.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public async Task Invoke(HttpContext httpContext,
4646
{
4747
_currentRequest.SetRequestResource(GetCurrentEntity());
4848
_currentRequest.IsRelationshipPath = PathIsRelationship();
49-
_currentRequest.BasePath = GetBasePath(_currentRequest.GetRequestResource().EntityName);
49+
_currentRequest.BasePath = GetBasePath(_currentRequest.GetRequestResource().ResourceName);
5050
}
5151

5252
if (IsValid())

src/JsonApiDotNetCore/QueryParameterServices/Common/QueryParameterService.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ protected RelationshipAttribute GetRelationship(string propertyName)
6666
if (propertyName == null) return null;
6767
var relationship = _requestResource.Relationships.FirstOrDefault(r => r.Is(propertyName));
6868
if (relationship == null)
69-
throw new JsonApiException(400, $"{propertyName} is not a valid relationship on {_requestResource.EntityName}.");
69+
throw new JsonApiException(400, $"{propertyName} is not a valid relationship on {_requestResource.ResourceName}.");
7070

7171
return relationship;
7272
}

src/JsonApiDotNetCore/QueryParameterServices/FilterService.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public class FilterService : QueryParameterService, IFilterService
1818

1919
public FilterService(IResourceDefinitionProvider resourceDefinitionProvider, IResourceGraph resourceGraph, ICurrentRequest currentRequest) : base(resourceGraph, currentRequest)
2020
{
21-
_requestResourceDefinition = resourceDefinitionProvider.Get(_requestResource.EntityType);
21+
_requestResourceDefinition = resourceDefinitionProvider.Get(_requestResource.ResourceType);
2222
_filters = new List<FilterQueryContext>();
2323
}
2424

src/JsonApiDotNetCore/QueryParameterServices/IncludeService.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,13 @@ private void ParseChain(string chain)
5959

6060
private JsonApiException CannotIncludeError(ResourceContext resourceContext, string requestedRelationship)
6161
{
62-
return new JsonApiException(400, $"Including the relationship {requestedRelationship} on {resourceContext.EntityName} is not allowed");
62+
return new JsonApiException(400, $"Including the relationship {requestedRelationship} on {resourceContext.ResourceName} is not allowed");
6363
}
6464

6565
private JsonApiException InvalidRelationshipError(ResourceContext resourceContext, string requestedRelationship)
6666
{
67-
return new JsonApiException(400, $"Invalid relationship {requestedRelationship} on {resourceContext.EntityName}",
68-
$"{resourceContext.EntityName} does not have a relationship named {requestedRelationship}");
67+
return new JsonApiException(400, $"Invalid relationship {requestedRelationship} on {resourceContext.ResourceName}",
68+
$"{resourceContext.ResourceName} does not have a relationship named {requestedRelationship}");
6969
}
7070
}
7171
}

src/JsonApiDotNetCore/QueryParameterServices/SortService.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public List<SortQueryContext> Get()
3939
{
4040
if (_queries == null)
4141
{
42-
var requestResourceDefinition = _resourceDefinitionProvider.Get(_requestResource.EntityType);
42+
var requestResourceDefinition = _resourceDefinitionProvider.Get(_requestResource.ResourceType);
4343
if (requestResourceDefinition != null)
4444
return requestResourceDefinition.DefaultSort()?.Select(d => BuildQueryContext(new SortQuery(d.Item1.PublicAttributeName, d.Item2))).ToList();
4545
}

src/JsonApiDotNetCore/QueryParameterServices/SparseFieldsService.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public virtual void Parse(KeyValuePair<string, StringValues> queryParameter)
6161
// it is possible that the request resource has a relationship
6262
// that is equal to the resource name, like with self-referering data types (eg directory structures)
6363
// if not, no longer support this type of sparse field selection.
64-
if (navigation == _requestResource.EntityName && !_requestResource.Relationships.Any(a => a.Is(navigation)))
64+
if (navigation == _requestResource.ResourceName && !_requestResource.Relationships.Any(a => a.Is(navigation)))
6565
throw new JsonApiException(400, $"Use \"?fields=...\" instead of \"fields[{navigation}]\":" +
6666
$" the square bracket navigations is now reserved " +
6767
$"for relationships only. See https://github.com/json-api-dotnet/JsonApiDotNetCore/issues/555#issuecomment-543100865");
@@ -71,7 +71,7 @@ public virtual void Parse(KeyValuePair<string, StringValues> queryParameter)
7171

7272
var relationship = _requestResource.Relationships.SingleOrDefault(a => a.Is(navigation));
7373
if (relationship == null)
74-
throw new JsonApiException(400, $"\"{navigation}\" in \"fields[{navigation}]\" is not a valid relationship of {_requestResource.EntityName}");
74+
throw new JsonApiException(400, $"\"{navigation}\" in \"fields[{navigation}]\" is not a valid relationship of {_requestResource.ResourceName}");
7575

7676
foreach (var field in fields)
7777
RegisterRelatedResourceField(field, relationship);
@@ -100,7 +100,7 @@ private void RegisterRequestResourceField(string field)
100100
{
101101
var attr = _requestResource.Attributes.SingleOrDefault(a => a.Is(field));
102102
if (attr == null)
103-
throw new JsonApiException(400, $"'{_requestResource.EntityName}' does not contain '{field}'.");
103+
throw new JsonApiException(400, $"'{_requestResource.ResourceName}' does not contain '{field}'.");
104104

105105
(_selectedFields = _selectedFields ?? new List<AttrAttribute>()).Add(attr);
106106
}

src/JsonApiDotNetCore/Serialization/Common/BaseDocumentParser.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ private IIdentifiable ParseResourceObject(ResourceObject data)
138138
+ "If you have manually registered the resource, check that the call to AddResource correctly sets the public name.");
139139
}
140140

141-
var entity = (IIdentifiable)Activator.CreateInstance(contextEntity.EntityType);
141+
var entity = (IIdentifiable)Activator.CreateInstance(contextEntity.ResourceType);
142142

143143
entity = SetAttributes(entity, data.Attributes, contextEntity.Attributes);
144144
entity = SetRelationships(entity, data.Relationships, contextEntity.Relationships);

src/JsonApiDotNetCore/Serialization/Common/ResourceObjectBuilder.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public ResourceObject Build(IIdentifiable entity, IEnumerable<AttrAttribute> att
2828
var resourceContext = _provider.GetResourceContext(entity.GetType());
2929

3030
// populating the top-level "type" and "id" members.
31-
var ro = new ResourceObject { Type = resourceContext.EntityName, Id = entity.StringId.NullIfEmpty() };
31+
var ro = new ResourceObject { Type = resourceContext.ResourceName, Id = entity.StringId.NullIfEmpty() };
3232

3333
// populating the top-level "attribute" member of a resource object. never include "id" as an attribute
3434
if (attributes != null && (attributes = attributes.Where(attr => attr.InternalAttributeName != _identifiablePropertyName)).Any())
@@ -98,7 +98,7 @@ private List<ResourceIdentifierObject> GetRelatedResourceLinkage(HasManyAttribut
9898
/// </summary>
9999
private ResourceIdentifierObject GetResourceIdentifier(IIdentifiable entity)
100100
{
101-
var resourceName = _provider.GetResourceContext(entity.GetType()).EntityName;
101+
var resourceName = _provider.GetResourceContext(entity.GetType()).ResourceName;
102102
return new ResourceIdentifierObject
103103
{
104104
Type = resourceName,

src/JsonApiDotNetCore/Serialization/Server/Builders/IncludedResourceObjectBuilder.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ protected override RelationshipEntry GetRelationshipData(RelationshipAttribute r
121121
private ResourceObject GetOrBuildResourceObject(IIdentifiable parent, RelationshipAttribute relationship)
122122
{
123123
var type = parent.GetType();
124-
var resourceName = _provider.GetResourceContext(type).EntityName;
124+
var resourceName = _provider.GetResourceContext(type).ResourceName;
125125
var entry = _included.SingleOrDefault(ro => ro.Type == resourceName && ro.Id == parent.StringId);
126126
if (entry == null)
127127
{

src/JsonApiDotNetCore/Serialization/Server/Builders/LinkBuilder.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public TopLevelLinks GetTopLevelLinks(ResourceContext primaryResource)
3131
{
3232
TopLevelLinks topLevelLinks = null;
3333
if (ShouldAddTopLevelLink(primaryResource, Link.Self))
34-
topLevelLinks = new TopLevelLinks { Self = GetSelfTopLevelLink(primaryResource.EntityName) };
34+
topLevelLinks = new TopLevelLinks { Self = GetSelfTopLevelLink(primaryResource.ResourceName) };
3535

3636
if (ShouldAddTopLevelLink(primaryResource, Link.Paging))
3737
SetPageLinks(primaryResource, ref topLevelLinks);
@@ -80,7 +80,7 @@ private string GetSelfTopLevelLink(string resourceName)
8080

8181
private string GetPageLink(ResourceContext primaryResource, int pageOffset, int pageSize)
8282
{
83-
return $"{GetBasePath()}/{primaryResource.EntityName}?page[size]={pageSize}&page[number]={pageOffset}";
83+
return $"{GetBasePath()}/{primaryResource.ResourceName}?page[size]={pageSize}&page[number]={pageOffset}";
8484
}
8585

8686

@@ -101,12 +101,12 @@ public RelationshipLinks GetRelationshipLinks(RelationshipAttribute relationship
101101
var childNavigation = relationship.PublicRelationshipName;
102102
RelationshipLinks links = null;
103103
if (ShouldAddRelationshipLink(parentResourceContext, relationship, Link.Related))
104-
links = new RelationshipLinks { Related = GetRelatedRelationshipLink(parentResourceContext.EntityName, parent.StringId, childNavigation) };
104+
links = new RelationshipLinks { Related = GetRelatedRelationshipLink(parentResourceContext.ResourceName, parent.StringId, childNavigation) };
105105

106106
if (ShouldAddRelationshipLink(parentResourceContext, relationship, Link.Self))
107107
{
108108
links = links ?? new RelationshipLinks();
109-
links.Self = GetSelfRelationshipLink(parentResourceContext.EntityName, parent.StringId, childNavigation);
109+
links.Self = GetSelfRelationshipLink(parentResourceContext.ResourceName, parent.StringId, childNavigation);
110110
}
111111

112112
return links;

src/JsonApiDotNetCore/Serialization/Server/ResponseSerializerFactory.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ private Type GetDocumentPrimaryType()
4646
if (_currentRequest.RequestRelationship != null && !_currentRequest.IsRelationshipPath)
4747
return _currentRequest.RequestRelationship.DependentType;
4848

49-
return _currentRequest.GetRequestResource()?.EntityType;
49+
return _currentRequest.GetRequestResource()?.ResourceType;
5050
}
5151
}
5252
}

src/JsonApiDotNetCore/Services/ResourceDefinitionProvider.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public ResourceDefinitionProvider(IResourceGraph resourceContextProvider, IScope
2020
/// <inheritdoc/>
2121
public IResourceDefinition Get(Type resourceType)
2222
{
23-
return (IResourceDefinition)_serviceProvider.GetService(_resourceContextProvider.GetResourceContext(resourceType).ResourceType);
23+
return (IResourceDefinition)_serviceProvider.GetService(_resourceContextProvider.GetResourceContext(resourceType).ResourceDefinitionType);
2424
}
2525
}
2626
}

test/UnitTests/Builders/ContextGraphBuilder_Tests.cs

+5-5
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ public void Can_Build_ResourceGraph_Using_Builder()
3737
var resourceGraph = container.GetRequiredService<IResourceGraph>();
3838
var dbResource = resourceGraph.GetResourceContext("db-resources");
3939
var nonDbResource = resourceGraph.GetResourceContext("non-db-resources");
40-
Assert.Equal(typeof(DbResource), dbResource.EntityType);
41-
Assert.Equal(typeof(NonDbResource), nonDbResource.EntityType);
42-
Assert.Equal(typeof(ResourceDefinition<NonDbResource>), nonDbResource.ResourceType);
40+
Assert.Equal(typeof(DbResource), dbResource.ResourceType);
41+
Assert.Equal(typeof(NonDbResource), nonDbResource.ResourceType);
42+
Assert.Equal(typeof(ResourceDefinition<NonDbResource>), nonDbResource.ResourceDefinitionType);
4343
}
4444

4545
[Fact]
@@ -54,7 +54,7 @@ public void Resources_Without_Names_Specified_Will_Use_Default_Formatter()
5454

5555
// assert
5656
var resource = resourceGraph.GetResourceContext(typeof(TestResource));
57-
Assert.Equal("test-resources", resource.EntityName);
57+
Assert.Equal("test-resources", resource.ResourceName);
5858
}
5959

6060
[Fact]
@@ -69,7 +69,7 @@ public void Resources_Without_Names_Specified_Will_Use_Configured_Formatter()
6969

7070
// assert
7171
var resource = resourceGraph.GetResourceContext(typeof(TestResource));
72-
Assert.Equal("testResources", resource.EntityName);
72+
Assert.Equal("testResources", resource.ResourceName);
7373
}
7474

7575
[Fact]

test/UnitTests/Builders/LinkBuilderTests.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ private ResourceContext GetResourceContext<TResource>(Link resourceLinks = Link.
211211
ResourceLinks = resourceLinks,
212212
TopLevelLinks = topLevelLinks,
213213
RelationshipLinks = relationshipLinks,
214-
EntityName = typeof(TResource).Name.Dasherize() + "s"
214+
ResourceName = typeof(TResource).Name.Dasherize() + "s"
215215
};
216216
}
217217
}

0 commit comments

Comments
 (0)