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基类设计如下:

    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

转载于:https://www.cnblogs.com/luminji/archive/2011/08/11/2134410.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值