`
linuxstuding
  • 浏览: 1222901 次
文章分类
社区版块
存档分类
最新评论

NBear - 支持基于操作符重载的强类型Where及OrderBy查询

 
阅读更多

  从v2.2.1版起,NBear开始支持强类型的实体查询语法。例如,我们可以以如下的语法查询需要的数据:

LocalUser[] users = gateway.Select<LocalUser>(_Entity.LocalUser.Id > 5 | _Entity.LocalUser.LoginId == "teddy", _Entity.LocalUser.Id.Desc & _Entity.LocalUser.LoginId.Asc);

  注意黑体部分,Select函数的两个参数分别为两个强类型表达式,一个是一组查询条件,另一个是排序条件。

  以上代码等价于:

LocalUser[] users = gateway.Select<LocalUser>("[Id] > @Id or [LoginId] = @LoginId", "[Id] desc, [LoginId]", new object[] { 5, "teddy" });

  我们可以看到,采用第一种语法的好处有:

  · 强类型,对于代码错误拥有编译器错误检查;

  · 更直观;

  · 独立与数据库特定的SQL语法,从而隔离了业务层和特定数据库的耦合;

  使用Entity Configurator生成强类型实体查询代码

  要让程序能够访问到_Entity命名空间下的这些强类型查询对象,需要先使用Entity Configurator生成强类型实体查询代码。只需在Entity Configurator中载入实体程序集,设置要对应的元数据,点击工具的Code->Generate EntityQuery Code菜单项,再将生成的代码复制到需要的任意程序集中,生成的代码及包含了所有实体的强类型查询对象。

  可用操作符

  NBear的像类型实体查询语法支持的操作符有:&(与),|(或),==(等于),!=(不等于),>(大于),<(小与),>=(大于等于),<=(小于等于),!(否),以及.Like()注意,Like是一个函数,可以以_Entity.User.LoginId.Like("teddy")这样的方式使用。

  对于OrderBy部分,唯一的可用操作符是&,即表示组合多个排序条件。

  代码示例

  下面的代码是使用Entity Configurator工具为使用Entity Configurator设置实体元数据、生成数据库创建脚本一文中所示的实体生成的强类型实体查询代码:

  namespace _Entity
  {
    public class AgentUser
    {
      public static PropertyItem LoginId = new PropertyItem("LoginId", "[", "]", "@");
      public static PropertyItem Id = new PropertyItem("Id", "[", "]", "@");
      public static PropertyItem Name = new PropertyItem("Name", "[", "]", "@");
      public static PropertyItem PrivilegeOwnerId = new PropertyItem("PrivilegeOwnerId", "[", "]", "@");
    }
  
    public class GhostUser
    {
      public static PropertyItem Id = new PropertyItem("Id", "[", "]", "@");
      public static PropertyItem Name = new PropertyItem("Name", "[", "]", "@");
      public static PropertyItem PrivilegeOwnerId = new PropertyItem("PrivilegeOwnerId", "[", "]", "@");
    }
  
    public class IdentableEntity
    {
      public static PropertyItem Id = new PropertyItem("Id", "[", "]", "@");
      public static PropertyItem Name = new PropertyItem("Name", "[", "]", "@");
    }
  
    public class IdFactory
    {
      public static PropertyItem NextId = new PropertyItem("NextId", "[", "]", "@");
    }
  
    public class LocalUser
    {
      public static PropertyItem Password = new PropertyItem("Password", "[", "]", "@");
      public static PropertyItem LoginId = new PropertyItem("LoginId", "[", "]", "@");
      public static PropertyItem Id = new PropertyItem("Id", "[", "]", "@");
      public static PropertyItem Name = new PropertyItem("Name", "[", "]", "@");
      public static PropertyItem PrivilegeOwnerId = new PropertyItem("PrivilegeOwnerId", "[", "]", "@");
    }
  
    public class Loginable
    {
      public static PropertyItem LoginId = new PropertyItem("LoginId", "[", "]", "@");
    }
  
    public class PasswordLoginable
    {
      public static PropertyItem Password = new PropertyItem("Password", "[", "]", "@");
      public static PropertyItem LoginId = new PropertyItem("LoginId", "[", "]", "@");
    }
  
    public class PrivilegeAssignable
    {
      public static PropertyItem PrivilegeOwnerId = new PropertyItem("PrivilegeOwnerId", "[", "]", "@");
    }
  
    public class User
    {
      public static PropertyItem Id = new PropertyItem("Id", "[", "]", "@");
      public static PropertyItem Name = new PropertyItem("Name", "[", "]", "@");
      public static PropertyItem PrivilegeOwnerId = new PropertyItem("PrivilegeOwnerId", "[", "]", "@");
    }
  
    public class UserGroup
    {
      public static PropertyItem Comment = new PropertyItem("Comment", "[", "]", "@");
      public static PropertyItem Id = new PropertyItem("Id", "[", "]", "@");
      public static PropertyItem Name = new PropertyItem("Name", "[", "]", "@");
      public static PropertyItem PrivilegeOwnerId = new PropertyItem("PrivilegeOwnerId", "[", "]", "@");
    }
  
  }

<!-- 分页 -->
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics