ASP. NET MVC5 + EF6 build a layer-3 instance, mvc5ef6
1. Create a project Solution
1 namespace wchlorophyll. EFDal 2 {3 /// <summary> 4 // custom EF context container class 5 /// </summary> 6 public class BaseDBContext: dbContext 7 {8 // <summary> 9 // initializes EF 10 /// according to the specified database link string. </summary> 11 public BaseDBContext (): base ("name = DonationEntities") {} 12} 13}View Code
2. Create a BaseDal class to store the common methods of each class: Query, add, edit, delete, and execute SQL statements.
2.1 First implement a query method, and then expand
BaseDal class:
1 namespace wchlorophyll. EFDal 2 {3 /// <summary> 4 /// performs operations on common addition, deletion, and query of all tables in the database, change to 5 // </summary> 6 // <typeparam name = "TEntity"> </typeparam> 7 public class BaseDal <TEntity> where TEntity: class 8 {9 // 1.0 instantiate EF context container object 10 BaseDBContext db = new BaseDBContext (); 11 12 DbSet <TEntity> _ dbset; 13 14 public BaseDal () 15 {16 // initialize 17 _ dbset = db. set <TEntity> (); 18} 19 20 # region Query 21 public List <TEntity> Query (Expression <Func <TEntity, bool> where) 22 {23 return _ dbset. where (where ). toList (); 24} 25 # endregion 26 27} 28}View Code
2.2 create the CompanyDal class and DonationDetailDal to inherit the BaseDal
CompanyDal class:
1 namespace wchlorophyll. EFDal 2 {3 public class CompanyDal: BaseDal <Company> 4 {5} 6}View Code
DonationDetailDal class:
1 namespace wchlorophyll. EFDal 2 {3 public class DonationDetailDal: BaseDal <DonationDetail> 4 {5 // unique methods of each class 6} 7}View Code
4. To call the Dal layer on the BLL layer, you must apply the model layer.
4.1 create a BaseBLL class as the base class and call the method queried in the dal
1 namespace wchlorophyll. EFBLL 2 {3 public class BaseBLL <TEntity> where TEntity: class 4 {5 // initialize BaseDal generic class Object 6 BaseDal <TEntity> bdal = new BaseDal <TEntity> (); 7 8 public List <TEntity> Query (Expression <Func <TEntity, bool> where) 9 {10 return bdal. query (where); 11} 12} 13}View Code
4.2 create the CompanyBLL class and the DonationDetailBLL class to create several layers of parent classes
DonationDetailBLL class:
1 namespace wchlorophyll. EFBLL 2 {3 public class DonationDetailBLL: BaseBLL <DonationDetail> 4 {5} 6}View Code
CompanyBLL class:
1 namespace wchlorophyll. EFBLL 2 {3 public class CompanyBLL: BaseBLL <Company> 4 {5} 6}View Code
5. The UI Layer calls the bl layer l and model layers, and creates a test method under the home controller to create Views
5.1 create the Test method
1 public ActionResult Test () 2 {3 CompanyBLL cbll = new CompanyBLL (); 4 5 return View (cbll. Query (c => true); 6}View Code
5.2 when creating a view, use the layout page, select the list template, and select "Company. EFModel" for the model class)
1 namespace wchlorophyll. EFDal 2 {3 /// <summary> 4 /// performs operations on common addition, deletion, and query of all tables in the database, change to 5 // </summary> 6 // <typeparam name = "TEntity"> </typeparam> 7 public class BaseDal <TEntity> where TEntity: class 8 {9 // 1.0 instantiate EF context container object 10 BaseDBContext db = new BaseDBContext (); 11 12 DbSet <TEntity> _ dbset; 13 14 public BaseDal () 15 {16 // initialize 17 _ dbset = db. set <TEntity> (); 18} 19 20 # region Query 21 public List <TEntity> Query (Expression <Func <TEntity, bool> where) 22 {23 return _ dbset. where (where ). toList (); 24} 25 26 public List <TEntity> QueryJoin (Expression <Func <TEntity, bool> where, string [] tableNames) 27 {28 // assign the subclass _ dbset to query 29 DbQuery of the parent class <TEntity> query = _ dbset; 30 31 foreach (var item in tableNames) 32 {33 // traverse the name of the table to be connected, and finally obtain the DbQuery object 34 query = query after all connected tables. include (item); 35} 36 return query. where (where ). toList (); 37} 38 # endregion 39 40 # region added 41 public void Add (TEntity model) 42 {43 _ dbset. add (model); 44} 45 # endregion 46 47 # region Edit 48 public void Edit (TEntity model, string [] propertyName) 49 {50 if (model = null) 51 {52 throw new Exception ("the model must be an object of the object"); 53} 54 if (propertyName = null | propertyName. any () = false) 55 {56 throw new Exception ("at least one attribute must be specified "); 57} 58 59 // append the model to EF container 60 DbEntityEntry = db. entry (model); 61 62 entry. state = EntityState. unchanged; 63 64 foreach (var item in propertyName) 65 {66 entry. property (item ). isModified = true; 67} 68} 69 # endregion 70 71 # region physical deletion 72 // EntityState. unchanged 73 public void Delete (TEntity model, bool isaddefcontext) 74 {75 if (isAddedEFContext = false) 76 {77 _ dbset. attach (model); 78} 79 // modify the status to deleted 80 _ dbset. remove (model); 81} 82 # endregion 83 84 # region unified execution save 85 public int SaveChanges () 86 {87 return db. saveChanges (); 88} 89 # endregion 90 91} 92}View Code
7. Improve the BLL layer BaseBLL Class Method
BaseBLL class:
1 namespace wchlorophyll. EFBLL 2 {3 public class BaseBLL <TEntity> where TEntity: class 4 {5 // initialize BaseDal generic class Object 6 BaseDal <TEntity> bdal = new BaseDal <TEntity> (); 7 # region Query 8 public List <TEntity> Query (Expression <Func <TEntity, bool> where) 9 {10 return bdal. query (where); 11} 12 13 public List <TEntity> QueryJoin (Expression <Func <TEntity, bool> where, string [] tableNames) 14 {15 return bdal. queryJoin (where, tableNames); 16} 17 # endregion 18 19 # region adds 20 21 public void Add (TEntity model) 22 {23 bdal. add (model); 24} 25 26 # endregion 27 28 # region edit 29 30 // <summary> 31 // required: The model must be a defined entity, at this time, it is not appended to the EF container 32 // </summary> 33 // <param name = "model"> </param> 34 public void Edit (TEntity model, string [] propertyName) 35 {36 bdal. edit (model, propertyName); 37} 38 39 # endregion 40 41 # region physical deletion 42 43 // <summary> 44 // The model must be customized, delete 45 // </summary> 46 // <param name = "model"> object to be deleted </param> 47 // <param name = "isAddedEFContext"> true: indicates the model and append it to the ef container. false: not appended </param> 48 public void Delete (TEntity model, bool isaddefcontext) 49 {50 bdal. delete (model, isaddefcontext); 51} 52 53 # endregion 54 55 # region uniformly executes SQL statement 56 57 public int SaveChanges () 58 {59 return bdal. saveChanges (); 60} 61 62 # endregion 63 64} 65}View Code
8. mvc call Test
1 public ActionResult Test () 2 {3 CompanyBLL cbll = new CompanyBLL (); 4 5 // new Test: 6 Company model = new Company () 7 {8 CName = "test 1111111" 9}; 10 cbll. add (model); 11 12 // modify 13 // Company model1 = new Company () 14 // {15 // cID = 8, 16 // CName = "test 22221" 17 //}; 18 19 // cbll. edit (model1, new string [] {"CName"}); 20 21 22 // Delete 23 var model2 = cbll. query (c => c. cID = 8 ). firstOrDefault (); 24 cbll. delete (model2, true); // execute the SQL statement to open and close the ado.net link once. 25 26 // add, edit, Delete, and generate insert, update, respectively, the delete statement is sent to the database at one time for execution 27 cbll. saveChanges (); 28 29 30 31 return View (cbll. query (c => true); 32}View Code