-
Notifications
You must be signed in to change notification settings - Fork 434
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor/More proper use of DI for Synchronizer (#7500)
Co-authored-by: Lukasz Rozmej <lukasz.rozmej@gmail.com> Co-authored-by: Alex <alexb5dh@gmail.com>
- Loading branch information
1 parent
31c5131
commit ae3e7a6
Showing
40 changed files
with
911 additions
and
749 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 113 additions & 0 deletions
113
src/Nethermind/Nethermind.Core.Test/ContainerBuilderExtensionsTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
// SPDX-FileCopyrightText: 2024 Demerzel Solutions Limited | ||
// SPDX-License-Identifier: LGPL-3.0-only | ||
|
||
using System; | ||
using Autofac; | ||
using FluentAssertions; | ||
using NUnit.Framework; | ||
|
||
namespace Nethermind.Core.Test; | ||
|
||
public class ContainerBuilderExtensionsTests | ||
{ | ||
[Test] | ||
public void AddPropertiesFrom_CanAddProperties() | ||
{ | ||
ITestInterface interfaceImplementation = new InterfaceImplementation(); | ||
IContainer sp = new ContainerBuilder() | ||
.AddPropertiesFrom(interfaceImplementation) | ||
.Build(); | ||
|
||
sp.ResolveOptional<DeclaredService>().Should().NotBeNull(); | ||
sp.ResolveOptional<DeclaredInBase>().Should().BeNull(); | ||
sp.ResolveOptional<Ignored>().Should().BeNull(); | ||
sp.ResolveOptional<DeclaredButNullService>().Should().BeNull(); | ||
} | ||
|
||
[Test] | ||
public void TestRegisterNamedComponent() | ||
{ | ||
IContainer sp = new ContainerBuilder() | ||
.AddScoped<MainComponent>() | ||
.AddScoped<MainComponentDependency>() | ||
.RegisterNamedComponentInItsOwnLifetime<MainComponent>("custom", cfg => | ||
{ | ||
// Override it in custom | ||
cfg.AddScoped<MainComponentDependency, MainComponentDependencySubClass>(); | ||
}) | ||
.Build(); | ||
|
||
using (ILifetimeScope scope = sp.BeginLifetimeScope()) | ||
{ | ||
scope.Resolve<MainComponent>().Property.Should().BeOfType<MainComponentDependency>(); | ||
} | ||
|
||
MainComponentDependency customMainComponentDependency = sp.ResolveNamed<MainComponent>("custom").Property; | ||
sp.ResolveNamed<MainComponent>("custom").Property.Should().BeOfType<MainComponentDependencySubClass>(); | ||
|
||
sp.Dispose(); | ||
|
||
customMainComponentDependency.WasDisposed.Should().BeTrue(); | ||
} | ||
|
||
private class MainComponent(MainComponentDependency mainComponentDependency, ILifetimeScope scope) : IDisposable | ||
{ | ||
public MainComponentDependency Property => mainComponentDependency; | ||
|
||
public void Dispose() | ||
{ | ||
scope.Dispose(); | ||
} | ||
} | ||
|
||
private class MainComponentDependency : IDisposable | ||
{ | ||
public bool WasDisposed { get; set; } | ||
|
||
public void Dispose() | ||
{ | ||
WasDisposed = true; | ||
} | ||
} | ||
|
||
private class MainComponentDependencySubClass : MainComponentDependency | ||
{ | ||
} | ||
|
||
private class InterfaceImplementation : ITestInterface | ||
{ | ||
public DeclaredService TheService { get; set; } = new DeclaredService(); | ||
public DeclaredButNullService? NullService { get; set; } = null; | ||
public Ignored IgnoredService { get; set; } = new Ignored(); | ||
public DeclaredInBase BaseService { get; set; } = new DeclaredInBase(); | ||
} | ||
|
||
private interface ITestInterface : ITestInterfaceBase | ||
{ | ||
DeclaredService TheService { get; set; } | ||
DeclaredButNullService? NullService { get; set; } | ||
|
||
[SkipServiceCollection] | ||
Ignored IgnoredService { get; set; } | ||
} | ||
|
||
private interface ITestInterfaceBase | ||
{ | ||
DeclaredInBase BaseService { get; set; } | ||
} | ||
|
||
private class DeclaredInBase { } | ||
private class DeclaredService { } | ||
private class DeclaredButNullService { } | ||
private class Ignored { } | ||
|
||
private class DisposableService : IDisposable | ||
{ | ||
public bool WasDisposed { get; set; } = false; | ||
|
||
public void Dispose() | ||
{ | ||
WasDisposed = true; | ||
} | ||
} | ||
} |
Oops, something went wrong.