Skip to content

Commit 23ed225

Browse files
Code refactoring
1 parent aceb54f commit 23ed225

29 files changed

Lines changed: 661 additions & 686 deletions

AutoIndexCache.Benchmarks/Benchmarks.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@
22
using BenchmarkDotNet.Attributes;
33
using BenchmarkDotNet.Configs;
44

5-
// ReSharper disable LocalVariableHidesMember
6-
// ReSharper disable InconsistentNaming
7-
85
namespace AutoIndexCache.Benchmarks;
96

107
[MemoryDiagnoser]
@@ -347,8 +344,6 @@ public void Reset_AutoIndexCache()
347344
private IUniqueIndex<User, String> autoIndexCache_UniqueIndex_UserByUserName = null!;
348345
private Dictionary<Int64, User> dictionary_UserById = null!;
349346
private Dictionary<String, User> dictionary_UserByUserName = null!;
350-
351347
private Dictionary<Int64, List<User>> dictionary_UsersByGroupId = null!;
352-
353348
private User[] users = null!;
354349
}

AutoIndexCache.Benchmarks/BenchmarksOrderer.cs

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,32 +12,28 @@ public class BenchmarksOrderer : IOrderer
1212
public Boolean SeparateLogicalGroups => true;
1313

1414
/// <inheritdoc />
15-
public IEnumerable<BenchmarkCase> GetExecutionOrder(ImmutableArray<BenchmarkCase> benchmarksCase, IEnumerable<BenchmarkLogicalGroupRule>? order = null)
16-
{
17-
return benchmarksCase.OrderBy(a => a.Descriptor.WorkloadMethod.DeclaringType!.GetMethods().ToList().IndexOf(a.Descriptor.WorkloadMethod));
18-
}
15+
public IEnumerable<BenchmarkCase> GetExecutionOrder(
16+
ImmutableArray<BenchmarkCase> benchmarksCase,
17+
IEnumerable<BenchmarkLogicalGroupRule>? order = null
18+
) =>
19+
benchmarksCase.OrderBy(a => a.Descriptor.WorkloadMethod.DeclaringType!.GetMethods().ToList().IndexOf(a.Descriptor.WorkloadMethod));
1920

2021
/// <inheritdoc />
21-
public String? GetHighlightGroupKey(BenchmarkCase benchmarkCase)
22-
{
23-
return benchmarkCase.Descriptor.Categories.FirstOrDefault();
24-
}
22+
public String? GetHighlightGroupKey(BenchmarkCase benchmarkCase) =>
23+
benchmarkCase.Descriptor.Categories.FirstOrDefault();
2524

2625
/// <inheritdoc />
27-
public String? GetLogicalGroupKey(ImmutableArray<BenchmarkCase> allBenchmarksCases, BenchmarkCase benchmarkCase)
28-
{
29-
return benchmarkCase.Descriptor.Categories.FirstOrDefault();
30-
}
26+
public String? GetLogicalGroupKey(ImmutableArray<BenchmarkCase> allBenchmarksCases, BenchmarkCase benchmarkCase) =>
27+
benchmarkCase.Descriptor.Categories.FirstOrDefault();
3128

3229
/// <inheritdoc />
33-
public IEnumerable<IGrouping<String, BenchmarkCase>> GetLogicalGroupOrder(IEnumerable<IGrouping<String, BenchmarkCase>> logicalGroups, IEnumerable<BenchmarkLogicalGroupRule>? order = null)
34-
{
35-
return logicalGroups.OrderBy(it => it.Key);
36-
}
30+
public IEnumerable<IGrouping<String, BenchmarkCase>> GetLogicalGroupOrder(
31+
IEnumerable<IGrouping<String, BenchmarkCase>> logicalGroups,
32+
IEnumerable<BenchmarkLogicalGroupRule>? order = null
33+
) =>
34+
logicalGroups.OrderBy(it => it.Key);
3735

