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

Updated unity resolver to resolve all instances #37

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
21 changes: 20 additions & 1 deletion ShortBus.Tests/Containers/ContainerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,18 @@ namespace ShortBus.Tests.Containers
using Unity;
using Windsor;
using SimpleInjector;
using System.Linq;

[TestFixture]
public class ContainerTests
{
public ContainerTests() { }

class Registered { }
interface IRegistered { }

class Registered : IRegistered { }

class Registered2 : IRegistered { }

[Test]
public void AutofacResolveSingleInstance()
Expand Down Expand Up @@ -78,6 +83,20 @@ public void UnityResolveSingleInstance()
Assert.That(resolved, Is.EqualTo(registered));
}

[Test]
public void UnityResolveMultipleTypes()
{
var container = new UnityContainer();
container.RegisterType<IRegistered, Registered>();
container.RegisterType<IRegistered, Registered2>("secondType");

var resolver = new UnityDependencyResolver(container);

var resolved = resolver.GetInstances<IRegistered>();

Assert.That(resolved.Count(), Is.EqualTo(2));
}

[Test]
public void WindsorResolveSingleInstance()
{
Expand Down
13 changes: 12 additions & 1 deletion ShortBus.Unity/UnityDependencyResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,18 @@ public object GetInstance(Type type)

public IEnumerable<T> GetInstances<T>()
{
return _container.ResolveAll<T>();
List<T> results = new List<T>(_container.ResolveAll<T>());

try
{
results.Add(_container.Resolve<T>()); // needed to resolve unnamed instances.
}
catch(ResolutionFailedException)
{
// Unity throws an error if it can't resolve a type. In this case we don't care if it failed to resolve.
}

return results;
}
}
}