我在我的MVC应用程序中使用简单的注入器。 我将我的Entity Framework DbContext注入我的MVC控制器,但是一个特定的控制器需要使用可选参数初始化DbContext(例如ownsConnection:false)。
我可以使用不同的构造函数参数多次注册相同的接口吗?我怎么做到这一点?
public static void Initialize()
{
var container = new Container();
container.Options.DefaultScopedLifestyle = new WebRequestLifestyle();
InitializeContainer(container);
container.RegisterMvcControllers(Assembly.GetExecutingAssembly());
container.Verify();
DependencyResolver.SetResolver(new SimpleInjectorDependencyResolver(container));
}
private static void InitializeContainer(Container container)
{
container.Register<IDbContext>(() => new DbContext("ConnectionString"), Lifestyle.Scoped);
}
对于特定的控制器,我需要使用参数contextOwnsConnection:false
注入DbContext答案 0 :(得分:1)
您可以装饰控制器以自定义其分辨率。但你不能直接做到。 Look here,指的是这个问题:How to decorate an ASP.NET MVC controller with Simple Injector。
然后看一下这种技术Intercepting the Creation of Types