asp.net MVC5 website development Implementation of data storage layer function (c) _ Practical skills

Source: Internet
Author: User

The data storage layer is implemented in the project Ninesky.datalibrary, and the entire project has only one class repository.

Repository in the implementation of additions and deletions to query and other methods for business logic layer calls, the main functions as follows:

Specific steps

Add a reference to the Entity Framework

1, open the solution, select the project Ninesky.datalibrary, right click on the reference, select the management NuGet package.

Click EntityFramework in the Navigation tab of the NuGet Package Manager and click on the Install button on the right side of the bar.

Enter Entityframework.zh-hans in the search box to install the dummy Chinese resource pack.

Second, the realization of data warehousing class

Open the solution, select Project Ninesky.datalibrary, rename Class1.cs to Repository.cs, add a using System.Data.Entity to the document header, and rename the class name to public class Repository. Change to public class repository<t> where T:class

1, DbContext Property

Add the following code to the repository class DbContext DbContext {get; set;}

2. Constructor function

Add a high enough function to the class to pass the Dbcontex directly.

Public Repository ()
    {} public
    Repository (dbcontext dbcontext)
    {
      dbcontext = DbContext;
    

3. Find Entity methods

Find has an overload. Two methods can find entities based on IDs and on LAMDBA expressions respectively.

<summary>
    ///Lookup entity
    ///</summary>
    ///<param name= "ID" > Entity primary key value </param>
    ///<returns></returns> Public
    T Find (int ID)
    {return
      dbcontext.set<t> (). Find (ID);
    }

    <summary>
    ///Lookup entity
    ///</summary>
    ///<param name= "where" > query lambda expression </ param>
    ///<returns></returns> public
    T Find (expression<func<t,bool>> where)
    {return
      dbcontext.set<t> (). Singleordefault (where);
    

4. Find Entity List Method findlist

Findlist multiple overloads on demand

<summary>///Lookup Entity list///</summary>///<returns></returns> public IQueryable
    <T> findlist () {return dbcontext.set<t> (); ///<summary>///Lookup entity list///</summary>///<typeparam name= "TKey" > Sort Build Type </typepara m>///<param name= "order" > Sort expression </param>///<param name= "ASC" > Positive sequence </param>/// ;returns></returns> public iqueryable<t> findlist<tkey> (expression<func<t, TKey>> Order, bool ASC) {return ASC? Dbcontext.set<t> (). Order BY (Order): Dbcontext.set<t> ().
    OrderByDescending (order); ///<summary>///Lookup entity list///</summary>///<typeparam name= "TKey" > sort key Type </typepara m>///<param name= "order" > sort key </param>///<param name= "ASC" > Positive sequence </param>///<p Aram name= "number" > obtained record quantity </param>///<returnS></returns> public iqueryable<t> findlist<tkey> (expression<func<t, TKey>> order, BOOL Asc,int number) {return ASC? Dbcontext.set<t> (). Order BY (order). Take (number): Dbcontext.set<t> (). OrderByDescending (Order).
    Take (number); ///<summary>///Lookup entity list///</summary>///<param name= "where" > query lambda expression </param  >///<returns></returns> public iqueryable<t> findlist (expression<func<t, bool>> where) {return dbcontext.set<t> ().
    where (where); ///<summary>///Lookup entity list///</summary>///<param name= "where" > query lambda expression </param >///<param name= "number" > obtained record quantity </param>///<returns></returns> public iqueryabl E<t> findlist (expression<func<t, bool>> where, int number) {return dbcontext.set<t> (). W Here (where).
    Take (number);
}
    <summary>///Lookup Entity list///</summary>///<typeparam name= "TKey" > sort key Type </typeparam&
    Gt <param name= "where" > query lambda expression </param>///<param name= "order" > sort key </param>///<pa Ram name= "ASC" > whether positive sequence </param>///<returns></returns> public iqueryable<t> FINDLIST&LT;TK Ey> (Expression<func<t, bool>> where, expression<func<t, tkey>> order, bool ASC) {RET Urn ASC? Dbcontext.set<t> (). where (where). Order BY (Order): Dbcontext.set<t> (). where (where).
    OrderByDescending (order); ///<summary>///Lookup entity list///</summary>///<typeparam name= "TKey" > sort key Type </typepara
    m>///<param name= "where" > query lambda expression </param>///<param name= "order" > sort key </param> <param name= "ASC" > whether positive sequence </param>///<param name= "number" > acquired record quantity </param>///<return S></returns> public iqueryable<t> findlist<tkey> (expression<func<t, bool>> where, Expression<func<t, tkey>> order, bool ASC, int number) {return ASC? Dbcontext.set<t> (). where (where). Order BY (order). Take (number): Dbcontext.set<t> (). where (where). OrderByDescending (Order).
    Take (number);

 }

5. Find Entity Paging List method findpagelist

Findpagelist multiple overloads on demand

<summary>///Find a paging list///</summary>///<param name= "PageSize" > The number of records per page. Must be greater than 1</param>///<param name= "pageIndex" > page number. Home starting from 1, the page number must be greater than 1</param>///<param name= "Totalnumber" > Total records </param>///<returns></return S> public iqueryable<t> findpagelist (int pageSize, int pageIndex, out int totalnumber) {if (Pagein
      Dex < 1) PageIndex = 1;
      if (PageSize < 1) pageSize = 10;
      iqueryable<t> _list = dbcontext.set<t> (); Totalnumber = _list.
      Count (); Return _list. Skip ((pageIndex-1) * pageIndex).
    Take (pageSize); ///<summary>///Find a paging list///</summary>///<param name= "PageSize" > The number of records per page. Must be greater than 1</param>///<param name= "pageIndex" > page number. Home starting from 1, page number must be greater than 1</param>///<param name= "Totalnumber" > Total records </param>///<param name= "Order" > Sort keys </param>///<param name= "ASC" > whether positive sequence &LT;/PAram>///<returns></returns> public iqueryable<t> findpagelist<tkey> (int pageSize, int PageIndex, out int totalnumber, expression<func<t, tkey>> order, bool ASC) {if (PageIndex < 1) p
      Ageindex = 1;
      if (PageSize < 1) pageSize = 10;
      iqueryable<t> _list = dbcontext.set<t> (); _list = ASC? _list. Order BY (Order): _list.
      OrderByDescending (order); Totalnumber = _list.
      Count (); Return _list. Skip ((pageIndex-1) * pageIndex).
    Take (pageSize); ///<summary>///Find a paging list///</summary>///<param name= "PageSize" > The number of records per page. Must be greater than 1</param>///<param name= "pageIndex" > page number. Home starting from 1, page number must be greater than 1</param>///<param name= "Totalnumber" > Total records </param>///<param name= "where" > Query expression </param> public iqueryable<t> findpagelist (int pageSize, int pageIndex, out int totalnumber, Expressi On<func<t, bool>> where) {
      if (PageIndex < 1) PageIndex = 1;
      if (PageSize < 1) pageSize = 10; iqueryable<t> _list = dbcontext.set<t> ().
      where (where); Totalnumber = _list.
      Count (); Return _list. Skip ((pageIndex-1) * pageIndex).
    Take (pageSize); ///<summary>///Find a paging list///</summary>///<param name= "PageSize" > The number of records per page. Must be greater than 1</param>///<param name= "pageIndex" > page number. Home starting from 1, page number must be greater than 1</param>///<param name= "Totalnumber" > Total records </param>///<param name= "where" > 
    Query expression </param>///<param name= "order" > sort key </param>///<param name= "ASC" > Positive sequence </param> Public iqueryable<t> findpagelist<tkey> (int pageSize, int pageIndex, out int totalnumber, expression<f Unc<t, bool>> where, expression<func<t, tkey>> order, bool ASC) {if (PageIndex < 1) Pag
      Eindex = 1;
      if (PageSize < 1) pageSize = 10; iqueryable<t> _list = dbcontext.set<t> ().
      where (where); _list = ASC? _list. Order BY (Order): _list.
      OrderByDescending (order); Totalnumber = _list.
      Count (); Return _list. Skip ((pageIndex-1) * pageIndex).
    Take (pageSize);

 }

