Smark.Data 功能強大又靈活的Expression


    Expression對象是Smark.Data的核心對象,雖然Expression是個條件對象,但它所具備的功能是你想象不到的:),以下把Expression的功能列出來。

        public RESULT Avg<RESULT, Entity>(string field) where Entity : Smark.Data.Mappings.DataObject;
        public RESULT Avg<RESULT, Entity>(string field, bool DISTINCT) where Entity : Smark.Data.Mappings.DataObject;
        public RESULT Avg<RESULT, Entity>(string field, IConnectinContext cc) where Entity : Smark.Data.Mappings.DataObject;
        public RESULT Avg<RESULT, Entity>(string field, bool DISTINCT, IConnectinContext cc) where Entity : Smark.Data.Mappings.DataObject;
        public int Count<T>() where T : Smark.Data.Mappings.DataObject;
        public int Count<T>(IConnectinContext cc) where T : Smark.Data.Mappings.DataObject;
        public int Delete<T>() where T : Smark.Data.Mappings.DataObject;
        public int Delete<T>(IConnectinContext cc) where T : Smark.Data.Mappings.DataObject;
        public int Edit<T>(Action<T> handler) where T : Smark.Data.Mappings.DataObject, new();
        public int Edit<T>(params Field[] fields) where T : Smark.Data.Mappings.DataObject, new();
        public int Edit<T>(IConnectinContext cc, Action<T> handler) where T : Smark.Data.Mappings.DataObject, new();
        public int Edit<T>(IConnectinContext cc, params Field[] fields) where T : Smark.Data.Mappings.DataObject, new();
        public IList<T> List<T>() where T : Smark.Data.Mappings.DataObject, new();
        public IList<RESULT> List<T, RESULT>()
            where T : Smark.Data.Mappings.DataObject, new()
            where RESULT : new();
        public IList<RESULT> List<T, RESULT>(params string[] orderby)
            where T : Smark.Data.Mappings.DataObject, new()
            where RESULT : new();
        public IList<T> List<T>(params string[] orderby) where T : Smark.Data.Mappings.DataObject, new();
        public IList<T> List<T>(Region region) where T : Smark.Data.Mappings.DataObject, new();
        public IList<RESULT> List<T, RESULT>(Region region)
            where T : Smark.Data.Mappings.DataObject, new()
            where RESULT : new();
        public IList<RESULT> List<T, RESULT>(IConnectinContext cc, params string[] orderby)
            where T : Smark.Data.Mappings.DataObject, new()
            where RESULT : new();
        public IList<T> List<T>(IConnectinContext cc, params string[] orderby) where T : Smark.Data.Mappings.DataObject, new();
        public IList<RESULT> List<T, RESULT>(IConnectinContext cc, Region region)
            where T : Smark.Data.Mappings.DataObject, new()
            where RESULT : new();
        public IList<T> List<T>(IConnectinContext cc, Region region) where T : Smark.Data.Mappings.DataObject, new();
        public IList<T> List<T>(Region region, params string[] orderby) where T : Smark.Data.Mappings.DataObject, new();
        public IList<RESULT> List<T, RESULT>(Region region, params string[] orderby)
            where T : Smark.Data.Mappings.DataObject, new()
            where RESULT : new();
        public IList<T> List<T>(IConnectinContext cc, Region region, params string[] orderby) where T : Smark.Data.Mappings.DataObject, new();
        public IList<RESULT> List<T, RESULT>(IConnectinContext cc, Region region, params string[] orderby)
            where T : Smark.Data.Mappings.DataObject, new()
            where RESULT : new();
        public T ListFirst<T>() where T : Smark.Data.Mappings.DataObject, new();
        public RESULT ListFirst<T, RESULT>()
            where T : Smark.Data.Mappings.DataObject, new()
            where RESULT : new();
        public RESULT ListFirst<T, RESULT>(IConnectinContext cc)
            where T : Smark.Data.Mappings.DataObject, new()
            where RESULT : new();
        public T ListFirst<T>(IConnectinContext cc) where T : Smark.Data.Mappings.DataObject, new();
        public T ListFirst<T>(params string[] orderby) where T : Smark.Data.Mappings.DataObject, new();
        public RESULT ListFirst<T, RESULT>(params string[] orderby)
            where T : Smark.Data.Mappings.DataObject, new()
            where RESULT : new();
        public T ListFirst<T>(IConnectinContext cc, params string[] orderby) where T : Smark.Data.Mappings.DataObject, new();
        public RESULT ListFirst<T, RESULT>(IConnectinContext cc, params string[] orderby)
            where T : Smark.Data.Mappings.DataObject, new()
            where RESULT : new();
        public RESULT Max<RESULT, Entity>(string field) where Entity : Smark.Data.Mappings.DataObject;
        public RESULT Max<RESULT, Entity>(string field, bool DISTINCT) where Entity : Smark.Data.Mappings.DataObject;
        public RESULT Max<RESULT, Entity>(string field, IConnectinContext cc) where Entity : Smark.Data.Mappings.DataObject;
        public RESULT Max<RESULT, Entity>(string field, bool DISTINCT, IConnectinContext cc) where Entity : Smark.Data.Mappings.DataObject;
        public RESULT Min<RESULT, Entity>(string field) where Entity : Smark.Data.Mappings.DataObject;
        public RESULT Min<RESULT, Entity>(string field, bool DISTINCT) where Entity : Smark.Data.Mappings.DataObject;
        public RESULT Min<RESULT, Entity>(string field, IConnectinContext cc) where Entity : Smark.Data.Mappings.DataObject;
        public RESULT Min<RESULT, Entity>(string field, bool DISTINCT, IConnectinContext cc) where Entity : Smark.Data.Mappings.DataObject;
        public RESULT Sum<RESULT, Entity>(string field) where Entity : Smark.Data.Mappings.DataObject;
        public RESULT Sum<RESULT, Entity>(string field, bool DISTINCT) where Entity : Smark.Data.Mappings.DataObject;
        public RESULT Sum<RESULT, Entity>(string field, IConnectinContext cc) where Entity : Smark.Data.Mappings.DataObject;
        public RESULT Sum<RESULT, Entity>(string field, bool DISTINCT, IConnectinContext cc) where Entity : Smark.Data.Mappings.DataObject;

    以上就是Expression所具備的功能,它的工作可以完成數據查詢,統計匯總,修改和刪除等操作。當你用Smark.Data進行數據訪問的時候相信大部分都是在和這個Expression打交道。下面詳細地介紹它的每一個功能。

