From 9af627b0608ebed8a77947000000eb3dc9fa3b08 Mon Sep 17 00:00:00 2001 From: Niyaz Kashafutdinov Date: Mon, 25 Mar 2024 22:11:40 +0300 Subject: [PATCH] feat: parent container --- ReDI.Tests/ParentContainerTest.cs | 40 +++++++++++++++++++++++++++++++ ReDI/Container.cs | 12 ++++++---- ReDI/ContainerBuilder.cs | 4 ++-- 3 files changed, 49 insertions(+), 7 deletions(-) create mode 100644 ReDI.Tests/ParentContainerTest.cs diff --git a/ReDI.Tests/ParentContainerTest.cs b/ReDI.Tests/ParentContainerTest.cs new file mode 100644 index 0000000..61ec76e --- /dev/null +++ b/ReDI.Tests/ParentContainerTest.cs @@ -0,0 +1,40 @@ +using NUnit.Framework; + +namespace ReDI.Tests; + +[TestFixture] +public class ParentContainerTest +{ + [Test] + public void TestParentContainerBuilding() + { + var containerBuilder = new ContainerBuilder(); + containerBuilder.AddModule(); + var container = containerBuilder.Build(); + Assert.IsNotNull(container); + var containerWithParent = containerBuilder.Build(container); + Assert.IsNotNull(containerWithParent); + Assert.That(containerWithParent, Is.Not.SameAs(container)); + } + + [Test] + public void TestParentContainerResolves() + { + var containerBuilder = new ContainerBuilder(); + containerBuilder.AddModule(); + var container = containerBuilder.Build(); + Assert.IsNotNull(container); + var emptyContainerBuilder = new ContainerBuilder(); + var containerWithParent = emptyContainerBuilder.Build(container); + Assert.IsNotNull(containerWithParent); + Assert.That(containerWithParent, Is.Not.SameAs(container)); + + var abc = container.Resolve(); + Assert.IsNotNull(abc); + Assert.IsInstanceOf(abc); + + var abcFromParent = containerWithParent.Resolve(); + Assert.IsNotNull(abcFromParent); + Assert.IsInstanceOf(abcFromParent); + } +} \ No newline at end of file diff --git a/ReDI/Container.cs b/ReDI/Container.cs index 5c9b852..9b45f9b 100644 --- a/ReDI/Container.cs +++ b/ReDI/Container.cs @@ -12,10 +12,12 @@ public class Container : IDisposable private readonly Dictionary> _registrationsByInterface = new Dictionary>(); private bool _isDisposed; + private Container _parent; private HashSet _constructingTypes = new HashSet(); - internal Container(IEnumerable bindings) + internal Container(IEnumerable bindings, Container parent) { + _parent = parent; var toBuild = new HashSet(); foreach (var binding in bindings) @@ -67,7 +69,7 @@ public void Dispose() public T? Resolve() where T : class { var obj = Resolve(typeof(T)); - return obj != null ? (T)obj : null; + return obj != null ? (T)obj : _parent?.Resolve(); } public object? Resolve(Type type) @@ -76,15 +78,15 @@ public void Dispose() if (IsGenericList(type)) { - return ResolveList(type); + return ResolveList(type) ?? _parent?.ResolveList(type); } else if (IsGeneric(type)) { - return ResolveGeneric(type); + return ResolveGeneric(type) ?? _parent?.ResolveGeneric(type); } else { - return ResolveSingle(type); + return ResolveSingle(type) ?? _parent?.ResolveSingle(type); } } diff --git a/ReDI/ContainerBuilder.cs b/ReDI/ContainerBuilder.cs index 0682317..d8e61a0 100644 --- a/ReDI/ContainerBuilder.cs +++ b/ReDI/ContainerBuilder.cs @@ -14,7 +14,7 @@ public void AddModule(TModule module) where TModule : Module _moduleManager.RegisterModule(module); } - public Container Build() + public Container Build(Container parentContainer = null) { var typeManager = new TypeManager(); var modules = _moduleManager.GetRegisteredModules(); @@ -25,7 +25,7 @@ public Container Build() } var bindings = typeManager.GetBindings(); - return new Container(bindings); + return new Container(bindings, parentContainer); } } } \ No newline at end of file