6, add the Entity method add

The Add method has an overload that the overloaded method can choose whether to save immediately

<summary>
    ///Add entity
    ///</summary>
    ///<param name= "entity" > Entity </param>
    / <returns> number of affected objects </returns> public
    int Add (T entity) {return
      Add (entity, true);

    <summary>
    ///Add entity
    ///</summary>
    ///<param name= "entity" > Entity </param>
    ///<param name= "Issave" > whether to save now </param>
    ///<returns> Returns the number of affected objects when "Issave" is true. Returns 0</returns> public
    int Add (T entity,bool issave)
    {
      dbcontext.set<t> () directly when false. ADD (entity);
      Return Issave? Dbcontext.savechanges (): 0;
    }

7, update the Entity method update

The Updae method has an overload that the overloaded method can choose whether to save immediately

#region

    update///<summary>
    ///updates entity "Save Now"
    ///</summary>
    ///<param name= "entity" > Entity </param>
    ///<returns> Returns the number of affected objects when "Issave" is true, and returns 0</returns> public directly when False
    int update (T entity)
    {return
      update (entity, true);
    }

    <summary>
    ///Update entity
    ///</summary>
    ///<param name= "entity" > Entity </param>
    ///<param name= "Issave" > whether to save now </param>
    ///<returns> Returns the number of affected objects when "Issave" is true. Returns 0</returns> public
    int Update (T entity, bool Issave)
    {
      dbcontext.set<t> () directly when false. Attach (entity);
      Dbcontext.entry<t> (entity). state = entitystate.modified;
      Return Issave? Dbcontext.savechanges (): 0;
    }

8. Delete Entity Method Delete

The Delete method has two overloads, one can choose whether to save immediately, and the other is to bulk delete

<summary>///Delete Entity "Save Now"///</summary>///<param name= "entity" > Entity </param>/
    <returns> number of affected objects </returns> public int Delete (T entity) {return Delete (entity, true); }///<summary>///delete entity///</summary>///<param name= "entity" > Entity </param>/ <param name= "Issave" > whether to save now </param>///<returns> returns the number of affected objects when "Issave" is true, returning directly to False 0</ returns> public int Delete (T entity,bool issave) {dbcontext.set<t> ().
      Remove (entity); Return Issave?
    Dbcontext.savechanges (): 0; 
    ///<summary>///Bulk Delete entity///</summary>///<param name= "Entities" > Entity Collection </param> <returns> number of affected objects </returns> public int Delete (ienumerable<t> entities) {Dbconte Xt. Set<t> ().
      RemoveRange (entities);
    return Dbcontext.savechanges ();

 }

9. Count method of statistic records

The Count method has an overload that can be counted based on an expression

<summary>
    ///record number
    ///</summary>
    ///<returns></returns> public
    int Count ()
    {return
      dbcontext.set<t> (). Count ();
    }

    <summary>
    ///record number
    ///</summary>
    ///<param name= "predicate" > Expression </param>
    ///<returns></returns> Public
    int Count (expression<func<t, bool>> predicate)
    {return
      dbcontext.set<t> (). Count (predicate);
    }

10. Is there

<summary>
    ///records exist
    ///</summary>
    ///<param name= "predicate" > Expression </param>
    ///<returns></returns> Public
    bool Iscontains (expression<func<t, bool>> predicate )
    {return
      Count (predicate) > 0;
    }

11. Save to Database

<summary>
    ///Save data "Use with ADD, upate, delete not immediately saved"
    ///</summary>
    ///<returns> The number of records affected </returns> public
    int Save ()
    {return
      dbcontext.savechanges ();
    }

Related Article

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.