3836
/// <inheritdoc />
39-
public IEnumerable<BenchmarkCase> GetSummaryOrder(ImmutableArray<BenchmarkCase> benchmarksCases, Summary summary)
40-
{
41-
return benchmarksCases.OrderBy(a => a.Descriptor.WorkloadMethod.DeclaringType!.GetMethods().ToList().IndexOf(a.Descriptor.WorkloadMethod));
42-
}
37+
public IEnumerable<BenchmarkCase> GetSummaryOrder(ImmutableArray<BenchmarkCase> benchmarksCases, Summary summary) =>
38+
benchmarksCases.OrderBy(a => a.Descriptor.WorkloadMethod.DeclaringType!.GetMethods().ToList().IndexOf(a.Descriptor.WorkloadMethod));
4339
}

AutoIndexCache.Tests/AutoIndexCacheTests.cs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using AutoIndexCache.Tests.TestData;
1+
using AutoIndexCache.Exceptions;
2+
using AutoIndexCache.Tests.TestData;
23
using FakeItEasy;
34
using FluentAssertions;
45
using NUnit.Framework;
@@ -20,19 +21,15 @@ public void AccessFromMultipleThreadsShouldCauseInvocationOfItemsLoaderOnlyOnce(
2021
User[] LoadUsers()
2122
{
2223
Interlocked.Increment(ref loadUsersInvocations);
23-
#pragma warning disable S2925
2424
Thread.Sleep(random.Next(10, 100));
25-
#pragma warning restore S2925
2625
return [];
2726
}
2827

2928
cache.SetItemsLoader(LoadUsers);
3029

3130
void ThreadBody()
3231
{
33-
#pragma warning disable S2925
3432
Thread.Sleep(random.Next(10, 100));
35-
#pragma warning restore S2925
3633
cache.Items<User>().GetAllItems();
3734
}
3835

@@ -66,9 +63,7 @@ public void AccessFromOtherThreadsShouldBeBlockedUntilItemsLoaderHasCompletedLoa
6663

6764
User[] LoadUsers()
6865
{
69-
#pragma warning disable S2925
7066
Thread.Sleep(waitTime);
71-
#pragma warning restore S2925
7267
return [];
7368
}
7469

@@ -255,4 +250,4 @@ public void SetItemsLoader_ShouldSetLoader()
255250
A.CallTo(() => userLoader())
256251
.MustHaveHappened(1, Times.Exactly);
257252
}
258-
}
253+
}

AutoIndexCache.Tests/ItemsListTests.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Data;
2+
using AutoIndexCache.Exceptions;
23
using AutoIndexCache.Tests.TestData;
34
using FakeItEasy;
45
using FluentAssertions;
@@ -126,7 +127,7 @@ public void ForceLoadItems_ItemsLoaderReturnsNull_ShouldThrow()
126127
cache.Invoking(c => c.Items<User>().ForceLoadItems())
127128
.Should()
128129
.Throw<ItemsLoaderReturnedNullException>()
129-
.WithMessage("Could not load the cache items of the type 'AutoIndexCache.Tests.TestData.User'. The cache items loader for that cache item type returned a null value. It must return a list of cache items instead.");
130+
.WithMessage("Could not load the cache items of the type 'AutoIndexCache.Tests.TestData.User'. The cache items loader for that cache item type returned a null reference. It must return a list of cache items instead.");
130131
}
131132

132133
[Test]
@@ -298,7 +299,7 @@ public void GetAllItems_ItemsLoaderReturnsNull_ShouldThrow()
298299
cache.Invoking(c => c.Items<User>().GetAllItems())
299300
.Should()
300301
.Throw<ItemsLoaderReturnedNullException>()
301-
.WithMessage("Could not load the cache items of the type 'AutoIndexCache.Tests.TestData.User'. The cache items loader for that cache item type returned a null value. It must return a list of cache items instead.");
302+
.WithMessage("Could not load the cache items of the type 'AutoIndexCache.Tests.TestData.User'. The cache items loader for that cache item type returned a null reference. It must return a list of cache items instead.");
302303
}
303304

304305
[Test]
@@ -478,4 +479,4 @@ public void UniqueIndex_ShouldReturnUniqueIndex()
478479
.And
479480
.BeOfType<UniqueIndex<User, String>>();
480481
}
481-
}
482+
}

