Added versions of EF encapsulation class, and added methods for searching data from the cache for your reference !, Search in ef

Source: Internet
Author: User

Added versions of EF encapsulation class, and added methods for searching data from the cache for your reference !, Search in ef

EF encapsulation class added version, added methods for searching data from the cache for reference!

This class is an abstract class. Here I add the method ValidateEntity that requires subclass verification to facilitate expansion. If you want to directly use this class, you can change this class to a static class, you can directly call all the methods in it by changing them to static methods. There may be some shortcomings. You are welcome to leave a comment below this article and make improvements together. Thank you!

Using System; using System. collections. generic; using System. linq; using System. text; using System. data. objects. dataClasses; using ZBService. model; using System. linq. expressions; using System. web; namespace ZBService {public abstract class ServiceBase <T> where T: entityObject {// <summary> // EF context object // </summary> protected mZhaoBiaoEntities zbEntities = new mZhaoBiaoEntities (); /// <summary> /// determine whether or not Yes /// </summary> /// <param name = "whereExpr"> </param> /// <returns> </returns> public bool Exist (Expression <Func <T, bool> whereExpr) {return (this. count (whereExpr)> 0 );} /// <summary> /// obtain the number of records /// </summary> /// <param name = "whereExpr"> </param> /// <returns> </returns> public int Count (Expression <Func <T, bool> whereExpr) {return zbEntities. createObjectSet <T> (). where (whereExpr ). count ();}/// <Summary> /// search for an object /// </summary> /// <param name = "whereExpr"> </param> /// <returns> </returns> public T Find (Expression <Func <T, bool> whereExpr) {return zbEntities. createObjectSet <T> (). where (whereExpr ). firstOrDefault ();} /// <summary> /// search for an object from the cache /// </summary> /// <param name = "whereExpr"> </param> /// <param name = "cacheKey"> </param> /// <param name = "expireTime"> </param> /// <returns> </ret Urns> public T FindFromCache (Expression <Func <T, bool> whereExpr, string cacheKey, DateTime expireTime) {T entity; object obj = HttpRuntime. cache [cacheKey]; if (obj = null) {entity = this. find (whereExpr); HttpRuntime. cache. insert (cacheKey, entity, null, expireTime, System. web. caching. cache. noSlidingExpiration);} else {entity = obj as T;} return entity;} // <summary> /// query the Object List /// </summ Ary> // <param name = "whereExpr"> </param> // <returns> </returns> public IEnumerable <T> FindList <TKey> (Expression <Func <T, bool> whereExpr, Expression <Func <T, TKey> orderbyExpr, int orderDirection) {return this. findList <T, TKey> (whereExpr, t => t, orderbyExpr, orderDirection );} /// <summary> /// search for the object list from the cache /// </summary> /// <typeparam name = "TKey"> </typeparam> // /<param name = "whereExpr"> </pa Ram> /// <param name = "orderbyExpr"> </param> /// <param name = "orderDirection"> </param> // <param name =" cacheKey "> </param> /// <param name =" expireTime "> </param> /// <returns> </returns> public IEnumerable <T> FindListFromCache <TKey> (Expression <Func <T, bool> whereExpr, Expression <Func <T, TKey> orderbyExpr, int orderDirection, string cacheKey, DateTime expireTime) {return this. findListFromCache (wher EExpr, t => t, orderbyExpr, orderDirection,-1, cacheKey, expireTime );} /// <summary> /// search for the object list /// </summary> /// <typeparam name = "TResult"> </typeparam> /// <typeparam name = "TKey"> </typeparam> // <param name = "whereExpr"> </param> // <param name = "selectExpr"> </param> /// <param name = "orderbyExpr"> </param> // <param name = "orderDirection"> </param> // <param name = "returnCount"> </param> // <re Turns> </returns> public IEnumerable <TResult> FindList <TResult, TKey> (Expression <Func <T, bool> whereExpr, Expression <Func <T, TResult> selectExpr, expression <Func <TResult, TKey> orderbyExpr, int orderDirection, int returnCount =-1) {var result = zbEntities. createObjectSet <T> (). where (whereExpr ). select (selectExpr); if (result! = Null & result. count ()> 0) {if (orderDirection> 0) {result = result. orderByDescending (orderbyExpr);} else {result = result. orderBy (orderbyExpr);} if (returnCount> 0) {result = result. take (returnCount);} return result. toList ();} return null ;} /// <summary> /// search for the object list from the cache /// </summary> /// <typeparam name = "TResult"> </typeparam> /// <typeparam name = "TKey"> </typeparam> // <param name = "whereExpr"> </param> // <param name = "selectExpr"> </ param> /// <param name = "orderbyExpr"> </param> /// <param name = "orderDirection"> </param> // <param name =" returnCount "> </param> // <param name =" cacheKey "> </param> // <param name =" expireTime "> </param> /// <returns> </returns> public IEnumerable <TResult> FindListFromCache <TResult, TKey> (Expression <Func <T, bool> whereExpr, Expression <Func <T, TResult> selectExpr, Expression <Func <TResult, TKey> orderbyExpr, int orderDirection, int returnCount, string cacheKey, DateTime expireTime) {IEnumerable <TResult> resultList; object obj = HttpRuntime. cache [cacheKey]; if (obj = null) {resultList = this. findList (whereExpr, selectExpr, orderbyExpr, orderDirection, returnCount); HttpRuntime. cache. insert (cacheKey, resultList, null, expireTime, System. web. caching. cache. noSlidingExpiration);} else {resultList = obj as IEnumerable <TResult >;} return resultList ;} /// <summary> /// list of object objects displayed by PAGE /// </summary> /// <typeparam name = "TResult"> </typeparam> /// <typeparam name = "TKey"> </typeparam> // <param name = "whereExpr"> </param> // <param name = "selectExpr"> </param> /// <param name = "orderbyExpr"> </param> /// <param name = "orderDirection"> </param> /// <param name = "pageSize "> </param> /// <param name =" pageNo "> </param> /// <param name =" recordCount "> </param> /// <returns> </returns> public IEnumerable <TResult> FindListByPage <TResult, TKey> (Expression <Func <T, bool> whereExpr, Expression <Func <T, TResult> selectExpr, Expression <Func <TResult, TKey> orderbyExpr, int orderDirection, int pageSize, int pageNo, out int recordCount) {recordCount = 0; var result = zbEntities. createObjectSet <T> (). where (whereExpr ). select (selectExpr); if (result = null) return null; recordCount = result. count (); if (pageNo> recordCount) pageNo = recordCount; if (pageNo <= 0) pageNo = 1; if (recordCount> 0) {if (recordCount> pageSize) {if (orderDirection> 0) {return result. orderByDescending (orderbyExpr ). skip (pageNo-1) * pageSize ). take (pageSize ). toList ();} else {return result. orderBy (orderbyExpr ). skip (pageNo-1) * pageSize ). take (pageSize ). toList () ;}} else {if (orderDirection> 0) {return result. orderByDescending (orderbyExpr ). toList ();} else {return result. orderBy (orderbyExpr ). toList () ;}} return null ;} /// <summary> /// query the Object List by page from the cache /// </summary> /// <typeparam name = "TResult"> </typeparam>/ // <typeparam name = "TKey"> </typeparam> // <param name = "whereExpr"> </param> // <param name = "selectExpr"> </param> /// <param name = "orderbyExpr"> </param> /// <param name = "orderDirection"> </param> /// <param name = "pageSize"> </param> // <param name = "pageNo"> </param> // <param name = "recordCount"> </param>/ // <param name = "cacheKey"> </param> // <param name = "expireTime"> </param> // <returns> </returns> public IEnumerable <TResult> FindListByPageFromCache <TResult, TKey> (Expression <Func <T, bool> whereExpr, Expression <Func <T, TResult> selectExpr, Expression <Func <TResult, TKey> orderbyExpr, int orderDirection, int pageSize, int pageNo, out int recordCount, string cacheKey, DateTime expireTime) {recordCount = 0; IQueryable <TResult> result; object obj = HttpRuntime. cache [cacheKey]; if (obj = null) {result = zbEntities. createObjectSet <T> (). where (whereExpr ). select (selectExpr);} else {result = obj as IQueryable <TResult >;} if (result = null) return null; recordCount = result. count (); if (pageNo> recordCount) pageNo = recordCount; if (pageNo <= 0) pageNo = 1; if (recordCount> 0) {if (recordCount> pageSize) {if (orderDirection> 0) {return result. orderByDescending (orderbyExpr ). skip (pageNo-1) * pageSize ). take (pageSize ). toList ();} else {return result. orderBy (orderbyExpr ). skip (pageNo-1) * pageSize ). take (pageSize ). toList () ;}} else {if (orderDirection> 0) {return result. orderByDescending (orderbyExpr ). toList ();} else {return result. orderBy (orderbyExpr ). toList () ;}} return null ;} /// <summary> /// Add entity /// </summary> /// <param name = "entity"> </param> public virtual void Add (T entity) {this. validateEntity (entity, ValidateMode. add); zbEntities. createObjectSet <T> (). addObject (entity );} /// <summary> /// add an Object List /// </summary> /// <param name = "entities"> </param> public virtual void AddList (IEnumerable <T> entities) {var objSet = zbEntities. createObjectSet <T> (); foreach (T entity in entities) {this. validateEntity (entity, ValidateMode. add); objSet. addObject (entity) ;}/// <summary> // update the separated entity, if this method is not separated, you do not need to execute this method // </summary> // <param name = "entity"> </param> public virtual void Update (T entity) {this. validateEntity (entity, ValidateMode. update); zbEntities. createObjectSet <T> (). applyCurrentValues (entity );} /// <summary> /// Delete an object /// </summary> /// <param name = "entity"> </param> public virtual void Delete (T entity) {this. validateEntity (entity, ValidateMode. delete); zbEntities. createObjectSet <T> (). deleteObject (entity );} /// <summary> /// Delete an object /// </summary> /// <param name = "whereExpr"> </param> public virtual void Delete (Expression <func <T, bool> whereExpr) {var objSet = zbEntities. createObjectSet <T> (); T entity = objSet. where (whereExpr ). single (); // this. validateEntity (entity, ValidateMode. delete); objSet. deleteObject (entity );} /// <summary> /// Delete the Object List /// </summary> /// <param name = "entities"> </param> public virtual void DeleteList (IEnumerable <T> entities) {var objSet = zbEntities. createObjectSet <T> (); foreach (T entity in entities) {// this. validateEntity (entity, ValidateMode. delete); objSet. deleteObject (entity) ;}/// <summary> /// submit the operation to save all changes /// </summary> public void SubmitSave () {zbEntities. saveChanges ();} /// <summary> /// verify /// </summary> /// <param name = "entity"> </param> /// <returns> </ returns> protected virtual void ValidateEntity (T entity, validateMode mode = ValidateMode. add) {}/// <summary> /// Verification Mode /// </summary> protected enum ValidateMode {Add = 0, Update = 1, delete =-1 }}}

Previous articles see: http://www.cnblogs.com/zuowj/p/4259515.html

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.