Working towards IInjectionAware, but for some reason test case isn't being injected at all...

This commit is contained in:
Antony Male 2014-02-18 08:27:14 +00:00
parent 145e5697f2
commit c7ea379071
3 changed files with 60 additions and 9 deletions

View File

@ -853,14 +853,27 @@ namespace Stylet
return Expression.Convert(Expression.Constant(x.DefaultValue), x.ParameterType);
});
// TODO: Might want to optimise out the block if there's no builder upper - not sure of the performance impact
var instanceVar = Expression.Variable(this.Type, "instance");
var creator = Expression.New(ctor, ctorParams);
var assignment = Expression.Assign(instanceVar, creator);
var buildUpExpression = container.GetBuilderUpper(this.Type).GetExpression(container, assignment);
var completeExpression = Expression.Block(new ParameterExpression[] { instanceVar }, assignment, buildUpExpression, instanceVar);
Expression completeExpression;
// If there's no buildUp, avoid creating the block expression after all - it hurts runtime performance
if (buildUpExpression == Expression.Empty())
{
completeExpression = creator;
}
else
{
var blockItems = new List<Expression>() { assignment, buildUpExpression };
if (typeof(IInjectionAware).IsAssignableFrom(this.Type))
blockItems.Add(Expression.Call(assignment, typeof(IInjectionAware).GetMethod("ParametersInjected")));
blockItems.Add(instanceVar); // Final appearance of instanceVar, as this sets the return value of the block
completeExpression = Expression.Block(new[] { instanceVar }, blockItems);
}
this.creationExpression = completeExpression;
return completeExpression;
@ -1024,6 +1037,11 @@ namespace Stylet
}
}
public interface IInjectionAware
{
void ParametersInjected();
}
public class StyletIoCException : Exception
{
public StyletIoCException(string message) : base(message) { }

View File

@ -9,7 +9,7 @@ using System.Threading.Tasks;
namespace StyletUnitTests
{
[TestFixture]
public class StyletIoCBuildUpTests
public class StyletIoCParameterInjectionTests
{
class C1 { }
interface I2 { }
@ -17,16 +17,16 @@ namespace StyletUnitTests
class Subject1
{
public C1 Ignored;
public C1 Ignored = null;
[Inject]
public C1 C1;
public C1 C1 = null;
}
class Subject2
{
[Inject]
private C1 c1;
private C1 c1 = null;
public C1 GetC1() { return this.c1; }
}
@ -50,7 +50,16 @@ namespace StyletUnitTests
class Subject5
{
[Inject("key")]
public C1 C1;
public C1 C1 = null;
}
class Subject6
{
[Inject]
public C1 C1 = null;
public bool ParametersInjectedCalledCorrectly;
public void ParametersInjected() { this.ParametersInjectedCalledCorrectly = this.C1 != null; }
}
[Test]
@ -117,5 +126,29 @@ namespace StyletUnitTests
var subject = new Subject1();
Assert.Throws<StyletIoCRegistrationException>(() => ioc.BuildUp(subject));
}
[Test]
public void BuildsUpParametersOfNewlyCreatedType()
{
var ioc = new StyletIoC();
ioc.Bind<C1>().ToSelf();
ioc.Bind<Subject1>().ToSelf();
var subject = ioc.Get<Subject1>();
Assert.IsInstanceOf<C1>(subject.C1);
Assert.IsNull(subject.Ignored);
}
[Test]
public void CallsParametersInjectedAfterInjectingParameters()
{
var ioc = new StyletIoC();
ioc.Bind<C1>().ToSelf();
ioc.Bind<Subject6>().ToSelf();
var subject = ioc.Get<Subject6>();
Assert.IsInstanceOf<C1>(subject.C1);
Assert.IsTrue(subject.ParametersInjectedCalledCorrectly);
}
}
}

View File

@ -48,7 +48,7 @@
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="StyletIoCAutobindingTests.cs" />
<Compile Include="StyletIoCBindingChecksTests.cs" />
<Compile Include="StyletIoCBuildUpTests.cs" />
<Compile Include="StyletIoCParameterInjectionTests.cs" />
<Compile Include="StyletIoCConstructorInjectionTests.cs" />
<Compile Include="StyletIoCFactoryTests.cs" />
<Compile Include="StyletIoCGetAllTests.cs" />