MVVM模式之:ViewModel Factory与注入

简介: 基于以下的理由,ViewModel也是需要多个,并且需要被注入的: 1:设计时和运行时需要为View提供不同的数据 简单来说,就是设计时需要模拟数据。界面设计开发人员需要进行绑定(包括支持Expression Blend绑定)做一些简单的处理,同时因为提供了模拟数据,UI人员可以更好的设计实际的界面。

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

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

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

2:为了方便单元测试

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

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

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

    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为:

    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/):

    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也有自己的基类,如下:

    public class ViewModelFactory<TViewModelRealBase>
        where TViewModelRealBase : class, new()
    {
        public TViewModelRealBase ViewModel
        {
            get;
            set;
        }
    }

MainPageVmFactory如下:

    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这个类型,它包含有实际提供数据的服务类,如下:

    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

Creative Commons License本文基于 Creative Commons Attribution 2.5 China Mainland License发布,欢迎转载,演绎或用于商业目的,但是必须保留本文的署名 http://www.cnblogs.com/luminji(包含链接)。如您有任何疑问或者授权方面的协商,请给我留言。
目录
相关文章
|
4月前
|
存储 前端开发
CreationExtras 来了,创建 ViewModel 的新方式
CreationExtras 来了,创建 ViewModel 的新方式
89 0
|
3月前
|
设计模式 存储 前端开发
MVC(Model-View-Controller)是一种软件设计模式,用于将应用程序的输入逻辑、业务逻辑和用户界面逻辑分离
【6月更文挑战第17天】**MVC模式**是软件设计模式,用于分离输入逻辑、业务逻辑和用户界面。模型处理数据和业务,视图展示数据,控制器协调两者响应用户请求。优点包括:关注点分离、提高开发效率、简化测试、支持多视图及便于大型项目管理。
41 3
|
2月前
LiveData和ViewModel源码学习
LiveData和ViewModel源码学习
|
4月前
|
前端开发
MVVM LiveData+DataBinding+Lifecycle+ViewModel架构
MVVM LiveData+DataBinding+Lifecycle+ViewModel架构
52 1
|
Android开发
Dagger Hilt - ViewModel的依赖注入及实现原理
Dagger Hilt VIewModel 依赖注入的原理
456 0
|
前端开发 Java 数据处理
【Spring MVC】(二)Controller、View之间的映射与参数传递
【Spring MVC】(二)Controller、View之间的映射与参数传递
457 0
|
JavaScript 前端开发
SAP Spartacus里的converter实例化逻辑
SAP Spartacus里的converter实例化逻辑
SAP Spartacus里的converter实例化逻辑
|
Android开发
【IOC 控制反转】Android 事件依赖注入 ( 事件依赖注入具体的操作细节 | 获取要注入事件的 View 对象 | 通过反射获取 View 组件的事件设置方法 )
【IOC 控制反转】Android 事件依赖注入 ( 事件依赖注入具体的操作细节 | 获取要注入事件的 View 对象 | 通过反射获取 View 组件的事件设置方法 )
249 0
|
前端开发
MVC4之ModelBinder-模型绑定
最近悟出来一个道理,在这儿分享给大家:学历代表你的过去,能力代表你的现在,学习代表你的将来。    十年河东十年河西,莫欺少年穷    学无止境,精益求精    最近在做自学MVC,遇到的问题很多,索性一点点总结下。
1086 0
|
Web App开发 前端开发 .NET
MVC ViewModel
ViewModel这个概念不只是在在MVC模式中有,你会在很多关于MVC、MVP、MVVM的文章中见到这个说法,并且这个概念在任何技术中都有可能提到,比如ASP.NET, Silverlight, WPF, or MVC... 现在我们来讨论如何在MVC中使用它。
932 0