程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> ASP.NET >> 關於ASP.NET >> NBear - 支持基於操作符重載的強類型Where及OrderBy查詢

NBear - 支持基於操作符重載的強類型Where及OrderBy查詢

編輯:關於ASP.NET

從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", "[", "]", "@");
    }

  }

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved