开发者

How to instantiate a class based on web.config file with Castle Windsor?

I'm working on an ASP.NET MVC application and I would like to instantiate a class based on settings written in web.config.

<configSections>
    &开发者_JAVA技巧lt;section name="castle"
        type="Castle.Windsor.Configuration.AppDomain.CastleSectionHandler, Castle.Windsor" />
</configSections>

<castle>
    <components>
        <component
            id="SqlRepository"
            service="GomokuGameAppDomain.IGameRepository, GomokuGameAppDomain"
            type="GomokuGameAppDomain.SqlGameRepository, GomokuGameAppDomain"
            lifestyle="PerWebRequest">
            <parameters>
                 <connectionString>Data Source=ZOLIQ-PC\SQLEXPRESS;Initial Catalog=Gomoku;Integrated Security=True</connectionString>
            </parameters>
    </component>
</castle>
...

I would like to use something like this:

var container =
    new WindsorContainer (new XmlInterpreter (new ConfigResource ("castle")));

gameRepository = container.Resolve (typeof (IGameRepository)) as IGameRepository;

How could I resolve this? Thanks in advance.


Just for the sake of completeness, the whole code looks like this:

var container = new WindsorContainer();
container.Install(Castle.Windsor.Installer.Configuration.FromAppConfig());
var gameRepository = container.Resolve<IGameRepository>();

Happy coding.


container.Install(Configuration.FromAppConfig());
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