我使用结构图来解决这样的构造函数注入
public class MyClass
{
private readonly IDependency _dependency;
public MyClass(IDependency dependency)
{
_dependency = dependency;
}
// my methods
}
我有一些类依赖:IDependency
在 StructureMap 配置中我有
public static IContainer Initialize()
{
ObjectFactory.Initialize(x =>
{
x.For<IDependency>().Use<Dependency>();
});
return ObjectFactory.Container;
}
一切正常
但是在其他控制器中我需要进行方法注入
public ActionResult(IDependency dependency)
{
dependency.DoSomething();
}
我想在依赖变量中有我的依赖类