AutoIndexCache.Tests/NonUniqueIndexTests.cs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
1-
using AutoIndexCache.Tests.TestData;
1+
using AutoIndexCache.Exceptions;
2+
using AutoIndexCache.Tests.TestData;
23
using FakeItEasy;
34
using FluentAssertions;
45
using NUnit.Framework;
56

6-
// ReSharper disable ReturnValueOfPureMethodIsNotUsed
7-
#pragma warning disable CA1806
8-
#pragma warning disable CS8621
9-
#pragma warning disable CS8714 // The type cannot be used as type parameter in the generic type or method. Nullability of type argument doesn't match 'notnull' constraint.
10-
117
namespace AutoIndexCache.Tests;
128

139
[TestFixture]
@@ -544,4 +540,4 @@ public void GetKeys_ShouldReturnAllKeys()
544540
.Should()
545541
.BeEquivalentTo(this.TestUsers.Select(a => new { a.GroupId, a.IsActive }).Distinct());
546542
}
547-
}
543+
}

AutoIndexCache.Tests/TestData/Group.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ public class Group(Int64 id, String category)
44
{
55
public String Category = category;
66
public Int64 Id = id;
7-
}
7+
}

AutoIndexCache.Tests/TestData/User.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ public class User(Int64 id, String userName, Int64 groupId, Boolean isActive)
88
public Object? NullableObject;
99
public Nullable<Int32> NullableValue;
1010
public String UserName = userName;
11-
}
11+
}

AutoIndexCache.Tests/TestsBase.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
using AutoIndexCache.Tests.TestData;
33
using NUnit.Framework;
44

5-
#pragma warning disable CS8618
6-
75
namespace AutoIndexCache.Tests;
86

97
public abstract class TestsBase
@@ -34,7 +32,7 @@ public void SetUp()
3432
/// <summary>
3533
/// Executes <paramref name="action" /> and measures the time it took to execute it.
3634
/// </summary>
37-
/// <param name="action">The delegate to measure the execution time of.</param>
35+
/// <param name="action">The function to measure the execution time of.</param>
3836
/// <returns>The time it took to execute <paramref name="action" />.</returns>
3937
protected static TimeSpan ExecutionTimeOf(Action action)
4038
{
@@ -45,6 +43,6 @@ protected static TimeSpan ExecutionTimeOf(Action action)
4543
return stopwatch.Elapsed;
4644
}
4745

48-
protected User[] TestUsers;
49-
protected User FirstActiveTestUser;
50-
}
46+
protected User[] TestUsers = null!;
47+
protected User FirstActiveTestUser = null!;
48+
}

AutoIndexCache.Tests/UniqueIndexTests.cs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
1-
using AutoIndexCache.Tests.TestData;
1+
using AutoIndexCache.Exceptions;
2+
using AutoIndexCache.Tests.TestData;
23
using FakeItEasy;
34
using FluentAssertions;
45
using NUnit.Framework;
56

6-
// ReSharper disable ReturnValueOfPureMethodIsNotUsed
7-
#pragma warning disable CA1806
8-
#pragma warning disable CS8621
9-
#pragma warning disable CS8714 // The type cannot be used as type parameter in the generic type or method. Nullability of type argument doesn't match 'notnull' constraint.
10-
117
namespace AutoIndexCache.Tests;
128

139
[TestFixture]
@@ -629,4 +625,4 @@ public void GetKeys_ShouldReturnAllKeys()
629625
.Should()
630626
.BeEquivalentTo(this.TestUsers.Select(a => a.Id));
631627
}
632-
}
628+
}

AutoIndexCache/AutoIndexCache.cs

Lines changed: 3 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,81 +1,8 @@
1-
using System;
2-
using System.Runtime.CompilerServices;
3-
using ItemType = System.Type;
4-
5-
[assembly: InternalsVisibleTo("AutoIndexCache.Tests")]
1+
[assembly: InternalsVisibleTo("AutoIndexCache.Tests")]
62
[assembly: InternalsVisibleTo("AutoIndexCache.Benchmarks")]
73

84
namespace AutoIndexCache;
95

