Skip to content

Commit 4f23fc2

Browse files
author
Bart Koelman
committed
Review non-public TryXXX methods
1 parent 8002a53 commit 4f23fc2

16 files changed

+32
-32
lines changed

src/JsonApiDotNetCore/CollectionConverter.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public ICollection<IIdentifiable> ExtractResources(object? value)
9292
/// <summary>
9393
/// Returns the element type if the specified type is a generic collection, for example: IList{string} -> string or IList -> null.
9494
/// </summary>
95-
public Type? TryGetCollectionElementType(Type? type)
95+
public Type? FindCollectionElementType(Type? type)
9696
{
9797
if (type != null)
9898
{

src/JsonApiDotNetCore/Configuration/ResourceDescriptorAssemblyCache.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ private IEnumerable<ResourceDescriptor> ScanForResourceDescriptors(Assembly asse
4747
{
4848
foreach (Type type in assembly.GetTypes())
4949
{
50-
ResourceDescriptor? resourceDescriptor = _typeLocator.TryGetResourceDescriptor(type);
50+
ResourceDescriptor? resourceDescriptor = _typeLocator.ResolveResourceDescriptor(type);
5151

5252
if (resourceDescriptor != null)
5353
{

src/JsonApiDotNetCore/Configuration/ResourceGraphBuilder.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ public ResourceGraphBuilder Add(Type resourceClrType, Type? idClrType = null, st
131131
if (resourceClrType.IsOrImplementsInterface(typeof(IIdentifiable)))
132132
{
133133
string effectivePublicName = publicName ?? FormatResourceName(resourceClrType);
134-
Type? effectiveIdType = idClrType ?? _typeLocator.TryGetIdType(resourceClrType);
134+
Type? effectiveIdType = idClrType ?? _typeLocator.LookupIdType(resourceClrType);
135135

136136
if (effectiveIdType == null)
137137
{

src/JsonApiDotNetCore/Configuration/ServiceCollectionExtensions.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public static IServiceCollection AddResourceDefinition<TResourceDefinition>(this
9696
private static void RegisterForConstructedType(IServiceCollection services, Type implementationType, IEnumerable<Type> openGenericInterfaces)
9797
{
9898
bool seenCompatibleInterface = false;
99-
ResourceDescriptor? resourceDescriptor = TryGetResourceTypeFromServiceImplementation(implementationType);
99+
ResourceDescriptor? resourceDescriptor = ResolveResourceTypeFromServiceImplementation(implementationType);
100100

101101
if (resourceDescriptor != null)
102102
{
@@ -118,14 +118,14 @@ private static void RegisterForConstructedType(IServiceCollection services, Type
118118
}
119119
}
120120

121-
private static ResourceDescriptor? TryGetResourceTypeFromServiceImplementation(Type? serviceType)
121+
private static ResourceDescriptor? ResolveResourceTypeFromServiceImplementation(Type? serviceType)
122122
{
123123
if (serviceType != null)
124124
{
125125
foreach (Type @interface in serviceType.GetInterfaces())
126126
{
127127
Type? firstGenericArgument = @interface.IsGenericType ? @interface.GenericTypeArguments.First() : null;
128-
ResourceDescriptor? resourceDescriptor = TypeLocator.TryGetResourceDescriptor(firstGenericArgument);
128+
ResourceDescriptor? resourceDescriptor = TypeLocator.ResolveResourceDescriptor(firstGenericArgument);
129129

130130
if (resourceDescriptor != null)
131131
{

src/JsonApiDotNetCore/Configuration/TypeLocator.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ internal sealed class TypeLocator
1414
/// <summary>
1515
/// Attempts to lookup the ID type of the specified resource type. Returns <c>null</c> if it does not implement <see cref="IIdentifiable{TId}" />.
1616
/// </summary>
17-
public Type? TryGetIdType(Type? resourceClrType)
17+
public Type? LookupIdType(Type? resourceClrType)
1818
{
1919
Type? identifiableInterface = resourceClrType?.GetInterfaces().FirstOrDefault(@interface =>
2020
@interface.IsGenericType && @interface.GetGenericTypeDefinition() == typeof(IIdentifiable<>));
@@ -25,11 +25,11 @@ internal sealed class TypeLocator
2525
/// <summary>
2626
/// Attempts to get a descriptor for the specified resource type.
2727
/// </summary>
28-
public ResourceDescriptor? TryGetResourceDescriptor(Type? type)
28+
public ResourceDescriptor? ResolveResourceDescriptor(Type? type)
2929
{
3030
if (type != null && type.IsOrImplementsInterface(typeof(IIdentifiable)))
3131
{
32-
Type? idType = TryGetIdType(type);
32+
Type? idType = LookupIdType(type);
3333

3434
if (idType != null)
3535
{

src/JsonApiDotNetCore/Errors/InvalidModelStateException.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ private Type GetDeclaredCollectionElementType()
341341
{
342342
if (ModelType != typeof(string))
343343
{
344-
Type? elementType = CollectionConverter.TryGetCollectionElementType(ModelType);
344+
Type? elementType = CollectionConverter.FindCollectionElementType(ModelType);
345345

346346
if (elementType != null)
347347
{

src/JsonApiDotNetCore/Middleware/JsonApiMiddleware.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public async Task InvokeAsync(HttpContext httpContext, IControllerResourceMappin
5555
}
5656

5757
RouteValueDictionary routeValues = httpContext.GetRouteData().Values;
58-
ResourceType? primaryResourceType = TryCreatePrimaryResourceType(httpContext, controllerResourceMapping);
58+
ResourceType? primaryResourceType = CreatePrimaryResourceType(httpContext, controllerResourceMapping);
5959

6060
if (primaryResourceType != null)
6161
{
@@ -121,7 +121,7 @@ private async Task<bool> ValidateIfMatchHeaderAsync(HttpContext httpContext, Jso
121121
return true;
122122
}
123123

124-
private static ResourceType? TryCreatePrimaryResourceType(HttpContext httpContext, IControllerResourceMapping controllerResourceMapping)
124+
private static ResourceType? CreatePrimaryResourceType(HttpContext httpContext, IControllerResourceMapping controllerResourceMapping)
125125
{
126126
Endpoint? endpoint = httpContext.GetEndpoint();
127127
var controllerActionDescriptor = endpoint?.Metadata.GetMetadata<ControllerActionDescriptor>();

src/JsonApiDotNetCore/Queries/Internal/QueryableBuilding/QueryClauseBuilder.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public override Expression VisitCount(CountExpression expression, TArgument argu
2525
{
2626
Expression collectionExpression = Visit(expression.TargetCollection, argument);
2727

28-
Expression? propertyExpression = TryGetCollectionCount(collectionExpression);
28+
Expression? propertyExpression = GetCollectionCount(collectionExpression);
2929

3030
if (propertyExpression == null)
3131
{
@@ -35,7 +35,7 @@ public override Expression VisitCount(CountExpression expression, TArgument argu
3535
return propertyExpression;
3636
}
3737

38-
private static Expression? TryGetCollectionCount(Expression? collectionExpression)
38+
private static Expression? GetCollectionCount(Expression? collectionExpression)
3939
{
4040
if (collectionExpression != null)
4141
{

src/JsonApiDotNetCore/Queries/Internal/QueryableBuilding/SelectClauseBuilder.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ private MemberAssignment CreatePropertyAssignment(PropertySelector selector, Lam
155155
private Expression CreateAssignmentRightHandSideForLayer(QueryLayer layer, LambdaScope outerLambdaScope, MemberExpression propertyAccess,
156156
PropertyInfo selectorPropertyInfo, LambdaScopeFactory lambdaScopeFactory)
157157
{
158-
Type? collectionElementType = CollectionConverter.TryGetCollectionElementType(selectorPropertyInfo.PropertyType);
158+
Type? collectionElementType = CollectionConverter.FindCollectionElementType(selectorPropertyInfo.PropertyType);
159159
Type bodyElementType = collectionElementType ?? selectorPropertyInfo.PropertyType;
160160

161161
if (collectionElementType != null)

src/JsonApiDotNetCore/Queries/Internal/QueryableBuilding/WhereClauseBuilder.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public override Expression VisitHas(HasExpression expression, Type? argument)
6060
{
6161
Expression property = Visit(expression.TargetCollection, argument);
6262

63-
Type? elementType = CollectionConverter.TryGetCollectionElementType(property.Type);
63+
Type? elementType = CollectionConverter.FindCollectionElementType(property.Type);
6464

6565
if (elementType == null)
6666
{

src/JsonApiDotNetCore/Repositories/EntityFrameworkCoreRepository.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ protected void AssertIsNotClearingRequiredToOneRelationship(RelationshipAttribut
296296
{
297297
if (relationship is HasOneAttribute)
298298
{
299-
INavigation? navigation = TryGetNavigation(relationship);
299+
INavigation? navigation = GetNavigation(relationship);
300300
bool isRelationshipRequired = navigation?.ForeignKey?.IsRequired ?? false;
301301

302302
bool isClearingRelationship = rightValue == null;
@@ -369,15 +369,15 @@ private NavigationEntry GetNavigationEntry(TResource resource, RelationshipAttri
369369

370370
private bool RequiresLoadOfRelationshipForDeletion(RelationshipAttribute relationship)
371371
{
372-
INavigation? navigation = TryGetNavigation(relationship);
372+
INavigation? navigation = GetNavigation(relationship);
373373
bool isClearOfForeignKeyRequired = navigation?.ForeignKey.DeleteBehavior == DeleteBehavior.ClientSetNull;
374374

375375
bool hasForeignKeyAtLeftSide = HasForeignKeyAtLeftSide(relationship, navigation);
376376

377377
return isClearOfForeignKeyRequired && !hasForeignKeyAtLeftSide;
378378
}
379379

380-
private INavigation? TryGetNavigation(RelationshipAttribute relationship)
380+
private INavigation? GetNavigation(RelationshipAttribute relationship)
381381
{
382382
IEntityType entityType = _dbContext.Model.FindEntityType(typeof(TResource));
383383
return entityType?.FindNavigation(relationship.Property.Name);

src/JsonApiDotNetCore/Resources/Annotations/HasManyAttribute.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ private bool EvaluateIsManyToMany()
3737
{
3838
if (InverseNavigationProperty != null)
3939
{
40-
Type? elementType = CollectionConverter.TryGetCollectionElementType(InverseNavigationProperty.PropertyType);
40+
Type? elementType = CollectionConverter.FindCollectionElementType(InverseNavigationProperty.PropertyType);
4141
return elementType != null;
4242
}
4343

src/JsonApiDotNetCore/Resources/Annotations/HasOneAttribute.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ private bool EvaluateIsOneToOne()
3636
{
3737
if (InverseNavigationProperty != null)
3838
{
39-
Type? elementType = CollectionConverter.TryGetCollectionElementType(InverseNavigationProperty.PropertyType);
39+
Type? elementType = CollectionConverter.FindCollectionElementType(InverseNavigationProperty.PropertyType);
4040
return elementType == null;
4141
}
4242

src/JsonApiDotNetCore/Serialization/JsonConverters/ResourceObjectConverter.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public override ResourceObject Read(ref Utf8JsonReader reader, Type typeToConver
4848
{
4949
// The 'attributes' element may occur before 'type', but we need to know the resource type before we can deserialize attributes
5050
// into their corresponding CLR types.
51-
Type = TryPeekType(ref reader)
51+
Type = PeekType(ref reader)
5252
};
5353

5454
ResourceType? resourceType = resourceObject.Type != null ? _resourceGraph.FindResourceType(resourceObject.Type) : null;
@@ -129,7 +129,7 @@ public override ResourceObject Read(ref Utf8JsonReader reader, Type typeToConver
129129
throw GetEndOfStreamError();
130130
}
131131

132-
private static string? TryPeekType(ref Utf8JsonReader reader)
132+
private static string? PeekType(ref Utf8JsonReader reader)
133133
{
134134
// https://docs.microsoft.com/en-us/dotnet/standard/serialization/system-text-json-converters-how-to?pivots=dotnet-5-0#an-alternative-way-to-do-polymorphic-deserialization
135135
Utf8JsonReader readerClone = reader;

src/JsonApiDotNetCore/Services/JsonApiResourceService.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ public virtual async Task<TResource> GetAsync(TId id, CancellationToken cancella
196196
if (!Equals(resourceFromRequest.Id, default(TId)))
197197
{
198198
TResource? existingResource =
199-
await TryGetPrimaryResourceByIdAsync(resourceFromRequest.Id, TopFieldSelection.OnlyIdAttribute, cancellationToken);
199+
await GetPrimaryResourceByIdOrDefaultAsync(resourceFromRequest.Id, TopFieldSelection.OnlyIdAttribute, cancellationToken);
200200

201201
if (existingResource != null)
202202
{
@@ -462,13 +462,13 @@ public virtual async Task RemoveFromToManyRelationshipAsync(TId leftId, string r
462462

463463
protected async Task<TResource> GetPrimaryResourceByIdAsync(TId id, TopFieldSelection fieldSelection, CancellationToken cancellationToken)
464464
{
465-
TResource? primaryResource = await TryGetPrimaryResourceByIdAsync(id, fieldSelection, cancellationToken);
465+
TResource? primaryResource = await GetPrimaryResourceByIdOrDefaultAsync(id, fieldSelection, cancellationToken);
466466
AssertPrimaryResourceExists(primaryResource);
467467

468468
return primaryResource;
469469
}
470470

471-
private async Task<TResource?> TryGetPrimaryResourceByIdAsync(TId id, TopFieldSelection fieldSelection, CancellationToken cancellationToken)
471+
private async Task<TResource?> GetPrimaryResourceByIdOrDefaultAsync(TId id, TopFieldSelection fieldSelection, CancellationToken cancellationToken)
472472
{
473473
AssertPrimaryResourceTypeInJsonApiRequestIsNotNull(_request.PrimaryResourceType);
474474

test/UnitTests/Graph/TypeLocatorTests.cs

+6-6
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public void GetIdType_Correctly_Identifies_JsonApiResource()
6262
var typeLocator = new TypeLocator();
6363

6464
// Act
65-
Type? idType = typeLocator.TryGetIdType(type);
65+
Type? idType = typeLocator.LookupIdType(type);
6666

6767
// Assert
6868
idType.Should().Be(typeof(int));
@@ -77,22 +77,22 @@ public void GetIdType_Correctly_Identifies_NonJsonApiResource()
7777
var typeLocator = new TypeLocator();
7878

7979
// Act
80-
Type? idType = typeLocator.TryGetIdType(type);
80+
Type? idType = typeLocator.LookupIdType(type);
8181

8282
// Assert
8383
idType.Should().BeNull();
8484
}
8585

8686
[Fact]
87-
public void TryGetResourceDescriptor_Returns_Type_If_Type_Is_IIdentifiable()
87+
public void ResolveResourceDescriptor_Returns_Type_If_Type_Is_IIdentifiable()
8888
{
8989
// Arrange
9090
Type resourceClrType = typeof(Model);
9191

9292
var typeLocator = new TypeLocator();
9393

9494
// Act
95-
ResourceDescriptor? descriptor = typeLocator.TryGetResourceDescriptor(resourceClrType);
95+
ResourceDescriptor? descriptor = typeLocator.ResolveResourceDescriptor(resourceClrType);
9696

9797
// Assert
9898
descriptor.ShouldNotBeNull();
@@ -101,15 +101,15 @@ public void TryGetResourceDescriptor_Returns_Type_If_Type_Is_IIdentifiable()
101101
}
102102

103103
[Fact]
104-
public void TryGetResourceDescriptor_Returns_False_If_Type_Is_IIdentifiable()
104+
public void ResolveResourceDescriptor_Returns_False_If_Type_Is_IIdentifiable()
105105
{
106106
// Arrange
107107
Type resourceClrType = typeof(string);
108108

109109
var typeLocator = new TypeLocator();
110110

111111
// Act
112-
ResourceDescriptor? descriptor = typeLocator.TryGetResourceDescriptor(resourceClrType);
112+
ResourceDescriptor? descriptor = typeLocator.ResolveResourceDescriptor(resourceClrType);
113113

114114
// Assert
115115
descriptor.Should().BeNull();

0 commit comments

Comments
 (0)