抽象基础资料ViewModel模板

基础资料是ERP中一个模块。主要功能是相当于一个数据字典。最近在搭建一个简单的ERP的框架。需要将基础资料与单据的模板抽象出来,做成通用模板,以后需要新增基础资料和单据时,只需要继承相应的模板,增删改查,审核、弃审、打印等都不需要再写。
这篇文章主要介绍基础资料的模板搭建。
我们先建一个ViewModelBase,添加一些常用的工具栏命令。

public partial class ViewModelBase : INotifyPropertyChanged
{
    public ViewModelBase()
    {
        CloseCommand = new DelegateCommand(ExecuteClose);
        PrintCommand = new DelegateCommand(ExecutePrint, CanExecutePrint);
        SaveCommand = new DelegateCommand(ExecuteSave, CanExecuteSave);
        DeleteCommand = new DelegateCommand(ExecuteDelete, CanExecuteDelete);
        DetailCommand = new DelegateCommand(ExecuteDetail);
        NavigateCommand = new DelegateCommand(ExecuteNavigate);
        ShowHandInputCommand = new DelegateCommand(ExecuteShowHandInput);
        AddCommand = new DelegateCommand(ExecuteAdd);
        ModifyCommand = new DelegateCommand(ExecuteModify);
        ListCommand = new DelegateCommand(ExecuteList);
        MouseDoubleClickCommand = new DelegateCommand(ExecuteMouseDoubleClick);
        ReferBDCommand = new DelegateCommand(ExecuteReferBD);
        ExitCommand = new DelegateCommand(ExecuteExit);
        AuditCommand = new DelegateCommand(ExecuteAudit, CanExecuteAudit);
        UnAuditCommand = new DelegateCommand(ExecuteUnAudit, CanExecuteUnAduti);
    } #region 【命令】
    public DelegateCommand AddCommand { get; set; }

    /// 
    /// 保存命令
    /// 
    public DelegateCommand SaveCommand { get; set; }

    /// 
    /// 审核
    /// 
    public DelegateCommand AuditCommand { get; set; }

    /// 
    /// 反审核
    /// 
    public DelegateCommand UnAuditCommand { get; set; }

    /// 
    /// 打印命令
    /// 
    public DelegateCommand PrintCommand { get; set; }

    /// 
    /// 关闭命令
    /// 
    public DelegateCommand CloseCommand { get; set; }

    /// 
    /// 详情命令
    /// 
    public DelegateCommand DetailCommand { get; set; }

    /// 
    /// 删除命令
    /// 
    public DelegateCommand DeleteCommand { get; set; }

    /// 
    /// 导航命令
    /// 
    public DelegateCommand NavigateCommand { get; set; }

    /// 
    /// 手写命令
    /// 
    public DelegateCommand ShowHandInputCommand { get; set; }

    public DelegateCommand ModifyCommand { get; set; }

    public DelegateCommand ListCommand { get; set; }

    public DelegateCommand MouseDoubleClickCommand { get; set; }

    public DelegateCommand ReferBDCommand { get; set; }

    public DelegateCommand ExitCommand { get; set; }
    #endregion
 #region 【执行命令】

    public virtual void ExecuteSave(T_DATAFLOWSUBOPER para)
    {
        MessageService.Show("Save");
    }

    public virtual void ExecutePrint(T_DATAFLOWSUBOPER para)
    {
        MessageService.Show("Print");
    }

    public virtual void ExecuteClose(T_DATAFLOWSUBOPER para)
    {
        Messager.Messager.Default.Send("CloseChildWindow");
    }

    public virtual void ExecuteDetail()
    {
        throw new NotImplementedException();
    }

    public virtual void ExecuteDelete(T_DATAFLOWSUBOPER para)
    {
        MessageService.Show("Delete");
    }

    public virtual void ExecuteNavigate(string obj)
    {
        throw new NotImplementedException();
    }


    public virtual void ExecuteShowHandInput()
    {
        GlobalMethod.CallHandWriting();
    }

    public virtual void ExecuteAdd(T_DATAFLOWSUBOPER para)
    {
        MessageService.Show("Add");
    }

    public virtual void ExecuteModify(T_DATAFLOWSUBOPER para)
    {
    }

    public virtual void ExecuteList(T_DATAFLOWSUBOPER para)
    {

    }

    public virtual void ExecuteMouseDoubleClick()
    {

    }

    public virtual void ExecuteReferBD()
    {
        GlobalVariableBusiness.BaseData.ReferBaseData();
    }

    public virtual void ExecuteExit()
    {
        GlobalMethod.ApplicationExit();
    }

    public virtual void ExecuteAudit(T_DATAFLOWSUBOPER para)
    {

    }
    public virtual void ExecuteUnAudit(T_DATAFLOWSUBOPER para)
    {

    }
    #endregion

    #region 【命令逻辑】

    public virtual bool CanExecuteSave(T_DATAFLOWSUBOPER para)
    {
        return true;
    }

    public virtual bool CanExecutePrint(T_DATAFLOWSUBOPER para)
    {
        return true;
    }

    protected virtual bool CanExecuteUnAduti(T_DATAFLOWSUBOPER arg)
    {
        return true;
    }

    protected virtual bool CanExecuteAudit(T_DATAFLOWSUBOPER arg)
    {
        return true;
    }


    protected virtual bool CanExecuteDelete(T_DATAFLOWSUBOPER arg)
    {
        return true;
    }
    #endregion
    #region 【实现接口】
    public event PropertyChangedEventHandler PropertyChanged;
    public void RaisePropertyChanged(string name)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(name));
        }
    }
    #endregion
}

