Skip to content

Commit

Permalink
feat: parent container
Browse files Browse the repository at this point in the history
  • Loading branch information
Niyaz Kashafutdinov committed Mar 25, 2024
1 parent f156c1b commit 9af627b
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 7 deletions.
40 changes: 40 additions & 0 deletions ReDI.Tests/ParentContainerTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using NUnit.Framework;

namespace ReDI.Tests;

[TestFixture]
public class ParentContainerTest
{
[Test]
public void TestParentContainerBuilding()
{
var containerBuilder = new ContainerBuilder();
containerBuilder.AddModule<ABModule>();
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<ABModule>();
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<ServiceABC>();
Assert.IsNotNull(abc);
Assert.IsInstanceOf<ServiceABC>(abc);

var abcFromParent = containerWithParent.Resolve<ServiceABC>();
Assert.IsNotNull(abcFromParent);
Assert.IsInstanceOf<ServiceABC>(abcFromParent);
}
}
12 changes: 7 additions & 5 deletions ReDI/Container.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ public class Container : IDisposable
private readonly Dictionary<Type, HashSet<ServiceRegistration>> _registrationsByInterface = new Dictionary<Type, HashSet<ServiceRegistration>>();

private bool _isDisposed;
private Container _parent;
private HashSet<Type> _constructingTypes = new HashSet<Type>();

internal Container(IEnumerable<BindingInfo> bindings)
internal Container(IEnumerable<BindingInfo> bindings, Container parent)
{
_parent = parent;
var toBuild = new HashSet<ServiceRegistration>();

foreach (var binding in bindings)
Expand Down Expand Up @@ -67,7 +69,7 @@ public void Dispose()
public T? Resolve<T>() where T : class
{
var obj = Resolve(typeof(T));
return obj != null ? (T)obj : null;
return obj != null ? (T)obj : _parent?.Resolve<T>();
}

public object? Resolve(Type type)
Expand All @@ -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);
}
}

Expand Down
4 changes: 2 additions & 2 deletions ReDI/ContainerBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public void AddModule<TModule>(TModule module) where TModule : Module
_moduleManager.RegisterModule(module);
}

public Container Build()
public Container Build(Container parentContainer = null)

Check warning on line 17 in ReDI/ContainerBuilder.cs

View workflow job for this annotation

GitHub Actions / build

Cannot convert null literal to non-nullable reference type.
{
var typeManager = new TypeManager();
var modules = _moduleManager.GetRegisteredModules();
Expand All @@ -25,7 +25,7 @@ public Container Build()
}

var bindings = typeManager.GetBindings();
return new Container(bindings);
return new Container(bindings, parentContainer);
}
}
}

0 comments on commit 9af627b

Please sign in to comment.