简体   繁体   中英

Castle windsor resolve instance in custom code

How can I leverage Windsor's lifestyle management but have my own resolve method? At the moment I register my dependency as follows:

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

I would like to resolve the dependency by following lambda:

() => {
    return new EntityContext("EntityContext");
}

How can this be combined?

Use UsingFactoryMethod to specify how to create the instance:

container.Register(Component.For<DbContext>()
    .UsingFactoryMethod(() => new EntityContext("EntityContext"))
    .LifestylePerWebRequest());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM