我一直在想IApplicationContext.GetObject(string name)方法,它似乎很大程度上依賴於魔術字符串來從應用程序上下文中獲取對象。有了像StructureMap這樣的其他容器,你可以使用泛型來指定配置和請求對象,但是比使用Spring.Net IoC容器的字符串來從ApplicationContext請求對象還有更好的方法嗎?Spring.Net IoC和Magic Strings
2
A
回答
0
您可以調用一個包裝器,並使用泛型類型參數。
事情是這樣的:
public void MyMethod()
{
IMyService myService = ApplicationContextWrapper.Resolve<IMyService>();
}
public static class ApplicationContextWrapper
{
public static T Resolve<T>()
{
return ApplicationContext.Resolve<T>(typeof(T).Name);
}
}
並不像某些其他的IoC contatiners一樣的好,但至少你會得到某種編譯器的支持。
2
Spring通過adapter支持CommonServiceLocator。此接口有您期望的the API,您還可以針對通用服務定位器進行編譯,以便您可以在不需要修改現有代碼的情況下切換IoC容器。
相關問題
- 1. Spring.Net IoC應用上下文
- 2. Javascript發佈和訂閱 - 如何避免magic strings
- 3. PyCharm和f-strings
- 4. Spring.NET和Immediacy CMS
- 5. ASP.NET MVC和Spring.NET
- 6. 集類型的XmlElement從XML Spring.NET的IoC配置
- 7. Spring.Net IoC:使用配置元數據的替代方法XML
- 8. IoC容器,WCF和ServiceHostFactories
- 9. spring.net驗證
- 10. Caliburn + Spring.NET - 配置自引導
- 11. ThreadPool SetMaxThreads和SetMinThreads Magic Number
- 12. Mach-O Magic和Cigam澄清
- 13. WP7上的Spring IoC?
- 14. IOC和Silverlight
- 15. Automapper和IoC
- 16. C#IOC和NancyFX
- 17. Unity IoC和Solrnet
- 18. NHibernate和IoC IInterceptor
- 19. IoC和事件
- 20. rootViewController magic
- 21. Spring.net和Oracle Coherence集成
- 22. ASP.NET MVC與Spring.NET和ModelBinder
- 23. Java - Split strings by「]」
- 24. 對Spring.NET
- 25. Strings的NSMutableArray
- 26. grepping multiple strings
- 27. Matlab Justify Strings
- 28. 使用IOC公開POCOs作爲Web服務 - Spring.net的WebServiceExporter的替代方案?
- 29. IOC(Ninject)和工廠
- 30. IOC與MVC和WCF