我打開其他IoC容器,如NInject和StructureMap,如果它們比這更清潔。我聽說StructureMap剛剛引入了「容器」,可能會簡化這個問題?有沒有更好的方法將Castle Windsor的API用於工廠?
正如標題所說,有沒有更好的方法?這看起來像很多代碼,只是爲了註冊一個需要工廠來創建它的對象。
// The process to register an object, with a factory method
var cfg = new MutableConfiguration(p.Name);
cfg.Attributes["factoryId"] = p.TypeFactory.Name;
cfg.Attributes["factoryCreate"] = "Create";
var model = _container.Kernel.ComponentModelBuilder.BuildModel(
p.Name, p.TypeService, p.Type, null);
model.LifestyleType = LifestyleType.Pooled;
model.Configuration = cfg;
_container.Kernel.AddCustomComponent(model);
添加或黑白的部件的 「非工廠」 的方式:
// registering a component with no factory method
_container.AddComponentLifeStyle(
p.Name, p.TypeService, p.Type, LifestyleType.Singleton);
第一似乎過於複雜。
在此先感謝!
您是否考慮在發佈此文檔之前檢查文檔? http://using.castleproject.org/display/IoC/Fluent+Registration+API#FluentRegistrationAPI-Usingadelegateascomponentfactory: – 2010-03-03 07:51:44
plus也FactorySupportFacility類有方法來幫助這個,如果你不想流利的API。你基本上選擇了這樣做的最不理想的方式。 – 2010-03-03 07:53:49
你好Krzysztof,並感謝您的意見。有趣的是,我從來沒有見過鏈接到城堡的文檔 - 因爲任何鏈接都是這樣的網頁:http://api.castleproject.org/直接從他們的網站。感謝您將我與實際文檔聯繫起來(但請在將來的評論中保持仁慈)。 – eduncan911 2010-03-03 17:34:43