Skip to content

Commit fe1f350

Browse files
author
Bart Koelman
committed
AV1008: Class should not be static
1 parent 1d73d87 commit fe1f350

32 files changed

+106
-45
lines changed

benchmarks/BenchmarkResourcePublicNames.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#pragma warning disable AV1008 // Class should not be static
2+
13
namespace Benchmarks
24
{
35
internal static class BenchmarkResourcePublicNames

benchmarks/DependencyFactory.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33

44
namespace Benchmarks
55
{
6-
internal static class DependencyFactory
6+
internal sealed class DependencyFactory
77
{
8-
public static IResourceGraph CreateResourceGraph(IJsonApiOptions options)
8+
public IResourceGraph CreateResourceGraph(IJsonApiOptions options)
99
{
1010
var builder = new ResourceGraphBuilder(options, NullLoggerFactory.Instance);
1111
builder.Add<BenchmarkResource>(BenchmarkResourcePublicNames.Type);

benchmarks/Query/QueryParserBenchmarks.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ namespace Benchmarks.Query
2020
[MemoryDiagnoser]
2121
public class QueryParserBenchmarks
2222
{
23+
private readonly DependencyFactory _dependencyFactory = new DependencyFactory();
2324
private readonly FakeRequestQueryStringAccessor _queryStringAccessor = new FakeRequestQueryStringAccessor();
2425
private readonly QueryStringReader _queryStringReaderForSort;
2526
private readonly QueryStringReader _queryStringReaderForAll;
@@ -31,7 +32,7 @@ public QueryParserBenchmarks()
3132
EnableLegacyFilterNotation = true
3233
};
3334

34-
IResourceGraph resourceGraph = DependencyFactory.CreateResourceGraph(options);
35+
IResourceGraph resourceGraph = _dependencyFactory.CreateResourceGraph(options);
3536

3637
var request = new JsonApiRequest
3738
{

benchmarks/Serialization/JsonApiDeserializerBenchmarks.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,13 @@ public class JsonApiDeserializerBenchmarks
2929
}
3030
});
3131

32+
private readonly DependencyFactory _dependencyFactory = new DependencyFactory();
3233
private readonly IJsonApiDeserializer _jsonApiDeserializer;
3334

3435
public JsonApiDeserializerBenchmarks()
3536
{
3637
var options = new JsonApiOptions();
37-
IResourceGraph resourceGraph = DependencyFactory.CreateResourceGraph(options);
38+
IResourceGraph resourceGraph = _dependencyFactory.CreateResourceGraph(options);
3839
var targetedFields = new TargetedFields();
3940
var request = new JsonApiRequest();
4041
var resourceFactory = new ResourceFactory(new ServiceContainer());

benchmarks/Serialization/JsonApiSerializerBenchmarks.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,13 @@ public class JsonApiSerializerBenchmarks
2121
Name = Guid.NewGuid().ToString()
2222
};
2323

24+
private readonly DependencyFactory _dependencyFactory = new DependencyFactory();
2425
private readonly IJsonApiSerializer _jsonApiSerializer;
2526

2627
public JsonApiSerializerBenchmarks()
2728
{
2829
var options = new JsonApiOptions();
29-
IResourceGraph resourceGraph = DependencyFactory.CreateResourceGraph(options);
30+
IResourceGraph resourceGraph = _dependencyFactory.CreateResourceGraph(options);
3031
IFieldsToSerialize fieldsToSerialize = CreateFieldsToSerialize(resourceGraph);
3132

3233
IMetaBuilder metaBuilder = new Mock<IMetaBuilder>().Object;

src/JsonApiDotNetCore/ArgumentGuard.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
using System.Linq;
44
using JetBrains.Annotations;
55

6+
#pragma warning disable AV1008 // Class should not be static
7+
68
namespace JsonApiDotNetCore
79
{
810
internal static class ArgumentGuard

src/JsonApiDotNetCore/ArrayFactory.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#pragma warning disable AV1008 // Class should not be static
2+
13
namespace JsonApiDotNetCore
24
{
35
internal static class ArrayFactory

src/JsonApiDotNetCore/Configuration/ResourceDescriptorAssemblyCache.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ namespace JsonApiDotNetCore.Configuration
1010
/// </summary>
1111
internal sealed class ResourceDescriptorAssemblyCache
1212
{
13+
private readonly TypeLocator _typeLocator = new TypeLocator();
14+
1315
private readonly Dictionary<Assembly, IReadOnlyCollection<ResourceDescriptor>> _resourceDescriptorsPerAssembly =
1416
new Dictionary<Assembly, IReadOnlyCollection<ResourceDescriptor>>();
1517

@@ -36,11 +38,11 @@ private void EnsureAssembliesScanned()
3638
}
3739
}
3840

39-
private static IEnumerable<ResourceDescriptor> ScanForResourceDescriptors(Assembly assembly)
41+
private IEnumerable<ResourceDescriptor> ScanForResourceDescriptors(Assembly assembly)
4042
{
4143
foreach (Type type in assembly.GetTypes())
4244
{
43-
ResourceDescriptor resourceDescriptor = TypeLocator.TryGetResourceDescriptor(type);
45+
ResourceDescriptor resourceDescriptor = _typeLocator.TryGetResourceDescriptor(type);
4446

4547
if (resourceDescriptor != null)
4648
{

src/JsonApiDotNetCore/Configuration/ResourceGraphBuilder.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public class ResourceGraphBuilder
1919
private readonly IJsonApiOptions _options;
2020
private readonly ILogger<ResourceGraphBuilder> _logger;
2121
private readonly List<ResourceContext> _resources = new List<ResourceContext>();
22+
private readonly TypeLocator _typeLocator = new TypeLocator();
2223

2324
public ResourceGraphBuilder(IJsonApiOptions options, ILoggerFactory loggerFactory)
2425
{
@@ -110,7 +111,7 @@ public ResourceGraphBuilder Add(Type resourceType, Type idType = null, string pu
110111
if (TypeHelper.IsOrImplementsInterface(resourceType, typeof(IIdentifiable)))
111112
{
112113
string effectivePublicName = publicName ?? FormatResourceName(resourceType);
113-
Type effectiveIdType = idType ?? TypeLocator.TryGetIdType(resourceType);
114+
Type effectiveIdType = idType ?? _typeLocator.TryGetIdType(resourceType);
114115

115116
ResourceContext resourceContext = CreateResourceContext(effectivePublicName, resourceType, effectiveIdType);
116117
_resources.Add(resourceContext);

src/JsonApiDotNetCore/Configuration/ServiceCollectionExtensions.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ namespace JsonApiDotNetCore.Configuration
1616
[PublicAPI]
1717
public static class ServiceCollectionExtensions
1818
{
19+
private static readonly TypeLocator TypeLocator = new TypeLocator();
20+
1921
/// <summary>
2022
/// Configures JsonApiDotNetCore by registering resources manually.
2123
/// </summary>

src/JsonApiDotNetCore/Configuration/ServiceDiscoveryFacade.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ public class ServiceDiscoveryFacade
7070
private readonly ResourceGraphBuilder _resourceGraphBuilder;
7171
private readonly IJsonApiOptions _options;
7272
private readonly ResourceDescriptorAssemblyCache _assemblyCache = new ResourceDescriptorAssemblyCache();
73+
private readonly TypeLocator _typeLocator = new TypeLocator();
7374

7475
public ServiceDiscoveryFacade(IServiceCollection services, ResourceGraphBuilder resourceGraphBuilder, IJsonApiOptions options,
7576
ILoggerFactory loggerFactory)
@@ -140,7 +141,7 @@ private void AddInjectables(IReadOnlyCollection<ResourceDescriptor> resourceDesc
140141

141142
private void AddDbContextResolvers(Assembly assembly)
142143
{
143-
IEnumerable<Type> dbContextTypes = TypeLocator.GetDerivedTypes(assembly, typeof(DbContext));
144+
IEnumerable<Type> dbContextTypes = _typeLocator.GetDerivedTypes(assembly, typeof(DbContext));
144145

145146
foreach (Type dbContextType in dbContextTypes)
146147
{
@@ -158,7 +159,7 @@ private void AddResourceHookDefinitions(Assembly assembly, ResourceDescriptor id
158159
{
159160
try
160161
{
161-
Type resourceDefinition = TypeLocator.GetDerivedGenericTypes(assembly, typeof(ResourceHooksDefinition<>), identifiable.ResourceType)
162+
Type resourceDefinition = _typeLocator.GetDerivedGenericTypes(assembly, typeof(ResourceHooksDefinition<>), identifiable.ResourceType)
162163
.SingleOrDefault();
163164

164165
if (resourceDefinition != null)
@@ -203,8 +204,8 @@ private void RegisterImplementations(Assembly assembly, Type interfaceType, Reso
203204
? ArrayFactory.Create(resourceDescriptor.ResourceType, resourceDescriptor.IdType)
204205
: ArrayFactory.Create(resourceDescriptor.ResourceType);
205206

206-
(Type implementation, Type registrationInterface)?
207-
result = TypeLocator.GetGenericInterfaceImplementation(assembly, interfaceType, genericArguments);
207+
(Type implementation, Type registrationInterface)? result =
208+
_typeLocator.GetGenericInterfaceImplementation(assembly, interfaceType, genericArguments);
208209

209210
if (result != null)
210211
{

src/JsonApiDotNetCore/Configuration/TypeLocator.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ namespace JsonApiDotNetCore.Configuration
99
/// <summary>
1010
/// Used to locate types and facilitate resource auto-discovery.
1111
/// </summary>
12-
internal static class TypeLocator
12+
internal sealed class TypeLocator
1313
{
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 static Type TryGetIdType(Type resourceType)
17+
public Type TryGetIdType(Type resourceType)
1818
{
1919
Type identifiableInterface = resourceType.GetInterfaces().FirstOrDefault(@interface =>
2020
@interface.IsGenericType && @interface.GetGenericTypeDefinition() == typeof(IIdentifiable<>));
@@ -25,7 +25,7 @@ public static Type TryGetIdType(Type resourceType)
2525
/// <summary>
2626
/// Attempts to get a descriptor for the specified resource type.
2727
/// </summary>
28-
public static ResourceDescriptor TryGetResourceDescriptor(Type type)
28+
public ResourceDescriptor TryGetResourceDescriptor(Type type)
2929
{
3030
if (TypeHelper.IsOrImplementsInterface(type, typeof(IIdentifiable)))
3131
{
@@ -57,7 +57,7 @@ public static ResourceDescriptor TryGetResourceDescriptor(Type type)
5757
/// GetGenericInterfaceImplementation(assembly, typeof(IResourceService<,>), typeof(Article), typeof(Guid));
5858
/// ]]></code>
5959
/// </example>
60-
public static (Type implementation, Type registrationInterface)? GetGenericInterfaceImplementation(Assembly assembly, Type openGenericInterface,
60+
public (Type implementation, Type registrationInterface)? GetGenericInterfaceImplementation(Assembly assembly, Type openGenericInterface,
6161
params Type[] interfaceGenericTypeArguments)
6262
{
6363
ArgumentGuard.NotNull(assembly, nameof(assembly));
@@ -121,7 +121,7 @@ private static (Type implementation, Type registrationInterface)? FindGenericInt
121121
/// GetDerivedGenericTypes(assembly, typeof(ResourceDefinition<>), typeof(Article))
122122
/// ]]></code>
123123
/// </example>
124-
public static IReadOnlyCollection<Type> GetDerivedGenericTypes(Assembly assembly, Type openGenericType, params Type[] genericArguments)
124+
public IReadOnlyCollection<Type> GetDerivedGenericTypes(Assembly assembly, Type openGenericType, params Type[] genericArguments)
125125
{
126126
Type genericType = openGenericType.MakeGenericType(genericArguments);
127127
return GetDerivedTypes(assembly, genericType).ToArray();
@@ -141,7 +141,7 @@ public static IReadOnlyCollection<Type> GetDerivedGenericTypes(Assembly assembly
141141
/// GetDerivedGenericTypes(assembly, typeof(DbContext))
142142
/// </code>
143143
/// </example>
144-
public static IEnumerable<Type> GetDerivedTypes(Assembly assembly, Type inheritedType)
144+
public IEnumerable<Type> GetDerivedTypes(Assembly assembly, Type inheritedType)
145145
{
146146
foreach (Type type in assembly.GetTypes())
147147
{

src/JsonApiDotNetCore/Hooks/Internal/Execution/HookExecutorHelper.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ namespace JsonApiDotNetCore.Hooks.Internal.Execution
1919
/// <inheritdoc />
2020
internal sealed class HookExecutorHelper : IHookExecutorHelper
2121
{
22+
private static readonly IncludeChainConverter IncludeChainConverter = new IncludeChainConverter();
23+
2224
private readonly IdentifiableComparer _comparer = IdentifiableComparer.Instance;
2325
private readonly IJsonApiOptions _options;
2426
private readonly IGenericServiceFactory _genericProcessorFactory;

src/JsonApiDotNetCore/Hooks/Internal/ResourceHookExecutor.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ namespace JsonApiDotNetCore.Hooks.Internal
2121
/// <inheritdoc />
2222
internal sealed class ResourceHookExecutor : IResourceHookExecutor
2323
{
24+
private static readonly IncludeChainConverter IncludeChainConverter = new IncludeChainConverter();
25+
2426
private readonly IHookExecutorHelper _executorHelper;
2527
private readonly ITraversalHelper _traversalHelper;
2628
private readonly IEnumerable<IQueryConstraintProvider> _constraintProviders;

src/JsonApiDotNetCore/Middleware/HeaderConstants.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#pragma warning disable AV1008 // Class should not be static
2+
13
namespace JsonApiDotNetCore.Middleware
24
{
35
public static class HeaderConstants

src/JsonApiDotNetCore/Queries/Expressions/IncludeChainConverter.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace JsonApiDotNetCore.Queries.Expressions
77
/// <summary>
88
/// Converts includes between tree and chain formats. Exists for backwards compatibility, subject to be removed in the future.
99
/// </summary>
10-
internal static class IncludeChainConverter
10+
internal sealed class IncludeChainConverter
1111
{
1212
/// <summary>
1313
/// Converts a tree of inclusions into a set of relationship chains.
@@ -28,7 +28,7 @@ internal static class IncludeChainConverter
2828
/// Article -> Revisions -> Author
2929
/// ]]></code>
3030
/// </example>
31-
public static IReadOnlyCollection<ResourceFieldChainExpression> GetRelationshipChains(IncludeExpression include)
31+
public IReadOnlyCollection<ResourceFieldChainExpression> GetRelationshipChains(IncludeExpression include)
3232
{
3333
ArgumentGuard.NotNull(include, nameof(include));
3434

@@ -57,7 +57,7 @@ public static IReadOnlyCollection<ResourceFieldChainExpression> GetRelationshipC
5757
/// }
5858
/// ]]></code>
5959
/// </example>
60-
public static IncludeExpression FromRelationshipChains(IReadOnlyCollection<ResourceFieldChainExpression> chains)
60+
public IncludeExpression FromRelationshipChains(IReadOnlyCollection<ResourceFieldChainExpression> chains)
6161
{
6262
ArgumentGuard.NotNull(chains, nameof(chains));
6363

src/JsonApiDotNetCore/Queries/Expressions/IncludeExpression.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ namespace JsonApiDotNetCore.Queries.Expressions
1111
[PublicAPI]
1212
public class IncludeExpression : QueryExpression
1313
{
14+
private static readonly IncludeChainConverter IncludeChainConverter = new IncludeChainConverter();
15+
1416
public static readonly IncludeExpression Empty = new IncludeExpression();
17+
1518
public IReadOnlyCollection<IncludeElementExpression> Elements { get; }
1619

1720
public IncludeExpression(IReadOnlyCollection<IncludeElementExpression> elements)

src/JsonApiDotNetCore/Queries/Internal/Parsing/IncludeParser.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ namespace JsonApiDotNetCore.Queries.Internal.Parsing
1111
[PublicAPI]
1212
public class IncludeParser : QueryExpressionParser
1313
{
14+
private static readonly IncludeChainConverter IncludeChainConverter = new IncludeChainConverter();
15+
1416
private readonly Action<RelationshipAttribute, ResourceContext, string> _validateSingleRelationshipCallback;
1517
private ResourceContext _resourceContextInScope;
1618

src/JsonApiDotNetCore/Queries/Internal/Parsing/Keywords.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using JetBrains.Annotations;
22

3+
#pragma warning disable AV1008 // Class should not be static
4+
35
namespace JsonApiDotNetCore.Queries.Internal.Parsing
46
{
57
[PublicAPI]

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ namespace JsonApiDotNetCore.Queries.Internal.QueryableBuilding
1515
[PublicAPI]
1616
public class IncludeClauseBuilder : QueryClauseBuilder<object>
1717
{
18+
private static readonly IncludeChainConverter IncludeChainConverter = new IncludeChainConverter();
19+
1820
private readonly Expression _source;
1921
private readonly ResourceContext _resourceContext;
2022
private readonly IResourceContextProvider _resourceContextProvider;

src/JsonApiDotNetCore/Repositories/EntityFrameworkCoreSupport.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using System;
22
using Microsoft.EntityFrameworkCore;
33

4+
#pragma warning disable AV1008 // Class should not be static
5+
46
namespace JsonApiDotNetCore.Repositories
57
{
68
internal static class EntityFrameworkCoreSupport

src/JsonApiDotNetCore/Serialization/Building/ResponseResourceObjectBuilder.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ namespace JsonApiDotNetCore.Serialization.Building
1515
[PublicAPI]
1616
public class ResponseResourceObjectBuilder : ResourceObjectBuilder
1717
{
18+
private static readonly IncludeChainConverter IncludeChainConverter = new IncludeChainConverter();
19+
1820
private readonly IIncludedResourceObjectBuilder _includedBuilder;
1921
private readonly IEnumerable<IQueryConstraintProvider> _constraintProviders;
2022
private readonly IResourceDefinitionAccessor _resourceDefinitionAccessor;

src/JsonApiDotNetCore/TypeHelper.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
using JsonApiDotNetCore.Resources;
88
using JsonApiDotNetCore.Resources.Annotations;
99

10+
#pragma warning disable AV1008 // Class should not be static
11+
1012
namespace JsonApiDotNetCore
1113
{
1214
internal static class TypeHelper

test/JsonApiDotNetCoreExampleTests/IntegrationTests/IdObfuscation/HexadecimalCodec.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88

99
namespace JsonApiDotNetCoreExampleTests.IntegrationTests.IdObfuscation
1010
{
11-
internal static class HexadecimalCodec
11+
internal sealed class HexadecimalCodec
1212
{
13-
public static int Decode(string value)
13+
public int Decode(string value)
1414
{
1515
if (value == null)
1616
{
@@ -45,7 +45,7 @@ private static string FromHexString(string hexString)
4545
return new string(chars);
4646
}
4747

48-
public static string Encode(int value)
48+
public string Encode(int value)
4949
{
5050
if (value == 0)
5151
{

test/JsonApiDotNetCoreExampleTests/IntegrationTests/IdObfuscation/IdObfuscationTests.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ await _testContext.RunOnDatabaseAsync(async dbContext =>
5959
await dbContext.SaveChangesAsync();
6060
});
6161

62-
string route = $"/bankAccounts?filter=any(id,'{accounts[1].StringId}','{HexadecimalCodec.Encode(99999999)}')";
62+
var codec = new HexadecimalCodec();
63+
string route = $"/bankAccounts?filter=any(id,'{accounts[1].StringId}','{codec.Encode(99999999)}')";
6364

6465
// Act
6566
(HttpResponseMessage httpResponse, Document responseDocument) = await _testContext.ExecuteGetAsync<Document>(route);
@@ -244,7 +245,8 @@ await _testContext.RunOnDatabaseAsync(async dbContext =>
244245
responseDocument.SingleData.Attributes["ownerName"].Should().Be(newCard.OwnerName);
245246
responseDocument.SingleData.Attributes["pinCode"].Should().Be(newCard.PinCode);
246247

247-
int newCardId = HexadecimalCodec.Decode(responseDocument.SingleData.Id);
248+
var codec = new HexadecimalCodec();
249+
int newCardId = codec.Decode(responseDocument.SingleData.Id);
248250

249251
await _testContext.RunOnDatabaseAsync(async dbContext =>
250252
{
@@ -448,7 +450,8 @@ await _testContext.RunOnDatabaseAsync(async dbContext =>
448450
public async Task Cannot_delete_missing_resource()
449451
{
450452
// Arrange
451-
string stringId = HexadecimalCodec.Encode(99999999);
453+
var codec = new HexadecimalCodec();
454+
string stringId = codec.Encode(99999999);
452455

453456
string route = "/bankAccounts/" + stringId;
454457

0 commit comments

Comments
 (0)