V.CodeGenerator WPF代码生成器--Model基类
前言
前言:
受 WTM 的影响,想自己尝试写一个自动生成WPF项目的代码生成器
本文主要用于介绍基础库中作者自定义的一些<Model基类>的使用。
作者的功底还不是很成熟,请大家多多包涵。
一、引用Vampirewal.Core基础库
二、使用
内容比较简单,直接上代码吧
NotifyBase(1.X和2.X通用)
WPF中属性变化通知封装的基类
本基础库中的ViewModelBase也继承了该基类
/// <summary>
/// 属性通知基类
/// </summary>
public class NotifyBase : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
/// <summary>
/// 属性变化通知
/// </summary>
/// <param name="propName"></param>
public void DoNotify([CallerMemberName] string propName = "")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propName));
}
/// <summary>
/// 属性变化通知
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="field"></param>
/// <param name="value"></param>
/// <param name="propName"></param>
public void Set<T>(ref T field,T value,[CallerMemberName]string propName = "")
{
if (EqualityComparer<T>.Default.Equals(field,value))
{
return;
}
field = value;
DoNotify(propName);
}
/// <summary>
/// 带<paramref name="Action"/>的属性变化通知 2022-3-9 新增
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="field"></param>
/// <param name="value"></param>
/// <param name="OnChanged">属性变化的时候执行的方法</param>
/// <param name="propName"></param>
public void ActionSet<T>(ref T field, T value,Action<T> OnChanged=null, [CallerMemberName] string propName = "")
{
if (EqualityComparer<T>.Default.Equals(field, value))
{
return;
}
field = value;
OnChanged?.Invoke(value);
DoNotify(propName);
}
}
版本1.X----适用于EFCore
1、TopBaseModel
/// <summary>
/// model基类
/// </summary>
public class TopBaseModel:NotifyBase
{
#region 属性
private string _id;
/// <summary>
/// Id
/// </summary>
[Key]
public string ID
{
get
{
if (string.IsNullOrEmpty(_id))
{
_id = Guid.NewGuid().ToString();
}
return _id;
}
set
{
_id = value;
DoNotify();
}
}
private DateTime? _CreateTime;
/// <summary>
/// 创建时间
/// </summary>
[Column("CreateTime")]
public DateTime? CreateTime
{
get
{
if (_CreateTime == null)
{
_CreateTime = DateTime.Now;
}
return _CreateTime;
}
set
{
_CreateTime = value;
DoNotify();
}
}
private string _CreateBy;
/// <summary>
/// 创建人
/// </summary>
[Column("CreateBy")]
public string CreateBy
{
get { return _CreateBy; }
set { _CreateBy = value;DoNotify(); }
}
private bool _Check;
/// <summary>
/// 是否选中
/// 标识当前行数据是否被选中
/// </summary>
[NotMapped]
[JsonIgnore]
public bool Checked { get=> _Check; set { _Check = value;DoNotify(); } }
#endregion
#region 公共方法
/// <summary>
/// 获取当前model的ID
/// </summary>
/// <returns></returns>
public object GetID()
{
var idpro = this.GetType().GetProperties().Where(x => x.Name.ToLower() == "id").FirstOrDefault();
var id = idpro.GetValue(this);
return id;
}
/// <summary>
/// 获取父ID
/// </summary>
/// <returns></returns>
public object GetParentID()
{
var idpro = this.GetType().GetSingleProperty("ParentId");
var id = idpro.GetValue(this) ?? "";
return id;
}
#endregion
}
2、BaseModel
提供一个IBaseModel接口,可以自行实现。
该基类主要用于提供更新人和更新时间属性
/// <summary>
/// BaseModel接口
/// </summary>
public interface IBaseModel
{
private DateTime? _UpdateTime;
/// <summary>
/// 更新时间
/// </summary>
public DateTime? UpdateTime
{
get => _UpdateTime; set
{
_UpdateTime = value;
DoNotify();
}
}
private string _UpdateBy;
/// <summary>
/// 更新人
/// </summary>
public string UpdateBy
{
get => _UpdateBy; set
{
_UpdateBy = value;
DoNotify();
}
}
}
/// <summary>
/// 需要继承
/// </summary>
public abstract class BaseModel:TopBaseModel, IBaseModel
{
/// <summary>
/// 更新时间
/// </summary>
public DateTime? UpdateTime { get ; set; }
/// <summary>
/// 更新人
/// </summary>
public string UpdateBy { get ; set ; }
}
3、TreeBaseModel
树形模型基类
/// <summary>
/// 树形model基类
/// </summary>
/// <typeparam name="T">需要使用到树形的model</typeparam>
public class TreeBaseModel<T>:TopBaseModel
{
/// <summary>
/// 构造函数
/// </summary>
public TreeBaseModel()
{
Childs = new ObservableCollection<T>();
}
private string _ParentId;
/// <summary>
/// 父ID
/// </summary>
public string ParentId { get => _ParentId; set { _ParentId = value; DoNotify(); } }
/// <summary>
/// 父类
/// </summary>
//[JsonIgnore]
//public T Parent { get; set; }
/// <summary>
/// 子集
/// </summary>
[InverseProperty("Parent")]
[NotMapped]
public ObservableCollection<T> Childs { get; set; }
/// <summary>
/// 是否存在子项
/// </summary>
[NotMapped]
public bool HasChildren
{
get
{
return Childs?.Any() == true;
}
}
}
版本2.X----适用于SqlSugar
1、TopModel(Model基类)
public class TopModel : NotifyBase
{
private bool _Check;
/// <summary>
/// 是否选中
/// 标识当前行数据是否被选中
/// </summary>
[SugarColumn(IsIgnore =true)]
[JsonIgnore]
public bool Checked { get => _Check; set { _Check = value; DoNotify(); } }
}
2、BillBaseModel
/// <summary>
/// 单据模型基类
/// </summary>
public class BillBaseModel : TopModel
{
#region 属性
private string _BillId;
/// <summary>
/// Id
/// </summary>
[SugarColumn(IsPrimaryKey = true,IsNullable =false, ColumnDescription = "主键")]
[ExportExcelProperty(SortId =1)]//是否属于导出excel的属性,后面可以看特性的介绍
public string BillId
{
get
{
if (string.IsNullOrEmpty(_BillId))
{
_BillId = Guid.NewGuid().ToString();
}
return _BillId;
}
set
{
_BillId = value;
DoNotify();
}
}
private DateTime? _CreateTime;
/// <summary>
/// 创建时间
/// </summary>
[SugarColumn(IsNullable =true,IsOnlyIgnoreUpdate =true)]
public DateTime? CreateTime
{
get
{
if (_CreateTime == null)
{
_CreateTime = DateTime.Now;
}
return _CreateTime;
}
set
{
_CreateTime = value;
DoNotify();
}
}
private string _CreateBy;
/// <summary>
/// 创建人
/// </summary>
[SugarColumn(IsNullable = true, IsOnlyIgnoreUpdate = true)]
public string CreateBy
{
get { return _CreateBy; }
set { _CreateBy = value; DoNotify(); }
}
private DateTime? _UpdateTime;
/// <summary>
/// 更新时间
/// </summary>
[SugarColumn(IsNullable = true, IsOnlyIgnoreInsert = true)]
public DateTime? UpdateTime
{
get
{
if (_UpdateTime == null)
{
_UpdateTime = DateTime.Now;
}
return _UpdateTime;
}
set
{
_UpdateTime = value;
DoNotify();
}
}
private string _UpdateBy;
/// <summary>
/// 更新人
/// </summary>
[SugarColumn(IsNullable = true, IsOnlyIgnoreInsert = true)]
public string UpdateBy
{
get => _UpdateBy; set
{
_UpdateBy = value;
DoNotify();
}
}
#endregion
}
3、DetailBaseModel
/// <summary>
/// 明细数据基类
/// </summary>
public class DetailBaseModel:TopModel
{
private string _DtlId;
/// <summary>
/// 明细ID
/// </summary>
[SugarColumn(IsPrimaryKey =true,IsNullable =false, ColumnDescription = "主键")]
public string DtlId
{
get
{
if (string.IsNullOrEmpty(_DtlId))
{
_DtlId = Guid.NewGuid().ToString();
}
return _DtlId;
}
set
{
_DtlId = value;
DoNotify();
}
}
private string _BillId;
/// <summary>
/// 关联单据Id
/// </summary>
[SugarColumn( IsNullable = false)]
public string BillId
{
get
{
return _BillId;
}
set
{
_BillId = value;
DoNotify();
}
}
}
4、TreeDetailBaseModel
/// <summary>
/// 树形明细基类
/// </summary>
/// <typeparam name="T"></typeparam>
public class TreeDetailBaseModel<T>:DetailBaseModel where T : DetailBaseModel
{
public TreeDetailBaseModel()
{
//构造函数
Childs=new ObservableCollection<T>();
}
private T _Parent;
/// <summary>
/// 父类
/// </summary>
public T Parent
{
get { return _Parent; }
set { _Parent = value;DoNotify(); }
}
private string _ParentId;
/// <summary>
/// 父ID
/// </summary>
public string ParentId
{
get { return _ParentId; }
set { _ParentId = value; DoNotify(); }
}
/// <summary>
/// 子集
/// </summary>
public ObservableCollection<T> Childs { get; set; }
}
5、DetailItemChangeBaseModel
基类描述:继承自该基类的子类,可通过在属性上添加特性[ListenDetailChange] ,让框架自动储存旧值,在后续进行修改之后,通过框架提供的方法或者产生了修改的数据。
//DTO模型
public class testDetailModel2DTO:TopModel
{
private string _DName;
public string DName
{
get { return _DName; }
set { _DName = value; DoNotify(); }
}
private bool _IsDelete = false;
public bool IsDelete { get => _IsDelete; set { _IsDelete = value; DoNotify(); } }
}
//继承DetailItemChangeBaseModel的model
public class TestDetailModel2 : DetailItemChangeBaseModel<testDetailModel2DTO>
{
public TestDetailModel2(testDetailModel2DTO dto) :base(dto)
{
}
private string _DName;
[ListenDetailChange]//★在需要监听数据变化的属性上,添加该特性,会对该属性的值进行判断是否有变化
public string DName
{
get { return _DName; }
set { _DName = value; DoNotify(); }
}
private bool _IsDelete=false;
public bool IsDelete { get=> _IsDelete; set { _IsDelete=value; DoNotify(); } }
//重写该方法,在这里进行数据的赋值
public override void LoadDTO(testDetailModel2DTO dto)
{
this.DName = dto.DName;
}
}
使用
testDetailModel2DTO dto = new testDetailModel2DTO()
{
DName = "abc"
};
TestDetailModel2 model = new TestDetailModel2(dto);
model.DName = "asd";
if (model.CompareSourceValue())
{
//数据有变化
}
else
{
//数据无变化
}