MVVM模式之:ViewModel Factory与注入

基于以下的理由,ViewModel也是需要多个,并且需要被注入的:

1:设计时和运行时需要为View提供不同的数据

简单来说,就是设计时需要模拟数据。界面设计开发人员需要进行绑定(包括支持Expression Blend绑定)做一些简单的处理,同时因为提供了模拟数据,UI人员可以更好的设计实际的界面。

2:为了方便单元测试

在运行时,大部分情况下,ViewModel会组合进提供Service的业务类。在简单的应用中,我们可以注入Service类的MOCK来进行单元测试,如果是这样,就可以避免提供多个ViewModel。但在有些应用中,如Silverlight应用中,服务由WerbService、WCF提供,就无法让客户端应用服务所支持的接口类,并且客户端的代码都是自动生成的,这样我们就需要提供多个ViewModel来支持单元测试。

3:为设计时提供模拟数据

考虑到VM需要存在多个,所以UI的VM需要存在一个基类,假设我的UI需要显示一个学生的列表,那么我的VM基类设计如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
public class MainPageVmBase
{
    public MainPageVmBase()
    {
        click = new DelegateCommand(OnClick);
    }
 
    public IStudent StudentService { get; set; }
    public IView View { get; set; }
 
    private ICommand click;
 
    public ICommand Click
    {
        get { return click; }
        set { click = value; }
    }
 
    void OnClick(object arg)
    {
        View.Title = arg as string;
        View.Show();
    }
 
    private List<Student> studets;
 
    public List<Student> Studets
    {
        get { return studets; }
        set { studets = value; }
    }
}

设计时的VM需要提供模拟数据,那么该VM为:

1
2
3
4
5
6
7
8
9
10
11
public class MainPageVmMock : MainPageVmBase
{
    public MainPageVmMock()
    {
        Studets = new List<Student>()
                          {
                              new Student() {Name = "d1", Age = 11},
                              new Student() {Name = "d2", Age = 22}
                          };
    }
}

要让设计时显式模拟数据,我们需要用到一个DesignHelpers类(该类来自于https://github.com/jeremiahredekop/):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
    public static class DesignHelpers
    {
        private static bool? _designMode;
        public static bool DesignMode
        {
            get
            {
                if (!_designMode.HasValue)
                {
#if !SILVERLIGHT
                    _designMode = new bool?(DesignerProperties.GetIsInDesignMode(new System.Windows.DependencyObject()));
#else
                    _isInDesignMode = new bool?(DesignerProperties.IsInDesignTool);
#endif
                }
                return _designMode.Value;
            }
        }
    }

借助于这个类的处理,这个时候我们在Expression Blend进行绑定的时候,就可以显式我们的模拟数据:

image

4:ViewModel FACTORY

每个UI绑定的VM实际都来自于VM FACTORY的类,如上面这个页面,我们绑定的就是MainPageVmFactory中的ViewModel属性。每个VmFactory也有自己的基类,如下:

1
2
3
4
5
6
7
8
9
public class ViewModelFactory<TViewModelRealBase>
    where TViewModelRealBase : class, new()
{
    public TViewModelRealBase ViewModel
    {
        get;
        set;
    }
}

MainPageVmFactory如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public class MainPageVmFactory : ViewModelFactory<MainPageVmBase>
{
    public MainPageVmFactory()
    {
        if (DesignHelpers.DesignMode == true)
        {
            this.ViewModel = new MainPageVmMock();
        }
        else
        {
            using (IUnityContainer container = new UnityContainer())
            {
                var section = (UnityConfigurationSection)ConfigurationManager.GetSection("unity");
                section.Configure(container, "containerOne");
                this.ViewModel = container.Resolve<MainPageVmBase>("MainPageVmReal");
            }
        }
    }
}

可以看到,运行时的VM我们通过unity注入的方式来得到,本文一开头已经说过了,之所以采用注入方式是为了单元测试方便。注入的是MainPageVmReal这个类型,它包含有实际提供数据的服务类,如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public class MainPageVmReal : MainPageVmBase
{
    public MainPageVmReal()
    {
        //Studets = new List<Student>()
        //                  {
        //                      new Student() {Name = "r1", Age = 11},
        //                      new Student() {Name = "r2", Age = 22}
        //                  };
        using (IUnityContainer container = new UnityContainer())
        {
            UnityConfigurationSection section = (UnityConfigurationSection)ConfigurationManager.GetSection("unity");
            section.Configure(container, "containerOne");
            IStudent service = container.Resolve<IStudent>("StudentService");
            Studets = service.GetAllStudent() as List<Student>;
        }
    }
}

MainPageVmReal中实际提供数据的服务类,可以是WCF的客户端代码,或者是任何别的东西,这里不是我们关注的重点,所以我们可以不用去管里面的那段注入代码。

运行时结果:

image

5:关于注入

注入这部分就很容易理解了,在配置处写入我们实际需要VM类型就可以了:

image

整体代码下载:WpfApplication20110811.rar

posted @   陆敏技  阅读(4983)  评论(4编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
Web Counter
Coupon for Contacts
点击右上角即可分享
微信分享提示