PropertyEnumerators For Entity Objects in LiteMDA

来源:互联网 发布:手机怎样安装软件 编辑:程序博客网 时间:2024/05/21 11:13

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

using System;
using System.Collections.Generic;
using System.Text;

namespace LiteMDA.Test.Entitys.PropertyEnumerators
{
    
public class UserPropertyEnumerator
    
{
        
private string _Prefix = null;
        
private string _ID = "User.ID";
        
private string _Name = "User.Name";

        
public string ID
        
{
            
get
            
{
                
return (_Prefix != null && _Prefix.Length > 0 ? string.Format("{0}.{1}", _Prefix, _ID) : _ID);
            }

        }


        
public string Name
        
{
            
get
            
{
                
return (_Prefix != null && _Prefix.Length > 0 ? string.Format("{0}.{1}", _Prefix, _Name) : _Name);
            }

        }


        
public ProfilePropertyEnumerator Profile;

        
public UserPropertyEnumerator()
        
{
            
this.Profile = new ProfilePropertyEnumerator("User");
        }


        
public UserPropertyEnumerator(string prefix)
        
{
            _Prefix 
= prefix;
            
this.Profile = new ProfilePropertyEnumerator(string.Format("{0}.{1}", _Prefix, "User"));
        }

    }

}


ProfilePropertyEnumerator.cs

using System;
using System.Collections.Generic;
using System.Text;

namespace LiteMDA.Test.Entitys.PropertyEnumerators
{
    
public class ProfilePropertyEnumerator
    
{
        
private string _Prefix = null;
        
private string _ID = "Profile.ID";
        
private string _LiveCity = "Profile.LiveCity";

        
public string ID
        
{
            
get
            
{
                
return (_Prefix != null && _Prefix.Length > 0 ? string.Format("{0}.{1}", _Prefix, _ID) : _ID);
            }

        }


        
public string LiveCity
        
{
            
get
            
{
                
return (_Prefix != null && _Prefix.Length > 0 ? string.Format("{0}.{1}", _Prefix, _LiveCity) : _LiveCity);
            }

        }


        
public ProfilePropertyEnumerator()
        
{
        }


        
public ProfilePropertyEnumerator(string prefix)
        
{
            _Prefix 
= prefix;
        }


    }

}


Entity.cs

using System;
using System.Collections.Generic;
using System.Text;

namespace LiteMDA.Test.Entitys.PropertyEnumerators
{
    
public class Entity
    
{
        
public static PropertyEnumerators.UserPropertyEnumerator User = new LiteMDA.Test.Entitys.PropertyEnumerators.UserPropertyEnumerator();
        
public static PropertyEnumerators.ProfilePropertyEnumerator Profile = new LiteMDA.Test.Entitys.PropertyEnumerators.ProfilePropertyEnumerator();
    }

}


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

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

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

//文章结束