Skip to content

Commit 36b9961

Browse files
author
Bart Koelman
committed
AV1130: Use collection interface as return type
1 parent 3a5d0ee commit 36b9961

39 files changed

+191
-203
lines changed

CodingGuidelines.ruleset

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<Rule Id="AV1506" Action="Warning" />
77
<Rule Id="AV1507" Action="Warning" />
88
<Rule Id="AV1008" Action="Warning" />
9-
<Rule Id="AV1130" Action="Info" />
9+
<Rule Id="AV1130" Action="Warning" />
1010
<Rule Id="AV1135" Action="Info" />
1111
<Rule Id="AV1536" Action="None" />
1212
<Rule Id="AV1537" Action="None" />
@@ -28,4 +28,4 @@
2828
<Rule Id="AV2310" Action="Info" />
2929
<Rule Id="AV2318" Action="Info" />
3030
</Rules>
31-
</RuleSet>
31+
</RuleSet>

src/JsonApiDotNetCore/ArrayFactory.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#pragma warning disable AV1008 // Class should not be static
2+
#pragma warning disable AV1130 // Return type in method signature should be a collection interface instead of a concrete type
23

34
namespace JsonApiDotNetCore
45
{

src/JsonApiDotNetCore/CollectionExtensions.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using System.Collections.Generic;
23
using System.Linq;
34
using JetBrains.Annotations;
@@ -17,5 +18,21 @@ public static bool IsNullOrEmpty<T>(this IEnumerable<T> source)
1718

1819
return !source.Any();
1920
}
21+
22+
public static int FindIndex<T>(this IList<T> source, Predicate<T> match)
23+
{
24+
ArgumentGuard.NotNull(source, nameof(source));
25+
ArgumentGuard.NotNull(match, nameof(match));
26+
27+
for (int index = 0; index < source.Count; index++)
28+
{
29+
if (match(source[index]))
30+
{
31+
return index;
32+
}
33+
}
34+
35+
return -1;
36+
}
2037
}
2138
}

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

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ public sealed class DiffableResourceHashSet<TResource> : ResourceHashSet<TResour
1717
{
1818
private readonly HashSet<TResource> _databaseValues;
1919
private readonly bool _databaseValuesLoaded;
20-
private readonly Dictionary<PropertyInfo, HashSet<TResource>> _updatedAttributes;
20+
private readonly IDictionary<PropertyInfo, HashSet<TResource>> _updatedAttributes;
2121

2222
public DiffableResourceHashSet(HashSet<TResource> requestResources, HashSet<TResource> databaseResources,
23-
Dictionary<RelationshipAttribute, HashSet<TResource>> relationships, Dictionary<PropertyInfo, HashSet<TResource>> updatedAttributes)
23+
IDictionary<RelationshipAttribute, HashSet<TResource>> relationships, IDictionary<PropertyInfo, HashSet<TResource>> updatedAttributes)
2424
: base(requestResources, relationships)
2525
{
2626
_databaseValues = databaseResources;
@@ -32,12 +32,10 @@ public DiffableResourceHashSet(HashSet<TResource> requestResources, HashSet<TRes
3232
/// Used internally by the ResourceHookExecutor to make live a bit easier with generics
3333
/// </summary>
3434
internal DiffableResourceHashSet(IEnumerable requestResources, IEnumerable databaseResources,
35-
Dictionary<RelationshipAttribute, IEnumerable> relationships, ITargetedFields targetedFields)
35+
IDictionary<RelationshipAttribute, IEnumerable> relationships, ITargetedFields targetedFields)
3636
: this((HashSet<TResource>)requestResources, (HashSet<TResource>)databaseResources,
37-
TypeHelper.ConvertRelationshipDictionary<TResource>(relationships),
38-
targetedFields.Attributes == null
39-
? null
40-
: TypeHelper.ConvertAttributeDictionary(targetedFields.Attributes, (HashSet<TResource>)requestResources))
37+
relationships.ToDictionary(pair => pair.Key, pair => (HashSet<TResource>)pair.Value),
38+
targetedFields.Attributes?.ToDictionary(attr => attr.Property, _ => (HashSet<TResource>)requestResources))
4139
{
4240
}
4341

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ private IResourceReadRepository<TResource, TId> GetRepository<TResource, TId>()
202202
return _genericProcessorFactory.Get<IResourceReadRepository<TResource, TId>>(typeof(IResourceReadRepository<,>), typeof(TResource), typeof(TId));
203203
}
204204

