Skip to content

Commit

Permalink
chore: Optimize code
Browse files Browse the repository at this point in the history
  • Loading branch information
Anson.G committed Jun 26, 2024
1 parent 8f042f4 commit 9342ff1
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 10 deletions.
5 changes: 3 additions & 2 deletions CoRProcessor/CoRProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ public CoRProcessor<T> AddRange(IEnumerable<IChainProcessor<T>> processors)

public async Task<T> Execute(T t, CancellationToken token = default)
{
_chainProcessors.Add(new EmptyProcessor<T>());
_firstHandler = BuildChain(_chainProcessors.DistinctBy(x => x.GetType()).ToList());

try
Expand Down Expand Up @@ -95,10 +94,12 @@ public CoRProcessor<T> OnException(FuncDelegate<T> action)
return this;
}

private IChainProcessor<T> BuildChain(IReadOnlyList<IChainProcessor<T>> processors)
private IChainProcessor<T> BuildChain(List<IChainProcessor<T>> processors)
{
if (processors.Count == 0)
throw new Exception("No processors provided. At least one processor is required.");

processors.Add(new EmptyProcessor<T>());

for (var i = 0; i < processors.Count - 1; i++)
{
Expand Down
10 changes: 2 additions & 8 deletions CoRProcessor/SetupCor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@ public static IServiceCollection AddCoR(this IServiceCollection services, Assemb
foreach (var type in assembly.GetTypes()
.Where(t => IsAssignableToGenericType(t, typeof(IChainProcessor<>)) && t.IsClass))
{
if (type.GetGenericArguments().Length > 0)
{
continue;
}
if (type.GetGenericArguments().Length > 0) continue;
services.AddScoped(type);
}

Expand All @@ -28,10 +25,7 @@ public static ContainerBuilder AddCoR(this ContainerBuilder containerBuilder, As
foreach (var type in assembly.GetTypes()
.Where(t => IsAssignableToGenericType(t, typeof(IChainProcessor<>)) && t.IsClass))
{
if (type.GetGenericArguments().Length > 0)
{
continue;
}
if (type.GetGenericArguments().Length > 0) continue;
containerBuilder.RegisterType(type).AsSelf().AsImplementedInterfaces().InstancePerLifetimeScope();
}

Expand Down
18 changes: 18 additions & 0 deletions UnitTests/UnitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -218,4 +218,22 @@ public async Task TestCorProcessorRunningOnAutofac()

Assert.That(result.Result, Is.EqualTo(2));
}


[Test]
public async Task TestEmptyProcessorRunning()
{
var emptyProcessor = new EmptyProcessor<NumberContext>();
emptyProcessor.Next = new AdditionProcessor();

var result = await emptyProcessor.Handle(new NumberContext()
{
Number1 = 1,
Number2 = 2,
Operation = Operation.Addition
}, default);

Assert.That(result.Result, Is.EqualTo(0));
Assert.That(emptyProcessor.Next.GetType().FullName, Is.EqualTo(typeof(AdditionProcessor).FullName));
}
}

0 comments on commit 9342ff1

Please sign in to comment.