數據查詢

當我們new一個Expression出來的時候就可以進行相關操作,只是該操作並不帶上條件針對全表操作。

var employees = exp.List<Employees>();

獲取所有雇員,不過實現查詢就沒這么簡單,有可能加上條件獲取某頁數據加上分頁等。

                Expression exp = new Expression();
                if (ProductName != null)
                {
                    exp &= Modules.Product.productName.Like(ProductName + "%");
                    UrlParams.Add("productname", ProductName);
                }
                if (PriceFrom != null)
                {
                    exp &= Modules.Product.unitPrice >= PriceFrom;
                    UrlParams.Add("pricefrom", PriceFrom.ToString());
                }
                if (PriceTo != null)
                {
                    exp &= Modules.Product.unitPrice <= PriceTo;
                    UrlParams.Add("priceTo", PriceTo.ToString());
                }
                DataPage.PageSize = 10;
                DataPage.RecordCount = exp.Count<Modules.Product>();
                Records = exp.List<Modules.Product>(new Region(DataPage.PageIndex, DataPage.PageSize),OrderField);

以上是一個比較常用的查詢,根據情況添加相應的查詢條件,統計相關條件的記錄數並獲取對應頁的記錄數。

數據刪除

刪除一般會執行SQL或在組件中刪除對象,但在Smark.Data中Expression可以輕松完成相應的工作。

        Expression exp = new Expression();
        exp.Delete<Employee>();

 刪除Employee所有對象,實際情況更多的是基於條件的刪除如下:

    (Modules.Employee.city == "gz").Delete<Modules.Employee>();

這樣我們就可以把某個城市的employee刪除了

數據更新

    Expression exp = new Expression();
    exp.Edit<Modules.Employee>(o => { o.City = "bbq"; });
    (Modules.Employee.city == "gz").Edit<Modules.Employee>(o => { o.City = "bbq"; });

基於同樣的方式就能進行數據編輯

Expression能處理怎樣的條件?

Expression所提供的條件組合非常靈活,重載了|和&使條件編寫可以更好地接近實際SQL的方式,對於一此運算的多樣性也利用得很好.

 qual = (Qualification.sellerID == qinfo.SellerID & Qualification.storeID == qinfo.StoreID).ListFirst<Qualification>();
                Expression exp = new Expression();
                if (EmployeeID != null)
                    exp &= Modules.Order.employeeID.At() == EmployeeID;
                if (CustomerID != null)
                    exp &= Modules.Order.customerID.At() == CustomerID;
                if (OrderDateFrom != null)
                    exp &= Modules.Order.orderDate >= OrderDateFrom;
                if (OrderDateTo != null)
                    exp &= Modules.Order.orderDate <= OrderDateTo;
                if (RequiredDateFrom != null)
                    exp &= Modules.Order.requiredDate >= RequiredDateFrom;
                if (RequiredDateTo != null)
                    exp &= Modules.Order.requiredDate <= RequiredDateTo;
   (Modules.Employee.employeeID == new int[] {2,4,5 }).Delete<Modules.Employee>();
   (Modules.Employee.employeeID != new int[] {2,4,5 }).Delete<Modules.Employee>();
   Modules.Order.orderDate["1997-1-1", "1997-2-1"].List<Modules.Order>();
(Modules.Order.employeeID== Modules.Employee.employeeID[Modules.Employee.city==new []{"gz","sz"}]).List<Modules.Order>();

Expression對象是非常靈活,但它的作用建是立在實體描述<T>上,Smark.Data的實體描述也非常靈活除了描述表外,還能描述關聯查詢,匯總統計結合Expression對象可以滿足大部分實際應用的需要。在下一章節會講解實體對象的描述。

Smark.Data是基於 Apache License 2.0 (Apache)協議的開源組件,詳情可以到http://www.ikende.com/了解


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM