Build a three-tier General DAL Class Based on Linq

Source: Internet
Author: User

Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Using System. Data;
Using System. Data. Linq;
Using Models;

Namespace DAL
{
Public class CommandDAL <T> where T: class
{
Public DataProjectDataContext db;

Public CommandDAL ()
{
// Each user generates an independent TestClassDataContext object
// Int count = HttpContext. Current. Session. Count;
// If (HttpContext. Current. Session ["Datas"] = null)
//{
Db = new DataProjectDataContext (System. Configuration. ConfigurationManager. ConnectionStrings ["Constr"]. ConnectionString );
// HttpContext. Current. Session ["Datas"] = db;
//}
// Else
//{
// Db = HttpContext. Current. Session ["Datas"] as JCPPSContext;
//}
}

/// <Summary>
/// Insert a piece of data into the database
/// </Summary>
/// <Param name = "obj"> data to be inserted </param>
/// <Returns> </returns>
Public T Insert (T obj)
{
Try
{

Db. GetTable <T> (). InsertOnSubmit (obj );
Db. SubmitChanges ();
}
Catch (Exception ex)
{

Throw new Exception (ex. Message );
}
Return obj;
}

/// <Summary>
/// Insert multiple data entries into the database
/// </Summary>
/// <Param name = "TEntities"> set of data to be inserted </param>
Public void InsertAll (IEnumerable <T> TEntities)
{
Try'
{
Db. GetTable <T> (). InsertAllOnSubmit (TEntities );
Db. SubmitChanges ();
}
Catch (Exception ex)
{

Throw new Exception (ex. Message );
}

}

/// <Summary>
/// Modify a piece of data based on the queried data
/// </Summary>
/// <Param name = "obj"> object to be modified </param>
/// <Param name = "action"> assign values to the object to be modified by expression </param>
Public void Update (T obj, Action <T> action)
{
Try
{
Action (obj );
Db. SubmitChanges ();
}
Catch (Exception ex)
{

Throw new Exception (ex. Message );
}
}

/// <Summary>
///????
/// </Summary>
/// <Param name = "obj"> </param>
Public void Update (T obj)
{
Try
{
Db. SubmitChanges ();
}
Catch (Exception ex)
{

Throw new Exception (ex. Message );
}
}

/// <Summary>
/// Find the desired data
/// </Summary>
/// <Param name = "s"> Search Condition expression </param>
/// <Returns> returns a group of object classes </returns>
Public List <T> Select (System. Linq. Expressions. Expression <Func <T, bool> expression)
{
Try
{
Return db. GetTable <T> (). Where <T> (expression). ToList ();
}
Catch (Exception ex)
{

Throw new Exception (ex. Message );
}
}

///// <Summary>
//// Find the desired data
///// </Summary>
///// <Param name = "s"> Search Condition expression </param>
///// <Returns> returns a group of entity classes </returns>
// Public List <T> Select (System. linq. expressions. expression <Func <T, bool> expression, System. linq. expressions. expression <Func <T, bool> expression1, bool bol)
//{
// Try
//{
// Return db. GetTable <T> (). Where <T> (expression). OrderBy (expression1). ToList ();
//}
// Catch (Exception ex)
//{

// Throw new Exception (ex. Message );
//}
//}

/// <Summary>
/// Locate the desired data and perform paging search
/// </Summary>
/// <Param name = "expression"> query condition </param>
/// <Param name = "pageindex"> current page, the first page starts from 0 </param>
/// <Param name = "pagesize"> data displayed on each page </param>
/// <Param name = "count"> total number of records </param>
/// <Returns> </returns>
Public List <T> Select (System. Linq. Expressions. Expression <Func <T, bool> expression, int pageindex, int pagesize, ref int count)
{
Try
{
Count = db. GetTable <T> (). Where <T> (expression). ToList (). Count ();
Return db. GetTable <T> (). Where <T> (expression). Skip (pageindex * pagesize). Take (pagesize). ToList ();
}
Catch (Exception ex)
{

Throw new Exception (ex. Message );
}
}

/// <Summary>
/// Find all the data
/// </Summary>
/// <Param name = "s"> data type to be searched </param>
/// <Returns> returns a group of object classes </returns>
Public List <T> SelectAll (int pageindex, int pagesize, ref int count)
{
Try
{
Count = db. GetTable <T> (). ToList (). Count ();
Return db. GetTable <T> (). Skip (pageindex * pagesize). Take (pagesize). ToList ();
}
Catch (Exception ex)
{

Throw new Exception (ex. Message );
}
}

/// <Summary>
/// Find all the data
/// </Summary>
/// <Param name = "s"> data type to be searched </param>
/// <Returns> returns a group of object classes </returns>
Public List <T> SelectAll ()
{
Try
{
Return db. GetTable <T> (). ToList ();
}
Catch (Exception ex)
{

Throw new Exception (ex. Message );
}
}

/// <Summary>
/// Search for a piece of data based on the Condition
/// </Summary>
/// <Param name = "s"> Search Condition expression </param>
/// <Returns> returns an object class </returns>
Public T SelectSingle (System. Linq. Expressions. Expression <Func <T, bool> expression)
{
Try
{
Return db. GetTable <T> (). Where <T> (expression). FirstOrDefault ();
}
Catch (Exception ex)
{

Throw new Exception (ex. Message );
}
}

/// <Summary>
/// Delete a piece of data from the database
/// </Summary>
/// <Param name = "obj"> data to be deleted </param>
Public void Delete (T obj)
{
Try
{
Db. GetTable <T> (). DeleteOnSubmit (obj );
Db. SubmitChanges ();
}
Catch (Exception ex)
{

Throw new Exception (ex. Message );
}
}

/// <Summary>
/// Delete the Qualified Data
/// </Summary>
/// <Param name = "expression"> deletion condition </param>
Public void Delete (System. Linq. Expressions. Expression <Func <T, bool> expression)
{
Try
{
Db. GetTable <T> (). DeleteAllOnSubmit <T> (Select (expression ));
Db. SubmitChanges ();
}
Catch (Exception ex)
{

Throw new Exception (ex. Message );
}
}

/// <Summary>
/// Perform Batch deletion or modification based on SQL statements, which improves the efficiency of batch deletion (update ).
/// </Summary>
/// <Param name = "SQL"> SQL statement to be deleted (updated) </param>
Public void DeleteOrUpdate (String SQL)
{
Try
{
Db. ExecuteCommand (SQL );
Db. SubmitChanges ();
}
Catch (Exception ex)
{

Throw new Exception (ex. Message );
}
}
}
}

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.