Skip to content

Commit 7b4ee7f

Browse files
author
Bart Koelman
committed
Removed single-parameter resource definitions
1 parent 8870ccd commit 7b4ee7f

File tree

21 files changed

+126
-182
lines changed

21 files changed

+126
-182
lines changed

benchmarks/Deserialization/DeserializationBenchmarkBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ protected DeserializationBenchmarkBase()
3030
var resourceDefinitionAccessor = new ResourceDefinitionAccessor(resourceGraph, serviceContainer);
3131

3232
serviceContainer.AddService(typeof(IResourceDefinitionAccessor), resourceDefinitionAccessor);
33-
serviceContainer.AddService(typeof(IResourceDefinition<ResourceA>), new JsonApiResourceDefinition<ResourceA>(resourceGraph));
33+
serviceContainer.AddService(typeof(IResourceDefinition<ResourceA, int>), new JsonApiResourceDefinition<ResourceA, int>(resourceGraph));
3434

3535
// ReSharper disable once VirtualMemberCallInConstructor
3636
JsonApiRequest request = CreateJsonApiRequest(resourceGraph);

src/Examples/JsonApiDotNetCoreExample/Definitions/TodoItemDefinition.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
namespace JsonApiDotNetCoreExample.Definitions
1313
{
1414
[UsedImplicitly(ImplicitUseKindFlags.InstantiatedNoFixedConstructorSignature)]
15-
public sealed class TodoItemDefinition : JsonApiResourceDefinition<TodoItem>
15+
public sealed class TodoItemDefinition : JsonApiResourceDefinition<TodoItem, int>
1616
{
1717
private readonly ISystemClock _systemClock;
1818

src/JsonApiDotNetCore/Configuration/JsonApiApplicationBuilder.cs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -181,33 +181,28 @@ private void AddMiddlewareLayer()
181181

182182
private void AddResourceLayer()
183183
{
184-
RegisterImplementationForOpenInterfaces(ServiceDiscoveryFacade.ResourceDefinitionInterfaces, typeof(JsonApiResourceDefinition<>),
185-
typeof(JsonApiResourceDefinition<,>));
184+
RegisterImplementationForOpenInterfaces(ServiceDiscoveryFacade.ResourceDefinitionInterfaces, typeof(JsonApiResourceDefinition<,>));
186185

187186
_services.AddScoped<IResourceDefinitionAccessor, ResourceDefinitionAccessor>();
188187
_services.AddScoped<IResourceFactory, ResourceFactory>();
189188
}
190189

191190
private void AddRepositoryLayer()
192191
{
193-
RegisterImplementationForOpenInterfaces(ServiceDiscoveryFacade.RepositoryInterfaces, null, typeof(EntityFrameworkCoreRepository<,>));
192+
RegisterImplementationForOpenInterfaces(ServiceDiscoveryFacade.RepositoryInterfaces, typeof(EntityFrameworkCoreRepository<,>));
194193

195194
_services.AddScoped<IResourceRepositoryAccessor, ResourceRepositoryAccessor>();
196195
}
197196

198197
private void AddServiceLayer()
199198
{
200-
RegisterImplementationForOpenInterfaces(ServiceDiscoveryFacade.ServiceInterfaces, null, typeof(JsonApiResourceService<,>));
199+
RegisterImplementationForOpenInterfaces(ServiceDiscoveryFacade.ServiceInterfaces, typeof(JsonApiResourceService<,>));
201200
}
202201

203-
private void RegisterImplementationForOpenInterfaces(HashSet<Type> openGenericInterfaces, Type intImplementation, Type implementation)
202+
private void RegisterImplementationForOpenInterfaces(HashSet<Type> openGenericInterfaces, Type implementationType)
204203
{
205204
foreach (Type openGenericInterface in openGenericInterfaces)
206205
{
207-
Type implementationType = openGenericInterface.GetGenericArguments().Length == 1 && intImplementation != null
208-
? intImplementation
209-
: implementation;
210-
211206
_services.TryAddScoped(openGenericInterface, implementationType);
212207
}
213208
}

src/JsonApiDotNetCore/Configuration/ServiceCollectionExtensions.cs

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Linq;
4-
using System.Reflection;
54
using JetBrains.Annotations;
65
using JsonApiDotNetCore.Errors;
76
using JsonApiDotNetCore.Repositories;
@@ -56,8 +55,8 @@ private static void SetupApplicationBuilder(IServiceCollection services, Action<
5655
}
5756

5857
/// <summary>
59-
/// Adds IoC container registrations for the various JsonApiDotNetCore resource service interfaces, such as <see cref="IGetAllService{TResource}" />,
60-
/// <see cref="ICreateService{TResource}" /> and the various others.
58+
/// Adds IoC container registrations for the various JsonApiDotNetCore resource service interfaces, such as <see cref="IGetAllService{TResource,TId}" />,
59+
/// <see cref="ICreateService{TResource, TId}" /> and the various others.
6160
/// </summary>
6261
public static IServiceCollection AddResourceService<TService>(this IServiceCollection services)
6362
{
@@ -70,7 +69,7 @@ public static IServiceCollection AddResourceService<TService>(this IServiceColle
7069

7170
/// <summary>
7271
/// Adds IoC container registrations for the various JsonApiDotNetCore resource repository interfaces, such as
73-
/// <see cref="IResourceReadRepository{TResource}" /> and <see cref="IResourceWriteRepository{TResource}" />.
72+
/// <see cref="IResourceReadRepository{TResource,TId}" /> and <see cref="IResourceWriteRepository{TResource, TId}" />.
7473
/// </summary>
7574
public static IServiceCollection AddResourceRepository<TRepository>(this IServiceCollection services)
7675
{
@@ -83,7 +82,7 @@ public static IServiceCollection AddResourceRepository<TRepository>(this IServic
8382

8483
/// <summary>
8584
/// Adds IoC container registrations for the various JsonApiDotNetCore resource definition interfaces, such as
86-
/// <see cref="IResourceDefinition{TResource}" /> and <see cref="IResourceDefinition{TResource,TId}" />.
85+
/// <see cref="IResourceDefinition{TResource,TId}" />.
8786
/// </summary>
8887
public static IServiceCollection AddResourceDefinition<TResourceDefinition>(this IServiceCollection services)
8988
{
@@ -103,19 +102,7 @@ private static void RegisterForConstructedType(IServiceCollection services, Type
103102
{
104103
foreach (Type openGenericInterface in openGenericInterfaces)
105104
{
106-
// A shorthand interface is one where the ID type is omitted.
107-
// e.g. IResourceService<TResource> is the shorthand for IResourceService<TResource, TId>
108-
bool isShorthandInterface = openGenericInterface.GetTypeInfo().GenericTypeParameters.Length == 1;
109-
110-
if (isShorthandInterface && resourceDescriptor.IdClrType != typeof(int))
111-
{
112-
// We can't create a shorthand for ID types other than int.
113-
continue;
114-
}
115-
116-
Type constructedType = isShorthandInterface
117-
? openGenericInterface.MakeGenericType(resourceDescriptor.ResourceClrType)
118-
: openGenericInterface.MakeGenericType(resourceDescriptor.ResourceClrType, resourceDescriptor.IdClrType);
105+
Type constructedType = openGenericInterface.MakeGenericType(resourceDescriptor.ResourceClrType, resourceDescriptor.IdClrType);
119106

120107
if (constructedType.IsAssignableFrom(implementationType))
121108
{

src/JsonApiDotNetCore/Configuration/ServiceDiscoveryFacade.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ public sealed class ServiceDiscoveryFacade
4343

4444
internal static readonly HashSet<Type> ResourceDefinitionInterfaces = new()
4545
{
46-
typeof(IResourceDefinition<>),
4746
typeof(IResourceDefinition<,>)
4847
};
4948

src/JsonApiDotNetCore/Repositories/IRepositorySupportsTransaction.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace JsonApiDotNetCore.Repositories
44
{
55
/// <summary>
6-
/// Used to indicate that an <see cref="IResourceRepository{TResource}" /> supports execution inside a transaction.
6+
/// Used to indicate that an <see cref="IResourceRepository{TResource, TId}" /> supports execution inside a transaction.
77
/// </summary>
88
[PublicAPI]
99
public interface IRepositorySupportsTransaction

src/JsonApiDotNetCore/Resources/IResourceDefinition.cs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,6 @@
1010

1111
namespace JsonApiDotNetCore.Resources
1212
{
13-
/// <summary>
14-
/// Provides an extensibility point to add business logic that is resource-oriented instead of endpoint-oriented.
15-
/// </summary>
16-
/// <typeparam name="TResource">
17-
/// The resource type.
18-
/// </typeparam>
19-
[PublicAPI]
20-
public interface IResourceDefinition<TResource> : IResourceDefinition<TResource, int>
21-
where TResource : class, IIdentifiable<int>
22-
{
23-
}
24-
2513
/// <summary>
2614
/// Provides an extensibility point to add business logic that is resource-oriented instead of endpoint-oriented.
2715
/// </summary>

src/JsonApiDotNetCore/Resources/JsonApiResourceDefinition.cs

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,6 @@
1414

1515
namespace JsonApiDotNetCore.Resources
1616
{
17-
/// <summary>
18-
/// Provides a resource-centric extensibility point for executing custom code when something happens with a resource. The goal here is to reduce the need
19-
/// for overriding the service and repository layers.
20-
/// </summary>
21-
/// <typeparam name="TResource">
22-
/// The resource type.
23-
/// </typeparam>
24-
[PublicAPI]
25-
public class JsonApiResourceDefinition<TResource> : JsonApiResourceDefinition<TResource, int>, IResourceDefinition<TResource>
26-
where TResource : class, IIdentifiable<int>
27-
{
28-
public JsonApiResourceDefinition(IResourceGraph resourceGraph)
29-
: base(resourceGraph)
30-
{
31-
}
32-
}
33-
3417
/// <inheritdoc />
3518
[PublicAPI]
3619
public class JsonApiResourceDefinition<TResource, TId> : IResourceDefinition<TResource, TId>

src/JsonApiDotNetCore/Resources/ResourceDefinitionAccessor.cs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -200,17 +200,6 @@ protected object ResolveResourceDefinition(Type resourceClrType)
200200

201201
protected virtual object ResolveResourceDefinition(ResourceType resourceType)
202202
{
203-
if (resourceType.IdentityClrType == typeof(int))
204-
{
205-
Type intResourceDefinitionType = typeof(IResourceDefinition<>).MakeGenericType(resourceType.ClrType);
206-
object intResourceDefinition = _serviceProvider.GetService(intResourceDefinitionType);
207-
208-
if (intResourceDefinition != null)
209-
{
210-
return intResourceDefinition;
211-
}
212-
}
213-
214203
Type resourceDefinitionType = typeof(IResourceDefinition<,>).MakeGenericType(resourceType.ClrType, resourceType.IdentityClrType);
215204
return _serviceProvider.GetRequiredService(resourceDefinitionType);
216205
}

test/DiscoveryTests/ServiceDiscoveryFacadeTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ public void Can_add_resource_definition_from_current_assembly_to_container()
127127
// Assert
128128
ServiceProvider services = _services.BuildServiceProvider();
129129

130-
var resourceDefinition = services.GetRequiredService<IResourceDefinition<TestResource>>();
130+
var resourceDefinition = services.GetRequiredService<IResourceDefinition<TestResource, int>>();
131131
resourceDefinition.Should().BeOfType<TestResourceDefinition>();
132132
}
133133
}

0 commit comments

Comments
 (0)