我正在研究一個ASP.NET MVC應用程序,我想根據web.config中的設置實例化一個類。如何基於Castle Windsor的web.config文件實例化一個類?
<configSections>
<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>
...
我想用這樣的:
var container =
new WindsorContainer (new XmlInterpreter (new ConfigResource ("castle")));
gameRepository = container.Resolve (typeof (IGameRepository)) as IGameRepository;
我怎麼能解決這個問題?提前致謝。
爲什麼不'var gameRepository = container.Resolve();'?更簡單,更好... ' –
2011-04-07 10:23:12