MVC, EF small encapsulation, mvcef Encapsulation
1. EF is often used in projects. Sometimes most add, delete, modify, and query operations are repetitive. This encapsulation is for rapid development, and various architectural ideas are not taken into account during this period, I feel that something is lacking. So I will pull it out this time. If there are any problems, please provide more guidance.
2. The package is not tested in terms of stress and concurrency. If you are interested, let's look at it. Okay, no nonsense.
A. First, let's look at the approximate structure as follows. a.1: an object is an object that is generally written manually.
A.2: The DALContext. cs code is as follows:
namespace Test.Web.Site.DAL{ public class DALContext<T> : DbContext where T : class { public DALContext(string con) : base(con){} public DALContext(){} public DbSet<T> TV { get; set; } }}
A.3 BaseDAL. cs main addition, deletion, and modification methods
Namespace Test. web. site. DAL {public class BaseDAL <TEntity> where TEntity: class, new () {private static readonly object s_lock = new object (); private string constr = "xxxConn "; // The default string public BaseDAL (string con = "") {if (con! = Constr & con. Length> 0) {lock (s_lock) {constr = con ;}}}
# Region Modify and Delete public virtual bool Insert (TEntity entity) {using (var db = new DALContext <TEntity> (constr) {db. set <TEntity> (). add (entity); return db. saveChanges ()> 0 ;}} public virtual bool Delete (object col) {using (var db = new DALContext <TEntity> (constr) {TEntity entityToDelete = db. set <TEntity> (). find (col); if (entityToDelete! = Null) {return Delete (entityToDelete);} else {return false ;}} public virtual bool Delete (Expression <Func <TEntity, bool> predicate) {TEntity entityToDelete = Get (predicate); if (entityToDelete! = Null) {return Delete (entityToDelete);} else {return false;} public virtual IEnumerable <TEntity> InsertAll (List <TEntity> list) {using (var db = new DALContext <TEntity> (constr) {var dbSet = db. set <TEntity> (); List <TEntity> tList = new List <TEntity> (); foreach (var item in list) {try {db. set <TEntity> (). add (item);} catch (Exception) {tList. add (item); throw ;}} db. saveChanges (); return tList ;}} public virtual IEnumerable <TEntity> UpdateAll (List <TEntity> list) {using (var db = new DALContext <TEntity> (constr )) {var dbSet = db. set <TEntity> (); List <TEntity> tList = new List <TEntity> (); foreach (var item in list) {try {var entity = dbSet. attach (item); db. entry (item ). state = System. data. entityState. modified;} catch (Exception) {tList. add (item); throw ;}} db. saveChanges (); return tList ;}} public virtual bool Update (TEntity entityToUpdate) {using (var db = new DALContext <TEntity> (constr) {var dbSet = db. set <TEntity> (); var entity = dbSet. attach (entityToUpdate); db. entry (entityToUpdate ). state = System. data. entityState. modified; return db. saveChanges ()> 0 ;}# endregion }}
A.4 DatabaseExtensions. cs an EF extension class is used to connect to other databases, such as mysql (refer to network resources). If you need to execute the SQL statement query view, you can comment below. I will reply in time
A.5: Test. Web. Site. BLL is a series of methods open to Control. Only some codes are listed here. If you need them, please comment below and I will reply to you in time.
Namespace Test. web. site. BLL {public class DataBLL <T> where T: class, new () {// connection string public DataBLL (string constr = "") {SingletonBase <BaseDAL <T>. initialize (new BaseDAL <T> (constr);} public T Get (Expression <Func <T, bool> filter, string includeProperties = "") {return SingletonBase <BaseDAL <T>. instance. get (filter, includeProperties);} // insert an object public bool AddEntity (T t_entity) {return SingletonBase <BaseDAL <T>. instance. insert (t_entity);} // follow the new entity public bool Update (T t_enttiy) {return SingletonBase <BaseDAL <T>. instance. update (t_enttiy);} // query by SQL list public IEnumerable <object> GetListBySql (string query, bool otherdb = true) {return SingletonBase <BaseDAL <T>. instance. getBySql (query, otherdb);} // delete all data that meets the condition public IEnumerable <T> DeleteAll (Expression <Func <T, bool> predicate) {return SingletonBase <BaseDAL <T>. instance. deleteAll (predicate);} // batch update list public IEnumerable <T> UpdateAll (List <T> list) {return SingletonBase <BaseDAL <T>. instance. updateAll (list);} // Add public IEnumerable <T> InsertAll (List <T> list) {return SingletonBase <BaseDAL <T>. instance. insertAll (list );}}}
3. The Control call only needs to be similar:
Account currAccount = new DataBLL<Account>(ConfigHelper.Constr_read).Get(p => p.UserId == 1111);
New DataBLL <Account>. GetPage (out totalCount, query. Expression, pageIndex, pageSize, k => k. OrderByDescending (_ => _. CreateTime). ToList ();
Conclusion: All code can be copied and added to EF reference. If any problem is found, please try again. Thank you very much.