测试:
1)实体类,这个类没有采用元数据匹配的方式,而是采用专门的类来存储Map信息,主要是为了进行表达式书写
public class TestEntity { private static readonly TableInfo _TableInfo = new TableInfo(){ TableName="EEEE"}; public static TableInfo TableInfo { get { return _TableInfo; } } private static readonly FieldInfo _Id = new FieldInfo() { FieldName = "id" }; public static FieldInfo Id { get { return TestEntity._Id; } } private static readonly FieldInfo _Name = new FieldInfo() { FieldName = "Name" }; public static FieldInfo Name { get { return TestEntity._Name; } } }
2)测试类
public class TestExp { public static string Test() { DbExpression theQuery = new DbExpression(); theQuery.Select(AliasExp.T1[TestEntity.Id], AliasExp.T1[TestEntity.Name]) .From(AliasExp.T1[TestEntity.TableInfo]) .Where(AliasExp.T1[TestEntity.Id] > ConstExp<int>.C("Name", 100)); return theQuery.Expression; } }
上面的表达式和参数就直接可以给Command来执行,我只做了select,其实Update,Delete表达式都是可以的。当然,如果继续重载操作符,可以把常量表达式除掉,可以像一般书写那种方式来书写,比如:A>100,A="100".这里采用了偷懒的方式。当然要完全模拟还是有困难,因此表达式也需要保留直接写SQL和设置参数的功能。
这种表达式的优点是跟写SQL类似,但全部针对实体,可以保持一致性,而且还可以借助VS的智能提示和语法检查功能。缺点是不太纯粹,性能上也会有损失。
输出结果,大家可以自己动手运行来看.
PS:CSDN的这个编辑器有点问题,稍微久点(1分钟不到)就不能保存了.
PS:希望C#能支持自定义操作符号,这样就可以模拟出更纯粹的表达式.