现在我们增加基础资料的通用ViewModel,继承自ViewModelBase,并重载命令的具体实现。

public class BDViewModelBase:ViewModelBase
{
private T _Master;
public T Master
{
get { return _Master; }
set
{
_Master = value;
RaisePropertyChanged("Master");
if (Master != null)
{
SetAuditImageShowStatus(Master.GetPropValue("FSTATUS"));
}
}
}
private List _Detail;

    public List Detail
    {
        get { return _Detail; }
        set { _Detail = value; RaisePropertyChanged("Detail"); }
    }
    private T _SelectedData;

    public T SelectedData
    {
        get { return _SelectedData; }
        set 
        { 
            _SelectedData = value; 
            RaisePropertyChanged("SelectedData");
            if (value != null)
            {
                Master = GlobalMethod.Clone(SelectedData);
                
            }
        }
    }
    #region 重载
    public override bool CanExecuteSave(T_DATAFLOWSUBOPER para)
    {
        if (BillState != NFMES.Core.Type.BillStateType.Complete)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    public override NFMES.Core.MVVM.ViewModelBase ReferData()
    {
        ReLoadData();
        return base.ReferData();
    }
    public override void ReLoadData()
    {
        Master = Activator.CreateInstance();
        Detail = BaseData.GetBaseDataList();
    }
    #region 工具栏操作
    public override void ExecuteAdd(T_DATAFLOWSUBOPER para)
    {
        CanEdit = true;
        Master = Activator.CreateInstance();
        BillState = NFMES.Core.Type.BillStateType.newbill;
    }
    public override void ExecuteModify(T_DATAFLOWSUBOPER para)
    {
        CanEdit = true;
        BillState = NFMES.Core.Type.BillStateType.modifybill;
    }
    public override void ExecuteSave(T_DATAFLOWSUBOPER para)
    {
        if (GlobalVariableBusiness.VaridateObj.CheckBaseDataNumberOnly(Master.GetTypeName(), Master.GetPropValue("FNUMBER").ToString())>0)
        {
            MessageService.Show("编号不可重复!");
            return;
        }
        int result = -1;
        switch (BillState)
        {
            case NFMES.Core.Type.BillStateType.newbill:
                result = BaseData.SaveBaseData(Master);

                MessageService.Show(NFMES.Core.Type.OperateType.save, result);
                break;

            case NFMES.Core.Type.BillStateType.modifybill:
                result = BaseData.UpdateBaseData(Master);

                MessageService.Show(NFMES.Core.Type.OperateType.update, result);
                break;
        }

        if (result >= 0)
        {
            Detail = new List(BaseData.GetBaseDataList());
            CanEdit = false;
            BillState = NFMES.Core.Type.BillStateType.Complete;
        }
    }

    public override void ExecuteDelete(T_DATAFLOWSUBOPER para)
    {
        if (GlobalVariableBusiness.VaridateObj.CheckBaseDataUsed(SubClass.FAddInKey, Convert.ToInt32(Master.GetPropValue("FID"))) == -1)
        {
            MessageService.Show("此条数据已被使用,不可删除!");
            return;
        }
        
        int result = BaseData.DeleteBaseData(Master);

        MessageService.Show(NFMES.Core.Type.OperateType.delete, result);
        if (result >= 0)
        {
            Master = Activator.CreateInstance();
            Detail = new List(BaseData.GetBaseDataList());

        }
    }
    #endregion
    #endregion
}

可以看到,我们的BDViewModelBase需要传递一个泛型,我们的模板类里,都是基于这个泛型来做增删改查的。
类中有三个属性,Master,Detail,SelectedData。MVVM模式,VM中的数据需要绑定到View中的。所以这三个数据,就是我们基础资料的数据源。后面加的所有基础资料View,绑定的数据源名称都是相同的。
Master是录入部分,Detail是列表部分,SelectedData是选中的列表行数据。
数据的DAL层用的Dapper框架,这个这边就不讨论了。我前面的文章有个对Dapper的封装的分享,也用在这个项目中了。

下面,我们基础资料的模板有了。以后变动的就是基础资料界面。如果基础资料的界面上的字段都一样,也可以做成通用的,不过很少。最常用的就是编码、名称字段。不过有时会关联其它基础资料。比如员工就需要关联角色。
下面我们看看一个标准的基础资料的View的写法。以部门资料为例:

xmlns:dxg="http://schemas.devexpress.com/winfx/2008/xaml/grid" xmlns:dxe="http://schemas.devexpress.com/winfx/2008/xaml/editors" xmlns:dxlc="http://schemas.devexpress.com/winfx/2008/xaml/layoutcontrol" x:Class="MESClient.Unit.Depart.Depart"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">

































现在,我们不需要再为部门单独做ViewModel了,我们直接用通用的BDViewModelBase。

public partial class Depart : UserControl
{
    public Depart()
        : this(null)
    {

    }

    public Depart(object para)
    {
        InitializeComponent();

        this.DataContext = new BDViewModelBase();
    }
}

T_Department是一个Model,与数据表的结构相同。
如果嫌写一个与表结构相同的Model麻烦,可以参考我前面的一篇文章,利用tt模板,自动将表结构生成Model实体。
如果我们需要增加一个职员的档案,界面如果和部门一样的话,那我们只需要改下this.DataContext的那行代码
this.DataContext = new BDViewModelBase();
只需要换一下实体。

抽象单据的方法与此类似,只是数据源和保存删除,数据刷新会有不同。

你可能感兴趣的:(抽象基础资料ViewModel模板)