在ViewModelLocator中注册所有视图模型和服务[英] Register all viewmodel and services in ViewModelLocator

本文是小编为大家收集整理的关于在ViewModelLocator中注册所有视图模型和服务的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

本文来自:IT宝库(https://www.itbaoku.cn)

我正在开发一个新的MVVM Light WPF应用程序.我有25个视图和ViewModels和25个DataService接口及其实现(一个用于设计时间数据服务的实现,一个用于实时DataService).

对于例如,这是我的供应商ViewModel的我的数据服务界面:

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)
    {
      ....
    }
}

在ViewModocator中,我需要注册我的所有25个ViewModels及其25个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>();
        ....
    }

我的问题是,我需要在我的ViewModeLocator中注册所有我的25个ViewModels及其25个数据服务器?

推荐答案

另一种可能性是编写工厂类ViewModelResolver然后可以通过SimpleIoc注入(给定您有一个IViewModelResolver).

主要目的是交付ViewModel.您可以根据惯例,按字符串,类型来完成任何最适合您的情况.

所以例如ViewModelResolver.GetViewModelFor("Namespace.CustomerView");

这可以按照惯例和反思进行,例如返回CustomViewModel的新实例... 因此,您也可以控制您是否想检索缓存的视图模型(总是相同)或在每个请求上生成新的...

这只是使您想法的示例...实现取决于您的要求...

htm

本文地址:https://www.itbaoku.cn/post/1657687.html

问题描述

I am developing a new MVVM light Wpf application.I have 25 View and ViewModels and 25 DataService Interface and its implementations (One implementation for Design time Data service and one for realtime dataservice).

For Eg, Here is a my DataService Interface for my SupplierViewModel:

interface ISupplierDataService
{
    ObservableCollection<Tbl_Supplier> GetAllSuppliers();
    int GetSupplierCount(string supplierNameMatch);
}

and Here is its implementation for design time :

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)
    {
      ....
    }
}

In ViewModelLocator is I need to register all my 25 ViewModels and its 25 DataService and its implementations like this :

 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>();
        ....
    }

My question is do I need to register all my 25 ViewModels and its 25 DataService in my ViewModelLocator ?

推荐答案

Another possibility would be to write a factory class ViewModelResolver this can then be injected by SimpleIoc (given you have an IViewModelResolver).

The main purpuse is to deliver a ViewModel. You can do it based on conventions, by string, by type, whatever fits best for you.

So for example ViewModelResolver.GetViewModelFor("Namespace.CustomerView");

This could be done per convention and reflection for example to return a new Instance of CustomViewModel... With this you do also have control whether you like to retrieve a cached view model (always the same) or generate a new on each request...

This is just example to get you the idea... The implementation depends on your requirements...

HTM