LiteMDA中,Load、Update和Delete都需要传入一个ICondition参数,ICondition包含了用于查询的条件,这样的条件,对于ORM来将,最终肯定是一组子条件的组合,比如Condition.Add("User.Name", OP.Equals, "teddy"),这样的语句有什么问题呢?主要是,条件Name需要手工输入,如果手误输了一个不存在的名称,也只有在运行时才会报错,所以,有必要提供一个现示的机制来,来避免这样的错误。双鱼座在一年以来我最好的创意中介绍了他在Kanas.Net中的一种实现,这里是我在LiteMDA中的实现,原理基本相似,就是在由辅助工具生成实体类的同时,生成一组我称为PropertyEnumerators的实体类属性词典。

以下的示例表示两个实体类User和Profile,每个User包含一个Profile属性。

UserPropertyEnumerator.cs

PropertyEnumerators For Entity Objects in LiteMDAusing System;
PropertyEnumerators For Entity Objects in LiteMDA
using System.Collections.Generic;
PropertyEnumerators For Entity Objects in LiteMDA
using System.Text;
PropertyEnumerators For Entity Objects in LiteMDA
PropertyEnumerators For Entity Objects in LiteMDA
namespace LiteMDA.Test.Entitys.PropertyEnumerators

ProfilePropertyEnumerator.cs

PropertyEnumerators For Entity Objects in LiteMDAusing System;
PropertyEnumerators For Entity Objects in LiteMDA
using System.Collections.Generic;
PropertyEnumerators For Entity Objects in LiteMDA
using System.Text;
PropertyEnumerators For Entity Objects in LiteMDA
PropertyEnumerators For Entity Objects in LiteMDA
namespace LiteMDA.Test.Entitys.PropertyEnumerators

Entity.cs

PropertyEnumerators For Entity Objects in LiteMDAusing System;
PropertyEnumerators For Entity Objects in LiteMDA
using System.Collections.Generic;
PropertyEnumerators For Entity Objects in LiteMDA
using System.Text;
PropertyEnumerators For Entity Objects in LiteMDA
PropertyEnumerators For Entity Objects in LiteMDA
namespace LiteMDA.Test.Entitys.PropertyEnumerators
}


通过以上这样的定义,我就可以以如下的方式来添加查询条件子句:

Condition.Add(Entity.User.Name, OP.Equals, "teddy").And(Entity.User.Profile.LiveCity, OP.Equals, "shanghai")

从而避免了以上提到“手误”可能导致的编译时不能发现的错误。

//文章结束

相关文章: