1
我有一個WPF MVVM應用程序有一個模型和服務程序集。我試圖弄清楚如何使用Windsor容器來解決本地(服務層中的服務)依賴項,但我唯一能找出的東西感覺混亂和不正確。IWindsorInstaller中的程序集和解決本地依賴項
Services安裝:
public class ServicesInstaller : IWindsorInstaller
{
public void Install(IWindsorContainer container, IConfigurationStore store)
{
//Services
container.Register(
Component.For<IServiceA>().ImplementedBy<ServiceA>().LifeStyle.Singleton,
Component.For<IServiceB>().ImplementedBy<ServiceB>().LifeStyle.Singleton
}
}
服務消費者(位於服務):
public class ServiceConsumer
{
public SomeMethodThatUsesServiceAOnlyOcassionally()
{
//buncha logic.
if (allThatFailed)
{
??? ResolveServiceA ???
}
}
}
因爲我不是依賴於ServiceA經常,我不想通過傳遞給它的構造函數注入或屬性注入。我會爲安裝程序添加一個靜態容器實例,但我必須相信有一個比這更加習慣的解決方案。
那麼從來沒有理由以編程方式解析類型(除了初始化/佈線)嗎? –
@Ritch:如果您手動調用容器(在應用程序邏輯內部)來解析實例?沒有永不!這將導致[服務定位器反模式](http://blog.ploeh.dk/2010/02/03/ServiceLocatorIsAnAntiPattern.aspx)。 – Steven
好的,服務定位器是我的習慣。我明白了,但很容易默認爲舊思維。 –