自定义代码中的 Castle Windsor 解析实例

Voj*_*h B 0 c# castle-windsor

如何利用温莎的生活方式管理,但有自己的解决方法?目前我注册我的依赖如下:

_container.Register(Component.For<DbContext>()
    .ImplementedBy<EntityContext>().DependsOn(Dependency.OnValue("connectionName", "EntityContext"))
    .LifestylePerWebRequest());
Run Code Online (Sandbox Code Playgroud)

我想通过以下 lambda 解决依赖关系:

() => {
    return new EntityContext("EntityContext");
}
Run Code Online (Sandbox Code Playgroud)

这怎么能结合?

Pat*_*irk 5

使用UsingFactoryMethod指定如何创建实例:

container.Register(Component.For<DbContext>()
    .UsingFactoryMethod(() => new EntityContext("EntityContext"))
    .LifestylePerWebRequest());
Run Code Online (Sandbox Code Playgroud)