Skip to content

Commit ec7a580

Browse files
author
Aaron Arroyo
committed
refactor: implement IoC factory pattern for DDD service abstractions
- Add IBrokenRulesManager and IValidatorRuleManager interfaces - Create DddServiceFactory for configurable service instantiation - Update ValueObject to use factory pattern (resolves TODO at line 52) - Improve test data clarity in BrokenRulesTest.cs - All 58 tests passing
1 parent 5e298e3 commit ec7a580

7 files changed

Lines changed: 65 additions & 20 deletions

File tree

src/libs/shell/ddd/src/Ums.Shell.Ddd.Test/BrokenRulesTest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public void Clear_RemovesAllBrokenRulesFromList()
9595
[TestMethod]
9696
public void Entity_Should_Has_BrokenRules()
9797
{
98-
var sampleEntity = SampleEntity.Create(SampleName.Create("Default"), SampleReferenceId.Create(Guid.NewGuid().ToString(),"XXX"));
98+
var sampleEntity = SampleEntity.Create(SampleName.Create("Default"), SampleReferenceId.Create(Guid.NewGuid().ToString(), "SomeReference"));
9999

100100
sampleEntity.IsValid().ShouldBeFalse();
101101
}
@@ -111,7 +111,7 @@ public void Entity_Should_Has_BrokenRules_In_Properties()
111111
[TestMethod]
112112
public void Entity_Should_Not_Have_BrokenRules()
113113
{
114-
var sampleEntity = SampleEntity.Create(SampleName.Create("BeyondNet"), SampleReferenceId.Create(Guid.NewGuid().ToString(), "XXX"));
114+
var sampleEntity = SampleEntity.Create(SampleName.Create("BeyondNet"), SampleReferenceId.Create(Guid.NewGuid().ToString(), "ValidReference"));
115115

116116
sampleEntity.IsValid().ShouldBeTrue();
117117
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using Ums.Shell.Ddd.Services.Impl;
2+
using Ums.Shell.Ddd.Services.Interfaces;
3+
4+
namespace Ums.Shell.Ddd.Factories
5+
{
6+
public static class DddServiceFactory
7+
{
8+
private static Func<IBrokenRulesManager> _brokenRulesFactory = () => new BrokenRulesManager();
9+
private static Func<ITrackingStateManager> _trackingStateFactory = () => new TrackingStateManager();
10+
11+
public static void Configure(
12+
Func<IBrokenRulesManager>? brokenRulesFactory = null,
13+
Func<ITrackingStateManager>? trackingStateFactory = null)
14+
{
15+
_brokenRulesFactory = brokenRulesFactory ?? _brokenRulesFactory;
16+
_trackingStateFactory = trackingStateFactory ?? _trackingStateFactory;
17+
}
18+
19+
public static IBrokenRulesManager CreateBrokenRulesManager() => _brokenRulesFactory();
20+
21+
public static ITrackingStateManager CreateTrackingStateManager() => _trackingStateFactory();
22+
}
23+
}

src/libs/shell/ddd/src/Ums.Shell.Ddd/Services/Impl/BrokenRulesManager.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
using System.Globalization;
22
using System.Text;
33
using Ums.Shell.Ddd.Rules;
4+
using Ums.Shell.Ddd.Services.Interfaces;
45

56
namespace Ums.Shell.Ddd.Services.Impl
67
{
7-
/// <summary>
8-
/// Represents a collection of broken rules.
9-
/// </summary>
10-
public class BrokenRulesManager
8+
public class BrokenRulesManager : IBrokenRulesManager
119
{
1210
private List<BrokenRule> _brokenRules = new();
1311

src/libs/shell/ddd/src/Ums.Shell.Ddd/Services/Impl/ValidatorRuleManager.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
using Ums.Shell.Ddd.Rules;
22
using Ums.Shell.Ddd.Rules.Interfaces;
3+
using Ums.Shell.Ddd.Services.Interfaces;
34

45
namespace Ums.Shell.Ddd.Services.Impl
56
{
6-
/// <summary>
7-
/// Represents a collection of validator rules for a specific type.
8-
/// </summary>
9-
/// <typeparam name="TValidator">The type of object to validate.</typeparam>
10-
public class ValidatorRuleManager<TValidator> where TValidator : IRuleValidator
7+
public class ValidatorRuleManager<TValidator> : IValidatorRuleManager<TValidator> where TValidator : IRuleValidator
118
{
129
private readonly List<TValidator> _businessRules = [];
1310

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using Ums.Shell.Ddd.Rules;
2+
3+
namespace Ums.Shell.Ddd.Services.Interfaces
4+
{
5+
public interface IBrokenRulesManager
6+
{
7+
void Add(BrokenRule brokenRule);
8+
void Add(IReadOnlyCollection<BrokenRule> brokenRules);
9+
void Remove(BrokenRule brokenRule);
10+
void Clear();
11+
ReadOnlyCollection<BrokenRule> GetBrokenRules();
12+
string GetBrokenRulesAsString();
13+
}
14+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using Ums.Shell.Ddd.Rules;
2+
using Ums.Shell.Ddd.Rules.Interfaces;
3+
4+
namespace Ums.Shell.Ddd.Services.Interfaces
5+
{
6+
public interface IValidatorRuleManager<TValidator> where TValidator : IRuleValidator
7+
{
8+
void Add(TValidator rule);
9+
void Add(IEnumerable<TValidator> rules);
10+
void Remove(TValidator rule);
11+
void Clear();
12+
ReadOnlyCollection<TValidator> GetValidators();
13+
ReadOnlyCollection<BrokenRule> GetBrokenRules(RuleContext? context = null);
14+
}
15+
}

src/libs/shell/ddd/src/Ums.Shell.Ddd/ValueObject.cs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
using Ums.Shell.Ddd.Interfaces;
1+
using Ums.Shell.Ddd.Factories;
2+
using Ums.Shell.Ddd.Interfaces;
23
using Ums.Shell.Ddd.Rules.Impl;
34
using Ums.Shell.Ddd.Rules.PropertyChange;
45
using Ums.Shell.Ddd.Services.Impl;
6+
using Ums.Shell.Ddd.Services.Interfaces;
57

68
namespace Ums.Shell.Ddd
79
{
@@ -13,11 +15,11 @@ public abstract class ValueObject<TValue> : AbstractNotifyPropertyChanged, IProp
1315
{
1416
#region Members
1517

16-
public TrackingStateManager TrackingState { get; private set; }
18+
public ITrackingStateManager TrackingState { get; private set; }
1719

1820
public ValidatorRuleManager<AbstractRuleValidator<ValueObject<TValue>>> ValidatorRules { get; private set; }
1921

20-
public BrokenRulesManager BrokenRules { get; private set; }
22+
public IBrokenRulesManager BrokenRules { get; private set; }
2123

2224
public bool IsValid => !BrokenRules.GetBrokenRules().Any();
2325

@@ -48,15 +50,11 @@ protected ValueObject(TValue value)
4850
{
4951
ArgumentNullException.ThrowIfNull(value, nameof(value));
5052

51-
/*
52-
* TODO: Enhance this logic using Inversion of Control (IoC) without injecting dependencies directly in the constructor.
53-
* We also need to abstract the creation of dependency instances to support multiple IoC containers.
54-
*/
55-
BrokenRules = new BrokenRulesManager();
53+
BrokenRules = DddServiceFactory.CreateBrokenRulesManager();
5654

5755
ValidatorRules = new ValidatorRuleManager<AbstractRuleValidator<ValueObject<TValue>>>();
5856

59-
TrackingState = new TrackingStateManager();
57+
TrackingState = DddServiceFactory.CreateTrackingStateManager();
6058

6159
RegisterProperty(nameof(InternalValue), typeof(TValue), value, ValuePropertyChanged);
6260

0 commit comments

Comments
 (0)