205-
public Dictionary<RelationshipAttribute, IEnumerable> LoadImplicitlyAffected(Dictionary<RelationshipAttribute, IEnumerable> leftResourcesByRelation,
205+
public IDictionary<RelationshipAttribute, IEnumerable> LoadImplicitlyAffected(IDictionary<RelationshipAttribute, IEnumerable> leftResourcesByRelation,
206206
IEnumerable existingRightResources = null)
207207
{
208208
List<IIdentifiable> existingRightResourceList = existingRightResources?.Cast<IIdentifiable>().ToList();

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@ public interface IByAffectedRelationships<TRightResource> : IRelationshipGetters
1515
/// <summary>
1616
/// Gets a dictionary of affected resources grouped by affected relationships.
1717
/// </summary>
18-
Dictionary<RelationshipAttribute, HashSet<TRightResource>> AffectedRelationships { get; }
18+
IDictionary<RelationshipAttribute, HashSet<TRightResource>> AffectedRelationships { get; }
1919
}
2020
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ IResourceHookContainer<TResource> GetResourceHookContainer<TResource>(ResourceHo
3131
/// <returns>
3232
/// The implicitly affected resources by relationship
3333
/// </returns>
34-
Dictionary<RelationshipAttribute, IEnumerable> LoadImplicitlyAffected(Dictionary<RelationshipAttribute, IEnumerable> leftResourcesByRelation,
34+
IDictionary<RelationshipAttribute, IEnumerable> LoadImplicitlyAffected(IDictionary<RelationshipAttribute, IEnumerable> leftResourcesByRelation,
3535
IEnumerable existingRightResources = null);
3636

3737
/// <summary>

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,19 @@ public interface IRelationshipGetters<TLeftResource>
1717
/// <summary>
1818
/// Gets a dictionary of all resources that have an affected relationship to type <typeparamref name="TLeftResource" />
1919
/// </summary>
20-
Dictionary<RelationshipAttribute, HashSet<TLeftResource>> GetByRelationship<TRightResource>()
20+
IDictionary<RelationshipAttribute, HashSet<TLeftResource>> GetByRelationship<TRightResource>()
2121
where TRightResource : class, IIdentifiable;
2222

2323
/// <summary>
2424
/// Gets a dictionary of all resources that have an affected relationship to type <paramref name="resourceType" />
2525
/// </summary>
26-
Dictionary<RelationshipAttribute, HashSet<TLeftResource>> GetByRelationship(Type resourceType);
26+
IDictionary<RelationshipAttribute, HashSet<TLeftResource>> GetByRelationship(Type resourceType);
2727

2828
/// <summary>
2929
/// Gets a collection of all the resources for the property within <paramref name="navigationAction" /> has been affected by the request
3030
/// </summary>
31+
#pragma warning disable AV1130 // Return type in method signature should be a collection interface instead of a concrete type
3132
HashSet<TLeftResource> GetAffected(Expression<Func<TLeftResource, object>> navigationAction);
33+
#pragma warning restore AV1130 // Return type in method signature should be a collection interface instead of a concrete type
3234
}
3335
}

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,28 +24,28 @@ public class RelationshipsDictionary<TResource> : Dictionary<RelationshipAttribu
2424
/// <param name="relationships">
2525
/// Relationships.
2626
/// </param>
27-
public RelationshipsDictionary(Dictionary<RelationshipAttribute, HashSet<TResource>> relationships)
27+
public RelationshipsDictionary(IDictionary<RelationshipAttribute, HashSet<TResource>> relationships)
2828
: base(relationships)
2929
{
3030
}
3131

3232
/// <summary>
3333
/// Used internally by the ResourceHookExecutor to make life a bit easier with generics
3434
/// </summary>
35-
internal RelationshipsDictionary(Dictionary<RelationshipAttribute, IEnumerable> relationships)
36-
: this(TypeHelper.ConvertRelationshipDictionary<TResource>(relationships))
35+
internal RelationshipsDictionary(IDictionary<RelationshipAttribute, IEnumerable> relationships)
36+
: this(relationships.ToDictionary(pair => pair.Key, pair => (HashSet<TResource>)pair.Value))
3737
{
3838
}
3939

4040
/// <inheritdoc />
41-
public Dictionary<RelationshipAttribute, HashSet<TResource>> GetByRelationship<TRelatedResource>()
41+
public IDictionary<RelationshipAttribute, HashSet<TResource>> GetByRelationship<TRelatedResource>()
4242
where TRelatedResource : class, IIdentifiable
4343
{
4444
return GetByRelationship(typeof(TRelatedResource));
4545
}
4646

4747
/// <inheritdoc />
48-
public Dictionary<RelationshipAttribute, HashSet<TResource>> GetByRelationship(Type resourceType)
48+
public IDictionary<RelationshipAttribute, HashSet<TResource>> GetByRelationship(Type resourceType)
4949
{
5050
return this.Where(pair => pair.Key.RightType == resourceType).ToDictionary(pair => pair.Key, pair => pair.Value);
5151
}

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections;
33
using System.Collections.Generic;
4+
using System.Linq;
45
using System.Linq.Expressions;
56
using JetBrains.Annotations;
67
using JsonApiDotNetCore.Resources;
@@ -19,9 +20,9 @@ public class ResourceHashSet<TResource> : HashSet<TResource>, IResourceHashSet<T
1920
private readonly RelationshipsDictionary<TResource> _relationships;
2021

2122
/// <inheritdoc />
22-
public Dictionary<RelationshipAttribute, HashSet<TResource>> AffectedRelationships => _relationships;
23+
public IDictionary<RelationshipAttribute, HashSet<TResource>> AffectedRelationships => _relationships;
2324

24-
public ResourceHashSet(HashSet<TResource> resources, Dictionary<RelationshipAttribute, HashSet<TResource>> relationships)
25+
public ResourceHashSet(HashSet<TResource> resources, IDictionary<RelationshipAttribute, HashSet<TResource>> relationships)
2526
: base(resources)
2627
{
2728
_relationships = new RelationshipsDictionary<TResource>(relationships);
@@ -30,19 +31,19 @@ public ResourceHashSet(HashSet<TResource> resources, Dictionary<RelationshipAttr
3031
/// <summary>
3132
/// Used internally by the ResourceHookExecutor to make live a bit easier with generics
3233
/// </summary>
33-
internal ResourceHashSet(IEnumerable resources, Dictionary<RelationshipAttribute, IEnumerable> relationships)
34-
: this((HashSet<TResource>)resources, TypeHelper.ConvertRelationshipDictionary<TResource>(relationships))
34+
internal ResourceHashSet(IEnumerable resources, IDictionary<RelationshipAttribute, IEnumerable> relationships)
35+
: this((HashSet<TResource>)resources, relationships.ToDictionary(pair => pair.Key, pair => (HashSet<TResource>)pair.Value))
3536
{
3637
}
3738

3839
/// <inheritdoc />
39-
public Dictionary<RelationshipAttribute, HashSet<TResource>> GetByRelationship(Type resourceType)
40+
public IDictionary<RelationshipAttribute, HashSet<TResource>> GetByRelationship(Type resourceType)
4041
{
4142
return _relationships.GetByRelationship(resourceType);
4243
}
4344

4445
/// <inheritdoc />
45-
public Dictionary<RelationshipAttribute, HashSet<TResource>> GetByRelationship<TRightResource>()
46+
public IDictionary<RelationshipAttribute, HashSet<TResource>> GetByRelationship<TRightResource>()
4647
where TRightResource : class, IIdentifiable
4748
{
4849
return GetByRelationship(typeof(TRightResource));

0 commit comments

Comments
 (0)