Created
August 5, 2014 14:59
-
-
Save chrisortman/b6ea7bf45ae8abfc770a to your computer and use it in GitHub Desktop.
Failing test for SM3 property injection
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<packages> | |
<package id="structuremap" version="3.0.5.130" targetFramework="net45" /> | |
<package id="xunit" version="1.9.2" targetFramework="net45" /> | |
</packages> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Linq; | |
using StructureMap; | |
using StructureMap.Pipeline; | |
using Xunit; | |
namespace SmTest | |
{ | |
public class SetterInjectionTest | |
{ | |
[Fact] | |
public void InjectsPropertyOfBaseType() | |
{ | |
var container = new Container(c => | |
{ | |
}); | |
var childContainer = container.GetNestedContainer(); | |
childContainer.Configure(c => | |
{ | |
c.For<IHandlerProvider>().Use<StructureMapHandlerProvider>(); | |
c.Policies.SetAllProperties(p => p.OfType<IHandlerProvider>()); | |
c.For(typeof (IModule)).LifecycleIs(Lifecycles.Unique).Use(typeof (DerivedModule)); | |
}); | |
var bm = (DerivedModule) childContainer.GetAllInstances<IModule>().First(); | |
Assert.NotNull(bm.Handlers); | |
} | |
} | |
public interface IModule | |
{ | |
} | |
public abstract class BaseModule : IModule | |
{ | |
public IHandlerProvider Handlers { get; set; } | |
} | |
public class DerivedModule : BaseModule | |
{ | |
} | |
public interface IHandlerProvider | |
{ | |
string Test(); | |
} | |
public class StructureMapHandlerProvider : IHandlerProvider | |
{ | |
private IContainer _container; | |
public StructureMapHandlerProvider(IContainer container) | |
{ | |
_container = container; | |
} | |
public string Test() | |
{ | |
return "Hello"; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The easy answer is "don't do that". The preferred answer is "don't use setter injection unless you absolutely have to". I'm adding a new issue to throw exceptions if you try to do this:
structuremap/structuremap#284