Skip to content

Instantly share code, notes, and snippets.

@chrisortman
Created August 5, 2014 14:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrisortman/b6ea7bf45ae8abfc770a to your computer and use it in GitHub Desktop.
Save chrisortman/b6ea7bf45ae8abfc770a to your computer and use it in GitHub Desktop.
Failing test for SM3 property injection
<?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>
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";
}
}
}
@jeremydmiller
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment