Skip to content

Commit

Permalink
tightens up finding the right constructor in an expression. Closes GH…
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremydmiller committed Jan 27, 2015
1 parent b86c0b7 commit f1954f0
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
using System.Diagnostics;
using NUnit.Framework;
using StructureMap.Graph;

namespace StructureMap.Testing.Bugs
{
[TestFixture]
public class Bug_322_class_with_multiple_constructors
{
[Test]
public void reproduce()
{
var anotherDependency = new AnotherDependency();

var container = new Container(x =>
{
x.Scan(s =>
{
s.TheCallingAssembly();
s.AssembliesFromApplicationBaseDirectory();
s.WithDefaultConventions();
});
x.For<IActivityService>().Singleton();
x.ForConcreteType<ClassWithTwoConstructors>()
.Configure.SelectConstructor(() => new ClassWithTwoConstructors(new AnotherDependency()))
.Ctor<AnotherDependency>().Is(anotherDependency);
//.Ctor<AnotherDependency>("anotherDependency").Is(anotherDependency);
});

container.GetInstance<MainService>()
.AnotherDependency.AnotherDependency.ShouldBeTheSameAs(anotherDependency);
}


public class AnotherDependency
{
public AnotherDependency()
{
}
}

public class ClassWithTwoConstructors
{
private readonly AnotherDependency _anotherDependency;


public ClassWithTwoConstructors(int age, string name)
{

}

public ClassWithTwoConstructors(AnotherDependency anotherDependency)
{
_anotherDependency = anotherDependency;
}

public AnotherDependency AnotherDependency
{
get { return _anotherDependency; }
}
}

public interface IStoryService { }

public class StoryService : IStoryService
{
}

public interface IActivityService { }
public class ActivityService : IActivityService { }

public class MainService
{
private readonly ClassWithTwoConstructors _anotherDependency;

public MainService(IStoryService storyService,
ClassWithTwoConstructors anotherDependency)
{
_anotherDependency = anotherDependency;
}

public ClassWithTwoConstructors AnotherDependency
{
get { return _anotherDependency; }
}
}
}





}
1 change: 1 addition & 0 deletions src/StructureMap.Testing/StructureMap.Testing.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@
<Compile Include="Bugs\Bug_300_naive_can_be_plugged_into_tests.cs" />
<Compile Include="Bugs\Bug_313.cs" />
<Compile Include="Bugs\Bug_318.cs" />
<Compile Include="Bugs\Bug_322_class_with_multiple_constructors.cs" />
<Compile Include="Bugs\BuildUpBug.cs" />
<Compile Include="Bugs\closed_type_generic_is_in_get_all.cs" />
<Compile Include="Bugs\CloseOpenGenericsWithSomeSpecifics.cs" />
Expand Down
2 changes: 1 addition & 1 deletion src/StructureMap/Building/ConcreteBuild.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ public class ConcreteBuild<T> : ConcreteBuild, IDependencySource
{
public static ConcreteBuild<T> For(Expression<Func<T>> expression)
{
var finder = new ConstructorFinderVisitor();
var finder = new ConstructorFinderVisitor(typeof(T));
finder.Visit(expression);

var ctor = finder.Constructor;
Expand Down
2 changes: 1 addition & 1 deletion src/StructureMap/Pipeline/SmartInstance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public SmartInstance(Expression<Func<T>> constructorSelection = null)

public SmartInstance<T, TPluginType> SelectConstructor(Expression<Func<T>> constructor)
{
var finder = new ConstructorFinderVisitor();
var finder = new ConstructorFinderVisitor(typeof(T));
finder.Visit(constructor);

_inner.Constructor = finder.Constructor;
Expand Down
12 changes: 11 additions & 1 deletion src/StructureMap/ReflectionHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -463,16 +463,26 @@ protected virtual Expression VisitInvocation(InvocationExpression iv)

internal class ConstructorFinderVisitor : ExpressionVisitorBase
{
private readonly Type _type;
private ConstructorInfo _constructor;

public ConstructorFinderVisitor(Type type)
{
_type = type;
}

public ConstructorInfo Constructor
{
get { return _constructor; }
}

protected override NewExpression VisitNew(NewExpression nex)
{
_constructor = nex.Constructor;
if (nex.Type == _type)
{
_constructor = nex.Constructor;
}

return base.VisitNew(nex);
}
}
Expand Down

0 comments on commit f1954f0

Please sign in to comment.