Heterogeneous Database Access Component Based on ef4.1 (III)

Source: Internet
Author: User
Next

Ef4.1-based heterogeneous database access component (ii) has completed the core components of this component:

  • Idbcontextstorage-dbcontext Repository
  • Idbcontextbuilder-dbcontext dynamic Builder
  • Idbcontextmanager-dbcontext Manager

Next, we will introduce the first article to provide a unified persistent data interface for the upper layer.

Irepository <t>

Irepository <t> is the unified persistent data interface that needs to be provided to the upper layer. Here you can find many examples. Not to mention, you can directly look at the class diagram:

It includes the following parts:

    • Irepository <t>-data persistence Interface
    • Sortorder enumeration-sorting Enumeration
    • Iunitofwork-Work Unit Interface
    • Unitofwork-work unit implementation class
    • Efrepository <t>-data persistence implementation base class

for iunitofwork and irepository you can find the Article in the garden , what needs to be noted here is -- how do we determine which business object belongs to which database? The key lies in efrepository In the constructor of this base class:

PublicEfrepository (StringConnectionstringname) {_ connectionstringname = connectionstringname ;}PrivateDbcontextDbcontext {Get{If(_ Context =Null){If(String. Isnullorempty (_ connectionstringname ))Throw newException("_ Connectionstringname is not assigned an μ value when the data persistence class is instantiated"); _ Context =Dbcontextmanager. Currentbykey (_ connectionstringname );}Return_ Context ;}}
Pagedlist <t> pagination

In irepository , we provide a method to retrieve data paging. The method I use is implemented using the pagedlist container and the extension method. The implementation of efrepository Code :

/// <Summary> ///Retrieve the paging list based on conditions and sorting Fields/// </Summary>PublicPagedlist<T> getpagedlist (Expression<Func<T,Bool> Predicate,StringOrderby,SortorderSortorder,IntPageindex,IntPagesize ){ReturnDbcontext. Set <t> (). Where (predicate). orderby (orderby, sortorder). topagedlist (pageindex, pagesize );}

Next let's take a look at pagedlist <t> and the extension method topagedlist.

Public class  Pagedlist <T>: List <T> { Public Pagedlist ( Ilist <T> items, Int Pageindex, Int Pagesize) {pagesize = pagesize; totalitemcount = items. Count; totalpagecount = ( Int ) Math . Ceiling (totalitemcount /( Double ) Pagesize); currentpageindex = pageindex; startrecordindex = (currentpageindex-1) * pagesize + 1; endrecordindex = totalitemcount> pageindex * pagesize? Pageindex * pagesize: totalitemcount; For ( Int I = StartRecordIndex-1; I <endrecordindex; I ++) {Add (items [I]);} Public Pagedlist ( Ienumerable <T> items, Int Pageindex, Int Pagesize, Int Totalitemcount) {addrange (items); totalitemcount = totalitemcount; totalpagecount = (Int ) Math . Ceiling (totalitemcount /( Double ) Pagesize); currentpageindex = pageindex; pagesize = pagesize; startrecordindex = (pageindex-1) * pagesize + 1; endrecordindex = totalitemcount> pageindex * pagesize? Pageindex * pagesize: totalitemcount ;} Public int Currentpageindex { Get ; Set ;} Public int Pagesize { Get ; Set ;} Public int Totalitemcount { Get ; Set ;} Public int Totalpagecount { Get ; Private set ;} Public int Startrecordindex { Get ; Private set ;} Public int Endrecordindex { Get ; Private set ;}}
 Public static class Pagelinqextensions { Public static  Pagedlist <T> topagedlist <t> ( This  Iqueryable <T> allitems, Int Pageindex, Int Pagesize ){ If (Pageindex <1) pageindex = 1; VaR Itemindex = (pageindex-1) * pagesize; VaR Pageofitems = allitems. Skip (itemindex). Take (pagesize );VaR Totalitemcount = allitems. Count (); Return new  Pagedlist <T> (pageofitems, pageindex, pagesize, totalitemcount );} Public static  Pagedlist <T> topagedlist <t> ( This  Ienumerable <T> allitems, Int Pageindex, Int Pagesize ){ If (Pageindex <1) pageindex = 1;VaR Itemindex = (pageindex-1) * pagesize; VaR Pageofitems = allitems. Skip (itemindex). Take (pagesize ); VaR Totalitemcount = allitems. Count (); Return new  Pagedlist <T> (pageofitems, pageindex, pagesize, totalitemcount );} Public static  Pagedlist <T> topagedlist <t> ( This  Iqueryable <T> allitems, Int Pageindex,Int Pagesize, Int Totalcount ){ If (Pageindex <1) pageindex = 1; VaR Totalitemcount = totalcount; Return new  Pagedlist <T> (allitems, pageindex, pagesize, totalitemcount );} Public static  Pagedlist <T> topagedlist <t> ( This  Ienumerable <T> allitems, Int Pageindex, Int Pagesize, Int Totalcount ){ If (Pageindex <1) pageindex = 1; VaR Totalitemcount = totalcount; Return new  Pagedlist <T> (allitems, pageindex, pagesize, totalitemcount );}}

About sorting

Add the extension method orderby directly.

 Public static  Iqueryable <T> orderby <t> (This  Iqueryable <T> source, String Propertyname, Sortorder Sortorder) Where T: Class { VaR Type = Typeof (T ); VaR Property = type. getproperty (propertyname ); If (Property = Null ) Throw new  Argumentexception ( "Propertyname" , "No? Save? In úres" ); VaR Param = Expression . Parameter (type, "P" ); Expression Propertyaccessexpression = Expression . Makememberaccess (Param, property ); VaR Orderbyexpression = Expression . Lambda (propertyaccessexpression, Param ); VaR Methodname = sortorder = Sortorder . Ascending? "Orderby" : "Orderbydescending" ; VaR Resultexp = Expression . Call ( Typeof ( Queryable ), Methodname, New  Type [] {Type, property. propertytype}, source. expression, Expression . Quote (orderbyexpression )); Return Source. provider. createquery <t> (resultexp );}
Multi-condition dynamic query

Reference: http://blogs.msdn.com/ B /meek/archive/2008/05/02/linq-to-entities-combining-predicates.aspx

Well, this is almost the way this component is. I will sort out the code and put it on codeplex. If you need it, please pay attention to it. Thank you!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.