I'm pretty much wanting to do what this guy describes (passing a dependency into a custom attribute):
How to use dependency injection with an attribute?
however, I want to do it with StructureMap 4.0, not Ninject.
My project is set up with the custom attribute in my Data Layer
dll, and I already have StructureMap installed and working with my controllers in my UI Layer
.
I have determined I probably have to do Setter injection with StructureMap:
http://docs.structuremap.net/ConstructorAndSetterInjection.htm
however, it is not working correctly for me with my custom attribute in the data layer.
I figured that in order to make things work that I should install Structuremap into my data layer as well, and put this in IoC.cs:
public static IContainer Initialize()
{
Container container = new Container(x =>
{
x.ForConcreteType<My_AuthorizeADAttribute>().Configure.Setter<My_AppDataContext>().IsTheDefault(); //not sure about this line
});
return container;
}
oh.. my custom Attribute:
public class My_AuthorizeADAttribute : AuthorizeAttribute
{
public IMy_Data_Context _dataContext;
[SetterProperty]
public IMy_Data_Context DataContext
{
get { return _dataContext; }
set { _dataContext = value; }
}
Is this the right thing to do? I'm thinking I left out a step. (but then again, I haven't really set up Structuremap on multiple dlls in the same project. Wondering if multiple projects with StructureMap needs something more.
At present, the app will run, but the Property in the Custom Attribute won't populate.
Update: This StackOverflow question has been helpful:
How do I get StructureMap working with an AngularJs / MVC5 and WebApi2 web project
ok, after much research, apparently this is the best option for me now. it means the Attribute has a dependency on StructureMap, but then again, [SetterProperty]
would have it as well.