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

查看:56
本文介绍了在ViewModelLocator中注册所有视图模型和服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

例如,这是我的SupplierViewModel的DataService接口:

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个ViewModel及其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>();
        ....
    }

我的问题是我需要在ViewModelLocator中注册所有25个ViewModel及其25个DataService吗?

解决方案

另一种可能性是编写一个工厂类ViewModelResolver,然后可以由SimpleIoc注入(如果您有IViewModelResolver). >

主要目的是提供一个ViewModel.您可以根据约定,按字符串,按类型进行任何最适合您的操作.

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

这可以按照惯例和反思来完成,例如返回CustomViewModel的新实例... 有了它,您还可以控制是要检索缓存的视图模型(始终相同)还是根据每个请求生成新的视图模型.

这只是使您了解主意的示例...实现取决于您的要求...

HTM

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

这篇关于在ViewModelLocator中注册所有视图模型和服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