Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#18 Use base container for resolving #20

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions BoDi.Tests/IsRegisteredTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,24 @@ public void ShouldReturnTrueIfTypeRegistered()

Assert.IsTrue(isRegistered);
}

[Test]
public void ShouldReturnTrueIfInterfaceRegisteredInBaseContainer()
{
// given

var baseContainer = new ObjectContainer();
var container = new ObjectContainer(baseContainer);

// when

baseContainer.RegisterTypeAs<VerySimpleClass, IInterface1>();

// then

bool isRegistered = container.IsRegistered<IInterface1>();

Assert.IsTrue(isRegistered);
}
}
}
17 changes: 17 additions & 0 deletions BoDi.Tests/ResolveAllTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,23 @@ public void ShouldResolveTheRightTypes()
Assert.IsTrue(results.Contains(container.Resolve<IFancy>("fancier")));
}

[Test]
public void ShouldResolveTypesRegisteredInBaseContainer()
{
// given
var baseContainer = new ObjectContainer();
var container = new ObjectContainer(baseContainer);
baseContainer.RegisterTypeAs<ImFancy, IFancy>("fancy");
baseContainer.RegisterTypeAs<ImFancier, IFancy>("fancier");

// when
var results = container.ResolveAll<IFancy>();

// then
Assert.IsTrue(results.Contains(container.Resolve<IFancy>("fancy")));
Assert.IsTrue(results.Contains(container.Resolve<IFancy>("fancier")));
}

}

}
10 changes: 7 additions & 3 deletions BoDi/BoDi.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/**************************************************************************************
/**************************************************************************************
*
* BoDi: A very simple IoC container, easily embeddable also as a source code.
*
Expand Down Expand Up @@ -754,14 +754,17 @@ public bool IsRegistered<T>(string name)

var keyToResolve = new RegistrationKey(typeToResolve, name);

return registrations.ContainsKey(keyToResolve);
if (registrations.ContainsKey(keyToResolve))
return true;
return baseContainer != null && baseContainer.IsRegistered<T>(name);
}

// ReSharper disable once UnusedParameter.Local
private void AssertNotResolved(RegistrationKey interfaceType)
{
if (resolvedKeys.Contains(interfaceType))
throw new ObjectContainerException("An object has been resolved for this interface already.", null);
baseContainer?.AssertNotResolved(interfaceType);
}

private void ClearRegistrations(RegistrationKey registrationKey)
Expand Down Expand Up @@ -826,7 +829,8 @@ public IEnumerable<T> ResolveAll<T>() where T : class
{
return registrations
.Where(x => x.Key.Type == typeof(T))
.Select(x => Resolve(x.Key.Type, x.Key.Name) as T);
.Select(x => Resolve(x.Key.Type, x.Key.Name) as T)
.Union(baseContainer != null ? baseContainer.ResolveAll<T>() : new T[0]);
}

private object Resolve(Type typeToResolve, ResolutionList resolutionPath, string name)
Expand Down