在上一节中,我们了解了怎么通过特性让
QDORM认识我们创建的Model,但如果我们的项目中需创建的Model很多,而且每个Model都会有一些重复的属性,比如每个Model中都可能会有创建时间(CreateDate)属性,该属性只是在表单创建时赋值,修改时不再赋值 了。每个Model中还都可能会有一个修改时间(ModifyDate)属性,该属性表单创建时不赋值,修改的时候才会赋值。通过上一节的讲解,我们知道可以通过DbColumn特性的IsIgnoreInsert属性与IsIgnoreModify属性来实现,但如果要为每个Model都设置一遍,就太浪费时间了,这一节我们讲解QDORM的全局配置,以及怎么通过全属配置来实现这个效果。
相关的代码位置如下图:
QDORM可根据需要,扩展出多个全局配置,现在我们只实现了“新增时忽略的属性”和“修改时忽略的属性”两个全局配置项。
1,
IConstraintsBase接口该类没有任何实现,只是作为全局配置项的标识。
2,
NonCreateDataCommandGeneral类该类为IConstraintsBase的实现类,主要用于注册所有Model都需遵守的配置,代码如下:
/// <summary >
/// 所有创建数据库执行命令时约束
/// </summary >
public class NonCreateDataCommandGeneral : IConstraintsBase
{
private IList<string > nonAppendFields;
/// <summary >
/// 新增时不生成对应sql的属性
/// </summary >
public IList<string > NonAppendFields
{
get
{
if (nonAppendFields == null)
nonAppendFields = new List<string >();
return nonAppendFields;
}
}
private IList<string > nonModifyFields;
/// <summary >
/// 修改时不生成对应sql的属性
/// </summary >
public IList<string > NonModifyFields
{
get
{
if (nonModifyFields == null)
nonModifyFields = new List<string >();
return nonModifyFields;
}
}
/// <summary >
/// 注册新增时不生成对应sql的属性
/// </summary >
/// <param name="fieldName" ></param >
public void RegisterNonAppendField(params string[] fieldNames)
{
foreach (string s in fieldNames)
{
if (!NonAppendFields.Any(p = > p == s))
{
NonAppendFields.Add(s);
}
}
}
/// <summary >
/// 注册修改时不生成对应sql的属性
/// </summary >
/// <param name="fieldName" ></param >
public void RegisterNonModifyField(params string[] fieldNames)
{
foreach (string s in fieldNames)
{
if (!NonModifyFields.Any(p = > p == s))
{
NonModifyFields.Add(s);
}
}
}
}
3,
NonCreateDataCommandModel类该类为IConstraintsBase的实现类,主要用于注册单个Model都需遵守的配置,代码如下:
/// <summary >
/// 指定Model的创建数据库执行命令时约束
/// </summary >
public class NonCreateDataCommandModel : IConstraintsBase
{
private IList<KeyValuePair<string, IList<string > > > nonAppendFields;
/// <summary >
/// 新增时不生成对应sql的属性
/// </summary >
public IList<KeyValuePair<string, IList<string > > > NonAppendFields
{
get
{
if (nonAppendFields == null)
nonAppendFields = new List<KeyValuePair<string, IList<string > > >();
return nonAppendFields;
}
}
private IList<KeyValuePair<string, IList<string > > > nonModifyFields;
/// <summary >
/// 修改时不生成对应sql的属性
/// </summary >
public IList<KeyValuePair<string, IList<string > > > NonModifyFields
{
get
{
if (nonModifyFields == null)
nonModifyFields = new List<KeyValuePair<string, IList<string > > >();
return nonModifyFields;
}
}
/// <summary >
/// 注册新增时不生成对应sql的属性
/// </summary >
/// <param name="fieldName" ></param >
public void RegisterNonAppendField<T >(params string[] fieldNames)
{
string key = typeof(T).Name;
AddNonAppendField(key, fieldNames);
}
/// <summary >
/// 注册新增时不生成对应sql的属性
/// </summary >
/// <param name="fieldName" ></param >
public void RegisterNonAppendField(string modelName, params string[] fieldNames)
{
AddNonAppendField(modelName, fieldNames);
}
private void AddNonAppendField(string key, string[] fieldNames)
{
if (NonAppendFields.Any(p = > p.Key == key))
{
KeyValuePair<string, IList<string > > pair = nonAppendFields.Where(p = > p.Key == key).First();
foreach (string s in fieldNames)
{
pair.Value.Add(s);
}
}
else
{
NonAppendFields.Add(new KeyValuePair<string, IList<string > >(key, fieldNames.ToList()));
}
}
/// <summary >
/// 注册修改时不生成对应sql的属性
/// </summary >
/// <param name="fieldName" ></param >
public void RegisterNonModifyField<T >(params string[] fieldNames)
{
string key = typeof(T).Name;
AddNonModifyField(key, fieldNames);
}
/// <summary >
/// 注册修改时不生成对应sql的属性
/// </summary >
/// <param name="fieldName" ></param >
public void RegisterNonModifyField(string modelName, params string[] fieldNames)
{
AddNonModifyField(modelName, fieldNames);
}
private void AddNonModifyField(string key, string[] fieldNames)
{
if (NonModifyFields.Any(p = > p.Key == key))
{
KeyValuePair<string, IList<string > > pair = nonModifyFields.Where(p = > p.Key == key).First();
foreach (string s in fieldNames)
{
pair.Value.Add(s);
}
}
else
{
NonModifyFields.Add(new KeyValuePair<string, IList<string > >(key, fieldNames.ToList()));
}
}
}
4,
IConstraints接口该接口定义了所有配置的集合,以便我们在一个实现类中实现所有的配置。代码如下:
/// <summary >
/// 数据操作约束
/// </summary >
public
interface IConstraints
{
QDORM.Conventions.NonCreateDataCommandGeneral NonCreateDataCommandGeneral { get; }
QDORM.Conventions.NonCreateDataCommandModel NonCreateDataCommandModel { get; }
}
5,
Constraints类该类为IConstraints接口的实现类,代码如下:
public class Constraints : IConstraints
{
private NonCreateDataCommandGeneral nonCreateDataCommandGeneral;
/// <summary >
/// 所有创建数据库执行命令时约束
/// </summary >
public NonCreateDataCommandGeneral NonCreateDataCommandGeneral
{
get
{
if (nonCreateDataCommandGeneral == null)
nonCreateDataCommandGeneral = new NonCreateDataCommandGeneral();
return nonCreateDataCommandGeneral;
}
}
private NonCreateDataCommandModel nonCreateDataCommandModel;
/// <summary >
/// 指定的Model的创建数据库执行命令时约束
/// </summary >
public NonCreateDataCommandModel NonCreateDataCommandModel
{
get
{
if (nonCreateDataCommandModel == null)
nonCreateDataCommandModel = new NonCreateDataCommandModel();
return nonCreateDataCommandModel;
}
}
}
好了,有关全局配置的所有代码就是这些了,那么,我们要如何具体配置这些全局配置呢?
在实现化DataContext对象时,重写配置创建方法即可,代码如下:
/// <summary >
/// 数据库上下文
/// </summary >
public class DbConfig : DataContext
{
public DbConfig()
: base()
{
base.ReadConnectionStrings = DbConfigConnectionStrings;
base.WriteConnectionStrings =DbConfigConnectionStrings;
}
public override void OnModelCreating(QDORM.Conventions.IConstraints constraints)
{
//这些都是示例,配置所有Model的ModifyDate属性在新增时都不赋值
constraints.NonCreateDataCommandGeneral.RegisterNonAppendField("ModifyDate");
//这些都是示例,配置Sys_Parameter的Remark,PType属性在新增时不赋值
constraints.NonCreateDataCommandModel.RegisterNonAppendField<Sys_Parameter >("Remark", "PType");
//这些都是示例,配置所有Model的CreateDate属性在修改时都不赋值
constraints.NonCreateDataCommandModel.RegisterNonModifyField("CreateDate");
//这些都是示例,配置Sys_Parameter的PID,PValue属性在修改时不赋值
constraints.NonCreateDataCommandModel.RegisterNonModifyField<Sys_Parameter >("PID", "PValue");
}
}