Modified:
experiments/ninject2/src/Ninject.Tests/Integration/EnumerableDependenciesTests.cs
experiments/ninject2/src/Ninject/Infrastructure/Introspection/FormatExtensions.cs
experiments/ninject2/src/Ninject/Planning/Targets/Target.cs
Log:
Modified:
experiments/ninject2/src/Ninject.Tests/Integration/EnumerableDependenciesTests.cs
==============================================================================
---
experiments/ninject2/src/Ninject.Tests/Integration/EnumerableDependenciesTests.cs
(original)
+++
experiments/ninject2/src/Ninject.Tests/Integration/EnumerableDependenciesTests.cs
Wed Feb 25 16:50:03 2009
@@ -6,20 +6,45 @@
namespace Ninject.Tests.Integration.EnumerableDependenciesTests
{
- public class EnumerableDependenciesContext
+ public abstract class EnumerableDependenciesContext
{
protected readonly StandardKernel kernel;
- public EnumerableDependenciesContext()
+ protected EnumerableDependenciesContext()
{
kernel = new StandardKernel();
}
+
+ protected abstract void VerifyInjection(IParent parent);
+ }
+
+ public class UnconstrainedDependenciesContext :
EnumerableDependenciesContext
+ {
+ protected override void VerifyInjection(IParent parent)
+ {
+ parent.ShouldNotBeNull();
+ parent.Children.ShouldNotBeNull();
+ parent.Children.Count.ShouldBe(2);
+ parent.Children[0].ShouldBeInstanceOf<ChildA>();
+ parent.Children[1].ShouldBeInstanceOf<ChildB>();
+ }
+ }
+
+ public class ConstrainedDependenciesContext :
EnumerableDependenciesContext
+ {
+ protected override void VerifyInjection(IParent parent)
+ {
+ parent.ShouldNotBeNull();
+ parent.Children.ShouldNotBeNull();
+ parent.Children.Count.ShouldBe(1);
+ parent.Children[0].ShouldBeInstanceOf<ChildB>();
+ }
}
- public class WhenServiceRequestsUnconstrainedEnumerableOfDependencies :
EnumerableDependenciesContext
+ public class WhenServiceRequestsUnconstrainedEnumerableOfDependencies :
UnconstrainedDependenciesContext
{
[Fact]
- public void ServiceIsInjectedWithAllAvailableDependencies()
+ public void ServiceIsInjectedWithEnumeratorOfAllAvailableDependencies()
{
kernel.Bind<IParent>().To<RequestsEnumerable>();
kernel.Bind<IChild>().To<ChildA>();
@@ -27,15 +52,41 @@
var parent = kernel.Get<IParent>();
- parent.ShouldNotBeNull();
- parent.Children.ShouldNotBeNull();
- parent.Children.Length.ShouldBe(2);
- parent.Children[0].ShouldBeInstanceOf<ChildA>();
- parent.Children[1].ShouldBeInstanceOf<ChildB>();
+ VerifyInjection(parent);
+ }
+ }
+
+ public class WhenServiceRequestsUnconstrainedListOfDependencies :
UnconstrainedDependenciesContext
+ {
+ [Fact]
+ public void ServiceIsInjectedWithListOfAllAvailableDependencies()
+ {
+ kernel.Bind<IParent>().To<RequestsList>();
+ kernel.Bind<IChild>().To<ChildA>();
+ kernel.Bind<IChild>().To<ChildB>();
+
+ var parent = kernel.Get<IParent>();
+
+ VerifyInjection(parent);
+ }
+ }
+
+ public class WhenServiceRequestsUnconstrainedArrayOfDependencies :
UnconstrainedDependenciesContext
+ {
+ [Fact]
+ public void ServiceIsInjectedWithArrayOfAllAvailableDependencies()
+ {
+ kernel.Bind<IParent>().To<RequestsArray>();
+ kernel.Bind<IChild>().To<ChildA>();
+ kernel.Bind<IChild>().To<ChildB>();
+
+ var parent = kernel.Get<IParent>();
+
+ VerifyInjection(parent);
}
}
- public class WhenServiceRequestsConstrainedEnumerableOfDependencies :
EnumerableDependenciesContext
+ public class WhenServiceRequestsConstrainedEnumerableOfDependencies :
ConstrainedDependenciesContext
{
[Fact]
public void ServiceIsInjectedWithAllDependenciesThatMatchTheConstraint()
@@ -46,10 +97,37 @@
var parent = kernel.Get<IParent>();
- parent.ShouldNotBeNull();
- parent.Children.ShouldNotBeNull();
- parent.Children.Length.ShouldBe(1);
- parent.Children[0].ShouldBeInstanceOf<ChildB>();
+ VerifyInjection(parent);
+ }
+ }
+
+ public class WhenServiceRequestsConstrainedListOfDependencies :
ConstrainedDependenciesContext
+ {
+ [Fact]
+ public void ServiceIsInjectedWithAllDependenciesThatMatchTheConstraint()
+ {
+ kernel.Bind<IParent>().To<RequestsConstrainedList>();
+ kernel.Bind<IChild>().To<ChildA>().Named("joe");
+ kernel.Bind<IChild>().To<ChildB>().Named("bob");
+
+ var parent = kernel.Get<IParent>();
+
+ VerifyInjection(parent);
+ }
+ }
+
+ public class WhenServiceRequestsConstrainedArrayOfDependencies :
ConstrainedDependenciesContext
+ {
+ [Fact]
+ public void ServiceIsInjectedWithAllDependenciesThatMatchTheConstraint()
+ {
+ kernel.Bind<IParent>().To<RequestsConstrainedArray>();
+ kernel.Bind<IChild>().To<ChildA>().Named("joe");
+ kernel.Bind<IChild>().To<ChildB>().Named("bob");
+
+ var parent = kernel.Get<IParent>();
+
+ VerifyInjection(parent);
}
}
@@ -60,26 +138,66 @@
public interface IParent
{
- IChild[] Children { get; }
+ IList<IChild> Children { get; }
}
public class RequestsEnumerable : IParent
{
- public IChild[] Children { get; private set; }
+ public IList<IChild> Children { get; private set; }
public RequestsEnumerable(IEnumerable<IChild> children)
{
- Children = children.ToArray();
+ Children = children.ToList();
+ }
+ }
+
+ public class RequestsList : IParent
+ {
+ public IList<IChild> Children { get; private set; }
+
+ public RequestsList(List<IChild> children)
+ {
+ Children = children;
+ }
+ }
+
+ public class RequestsArray : IParent
+ {
+ public IList<IChild> Children { get; private set; }
+
+ public RequestsArray(IChild[] children)
+ {
+ Children = children;
}
}
public class RequestsConstrainedEnumerable : IParent
{
- public IChild[] Children { get; private set; }
+ public IList<IChild> Children { get; private set; }
public RequestsConstrainedEnumerable([Named("bob")] IEnumerable<IChild>
children)
{
- Children = children.ToArray();
+ Children = children.ToList();
+ }
+ }
+
+ public class RequestsConstrainedList : IParent
+ {
+ public IList<IChild> Children { get; private set; }
+
+ public RequestsConstrainedList([Named("bob")] List<IChild> children)
+ {
+ Children = children;
+ }
+ }
+
+ public class RequestsConstrainedArray : IParent
+ {
+ public IList<IChild> Children { get; private set; }
+
+ public RequestsConstrainedArray([Named("bob")] IChild[] children)
+ {
+ Children = children;
}
}
}
Modified:
experiments/ninject2/src/Ninject/Infrastructure/Introspection/FormatExtensions.cs
==============================================================================
---
experiments/ninject2/src/Ninject/Infrastructure/Introspection/FormatExtensions.cs
(original)
+++
experiments/ninject2/src/Ninject/Infrastructure/Introspection/FormatExtensions.cs
Wed Feb 25 16:50:03 2009
@@ -138,7 +138,7 @@
var sb = new StringBuilder();
sb.Append(type.Name.Substring(0, type.Name.LastIndexOf('`')));
- sb.Append("[");
+ sb.Append("{");
foreach (Type genericArgument in type.GetGenericArguments())
{
@@ -147,7 +147,7 @@
}
sb.Remove(sb.Length - 2, 2);
- sb.Append("]");
+ sb.Append("}");
return sb.ToString();
}
Modified: experiments/ninject2/src/Ninject/Planning/Targets/Target.cs
==============================================================================
--- experiments/ninject2/src/Ninject/Planning/Targets/Target.cs (original)
+++ experiments/ninject2/src/Ninject/Planning/Targets/Target.cs Wed Feb 25
16:50:03 2009
@@ -128,7 +128,7 @@
if (Type.IsArray)
{
Type service = Type.GetElementType();
- return ResolveInstances(service, parent).ToArraySlow(service);
+ return ResolveInstances(service,
parent).CastSlow(service).ToArraySlow(service);
}
if (Type.IsGenericType)
@@ -136,8 +136,8 @@
Type gtd = Type.GetGenericTypeDefinition();
Type service = Type.GetGenericArguments()[0];
- if (typeof(ICollection<>).IsAssignableFrom(gtd))
- return ResolveInstances(service, parent).ToListSlow(service);
+ if (gtd == typeof(List<>) || gtd == typeof(IList<>))
+ return ResolveInstances(service,
parent).CastSlow(service).ToListSlow(service);
if (typeof(IEnumerable<>).IsAssignableFrom(gtd))
return ResolveInstances(service, parent).CastSlow(service);