我正在開發一個新的MVVM輕型Wpf應用程序。我有25個View和ViewModels以及25個DataService接口及其實現(一個用於設計時數據服務,一個用於實時數據服務)。在ViewModelLocator中註冊所有視圖模型和服務
對於例如,下面是我的DataService接口爲我SupplierViewModel:
interface ISupplierDataService
{
ObservableCollection<Tbl_Supplier> GetAllSuppliers();
int GetSupplierCount(string supplierNameMatch);
}
,這裏是它的設計時間實現:
class SupplierDataServiceMock : ISupplierDataService
{
public ObservableCollection<Tbl_Supplier> GetAllSuppliers()
{
.....
}
public int GetSupplierCount(string supplierNameMatch)
{
....
}
}
class SupplierDataService : ISupplierDataService
{
public ObservableCollection<Tbl_Supplier> GetAllSuppliers()
{
....
}
public int GetSupplierCount(string supplierNameMatch)
{
....
}
}
在ViewModelLocator是我需要註冊我的所有25個ViewModels及其DataService及其實現如下:
static ViewModelLocator()
{
ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);
if (ViewModelBase.IsInDesignModeStatic)
{
SimpleIoc.Default.Register<ISupplierDataService, SupplierDataServiceMock>();
SimpleIoc.Default.Register<ICustomerDataService, CustomerDataServiceMock>();
....
}
else
{
SimpleIoc.Default.Register<ISupplierDataService, SupplierDataService>();
SimpleIoc.Default.Register<ICustomerDataService, CustomerDataService>();
....
}
SimpleIoc.Default.Register<MainViewModel>();
SimpleIoc.Default.Register<SupplierViewModel>();
SimpleIoc.Default.Register<CustomerViewModel>();
....
}
我的問題是我需要在我的ViewModelLocator中註冊所有25個ViewModel及其25個DataService嗎?
有什麼問題? – Mashton
@Mashton,我加了我的問題 – Angel
是的,你必須在locator中添加所有的viewmodels和services。 –