Skip to content

Commit 445f19f

Browse files
Merge pull request #367 from TNG/chore/reduce-code-duplication
Reduce Code Duplication in Fluent Syntax Conditions
2 parents 533d9b0 + 9688fd8 commit 445f19f

20 files changed

+588
-1453
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System.Collections.Generic;
2+
3+
namespace ArchUnitNET.Domain
4+
{
5+
/// <summary>
6+
/// An interface for object providers that can provide a size independent of the used architecture.
7+
/// </summary>
8+
/// <typeparam name="T"></typeparam>
9+
public interface ISizedObjectProvider<out T> : IObjectProvider<T>
10+
{
11+
int Count { get; }
12+
}
13+
}

ArchUnitNET/Fluent/Conditions/RelationCondition.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Linq;
34
using ArchUnitNET.Domain;
45

56
namespace ArchUnitNET.Fluent.Conditions
@@ -8,10 +9,10 @@ public class RelationCondition<TRuleType, TRelatedType> : IHasDescription
89
where TRuleType : ICanBeAnalyzed
910
where TRelatedType : ICanBeAnalyzed
1011
{
11-
private readonly Func<IEnumerable<TRelatedType>, ICondition<TRuleType>> _relation;
12+
private readonly Func<IObjectProvider<TRelatedType>, ICondition<TRuleType>> _relation;
1213

1314
public RelationCondition(
14-
Func<IEnumerable<TRelatedType>, ICondition<TRuleType>> relation,
15+
Func<IObjectProvider<TRelatedType>, ICondition<TRuleType>> relation,
1516
string description,
1617
string failDescription
1718
)
@@ -25,9 +26,9 @@ string failDescription
2526

2627
public string Description { get; }
2728

28-
public ICondition<TRuleType> GetCondition(IEnumerable<TRelatedType> objectProvider)
29+
public ICondition<TRuleType> GetCondition(IEnumerable<TRelatedType> objects)
2930
{
30-
return _relation(objectProvider);
31+
return _relation(new ListObjectProvider<TRelatedType>(objects.ToList()));
3132
}
3233

3334
private bool Equals(RelationCondition<TRuleType, TRelatedType> other)
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
using ArchUnitNET.Domain;
4+
5+
namespace ArchUnitNET.Fluent
6+
{
7+
public class ListObjectProvider<T> : ISizedObjectProvider<T>
8+
where T : ICanBeAnalyzed
9+
{
10+
private readonly IEnumerable<T> _objects;
11+
12+
public ListObjectProvider(List<T> objects)
13+
{
14+
_objects = objects;
15+
Description = string.Join(" or ", objects.Select(obj => $"\"{obj.FullName}\""));
16+
}
17+
18+
public string Description { get; }
19+
20+
public int Count => _objects.Count();
21+
22+
public IEnumerable<T> GetObjects(Architecture architecture)
23+
{
24+
return _objects;
25+
}
26+
27+
private bool Equals(ListObjectProvider<T> other)
28+
{
29+
return string.Equals(Description, other.Description);
30+
}
31+
32+
public override bool Equals(object obj)
33+
{
34+
if (ReferenceEquals(null, obj))
35+
{
36+
return false;
37+
}
38+
39+
if (ReferenceEquals(this, obj))
40+
{
41+
return true;
42+
}
43+
44+
return obj.GetType() == GetType() && Equals((ListObjectProvider<T>)obj);
45+
}
46+
47+
public override int GetHashCode()
48+
{
49+
return Description != null ? Description.GetHashCode() : 0;
50+
}
51+
}
52+
}

0 commit comments

Comments
 (0)