我有一個CustomersModule.cs有以下初始化()方法:有人能解釋在Prism的解決<>方法中發生的魔法嗎?
public void Initialize()
{
container.RegisterType<ICustomersRepository, CustomersRepository>(new ContainerControlledLifetimeManager());
CustomersPresenter customersPresenter = this.container.Resolve<CustomersPresenter>();
}
的I類從容器解決看起來是這樣的:
class CustomersPresenter
{
private CustomersView view;
private ICustomersRepository customersRespository;
public CustomersPresenter(CustomersView view,
ICustomersRepository customersRepository,
TestWhatever testWhatever)
{
this.view = view;
this.customersRespository = customersRepository;
}
}
的TestWhatever類只是一個虛擬類我創建:
public class TestWhatever
{
public string Title { get; set; }
public TestWhatever()
{
Title = "this is the title";
}
}
然而,容器高高興興地解決CustomersPresenter
即使我從來沒有註冊它,也是集裝箱莫名其妙地發現 TestWhatever,其實例化,並將其注入到CustomersPresenter
。
我很驚訝地意識到這個,因爲我無法在Prism文檔中找到明確指出容器是如此自動的任何地方。
所以這很好,但容器做的事情我不知道,還有什麼可以做,我不知道? 例如,我可以從其他模塊注入類,如果模塊恰好被加載,容器將注入它們,如果不是,它會注入一個null?
但是那麼解耦是在哪裏進行的,即如果我需要來自另一個模塊的類「MenuManager」,並且該模塊沒有碰巧被加載,我就理解容器應該例如注入null以便應用程序可以在有或沒有其他部分的情況下運行,但是如果我刪除類「TestWhatever」的應用程序會得到一個錯誤。 – 2009-07-20 14:06:33