10-
/// <summary>
11-
/// Represents a thread-safe, lazy loading cache that automatically indexes cached items.
12-
/// </summary>
13-
public interface IAutoIndexCache
14-
{
15-
/// <summary>
16-
/// Gets the list of cached items of the type <typeparamref name="TItem" /> from this cache.
17-
/// </summary>
18-
/// <typeparam name="TItem">The type of cache items to get.</typeparam>
19-
/// <returns>An instance of <see cref="IItemsList{TItem}" /> that allows to access the cached items of the type <typeparamref name="TItem" />.</returns>
20-
/// <exception cref="MissingItemsLoaderException">No cache items loader has been set for the cache item type <typeparamref name="TItem" /> yet.</exception>
21-
/// <example>
22-
/// <code>
23-
/// <![CDATA[
24-
/// var cache = new AutoIndexCache();
25-
/// cache.SetItemsLoader<User>(() => this.LoadUsers());
26-
/// var users = cache.Items<User>().GetAllItems();
27-
/// ]]>
28-
/// </code>
29-
/// </example>
30-
IItemsList<TItem> Items<TItem>()
31-
where TItem : class;
32-
33-
/// <summary>
34-
/// Sets the delegate that loads the cache items of the type <typeparamref name="TItem" /> when cache items of that type are requested from the cache.
35-
/// If a cache items loader has already been set for the type <typeparamref name="TItem" /> on this instance, the old cache items loader is replaced and the corresponding <see cref="ItemsList{TItem}" /> is reset, so the new cache items loader will be used to load the cache items the next time the cache items are requested from the cache.
36-
/// </summary>
37-
/// <typeparam name="TItem">The type of cache items the loader loads.</typeparam>
38-
/// <param name="itemsLoader">The delegate that loads the cache items of the type <typeparamref name="TItem" />.</param>
39-
/// <exception cref="ArgumentNullException"><paramref name="itemsLoader" /> is null.</exception>
40-
/// <remarks>
41-
/// The specified cache items loader delegate may not return null.
42-
///
43-
/// The specified cache items loader delegate may not access the cached items of the type <typeparamref name="TItem" /> inside its method body.
44-
/// For example, the cache items loader for the cache item type "User" may not call the following methods inside its method body:
45-
/// <code>
46-
/// <![CDATA[
47-
/// - IItemsList<User>.GetAllItems
48-
/// - IItemsList<User>.Reset
49-
/// - IItemsList<User>.NonUniqueIndex<TKey>
50-
/// - IItemsList<User>.UniqueIndex<TKey>
51-
/// ]]>
52-
/// </code>
53-
/// However, the cache items loader is allowed to access cache items of other types.
54-
/// For example, the cache items loader for the cache item type "User" is allowed call the following methods inside its method body:
55-
/// <code>
56-
/// <![CDATA[
57-
/// - IItemsList<Group>.GetAllItems
58-
/// - IItemsList<Group>.Reset
59-
/// - IItemsList<Group>.NonUniqueIndex<TKey>
60-
/// - IItemsList<Group>.UniqueIndex<TKey>
61-
/// ]]>
62-
/// </code>
63-
/// However, cyclic dependencies are not allowed.
64-
/// For example, when the cache items loader of the cache item type "User" accesses cache items of the type "Group" and the cache items loader of the type "Group" accesses cache items of the type "User" (meaning User > Group > User) this is not allowed.
65-
/// </remarks>
66-
/// <example>
67-
/// <code>
68-
/// <![CDATA[
69-
/// var cache = new AutoIndexCache();
70-
/// cache.SetItemsLoader<User>(() => this.LoadUsers());
71-
/// var users = cache.Items<User>().GetAllItems();
72-
/// ]]>
73-
/// </code>
74-
/// </example>
75-
void SetItemsLoader<TItem>(Func<TItem[]> itemsLoader)
76-
where TItem : class;
77-
}
78-
796
/// <summary>
807
/// A thread-safe, lazy loading cache that automatically indexes cached items.
818
/// </summary>
@@ -118,5 +45,5 @@ public void SetItemsLoader<TItem>(Func<TItem[]> itemsLoader)
11845
);
11946
}
12047

121-
private readonly CopyOnWriteDictionary<ItemType, IItemsList> itemsLists = new();
122-
}
48+
private readonly CopyOnWriteDictionary<Type, IItemsList> itemsLists = new();
49+
}

0 commit comments

Comments
 (0)