EF additions and deletions by bad code
We created a series of solutions in the previous session, and we looked at the relationship between layers through an example.
We separated the controllers from the BLL layer and the DAL layer.
BLL focus on the business process
DAL focuses on data access layer processing
and controller with a clear view of the interaction
We have added an entity to EF in the last lecture syssample
Let's create the IDAL,DAL,IBLL,BLL code below.
Using App.models;
Using System.Linq;
Namespace App.idal {public interface isyssamplerepository {///<summary>///get list </summary>///<param name= "db" > Database context </param>///<returns> data list </RETURNS&G
T
Iqueryable<syssample> getlist (Dbcontainer db); <summary>///Create an entity///</summary>///<param name= "entity" > Entity </param>
;
int Create (syssample entity); <summary>///Delete an entity///</summary>///<param name= "entity" > Primary key id</param&
Gt
int Delete (string id); <summary>///Modify an entity///</summary>///<param name= "entity" > Entity </param>
;
int Edit (syssample entity);
<summary>///get an entity///</summary>///<param name= "id" >id</param> /// <returns> entity </returns> syssample GetByID (string id);
<summary>///to determine whether an entity exists///</summary> bool Isexist (string ID); }} ISysSampleRepository.cs
Using System;
Using System.Linq;
Using App.idal;
Using App.models;
Using System.Data; Namespace App.dal {public class syssamplerepository:isyssamplerepository, IDisposable {///<summary >///Get list///</summary>///<param name= "db" > Database context </param>///
;returns> Data List </returns> public iqueryable<syssample> getlist (Dbcontainer db) { iqueryable<syssample> list = db.
Syssample.asqueryable ();
return list; ///<summary>///Create an entity///</summary>///<param name= "db" > Database context
;/param>///<param name= "entity" > Entity </param> public int Create (syssample entity) { using (Dbcontainer db = new Dbcontainer ()) {db.
Syssample.addobject (entity); Return DB.
SaveChanges (); }}///<summAry>///Delete an entity///</summary>///<param name= "db" > Database context </param>// /<param Name= "entity" > Primary key id</param> public int Delete (string id) {using (Dbcontai NER db = new Dbcontainer ()) {syssample entity = db.
Syssample.singleordefault (a => a.id = Id); if (entity!= null) {db.
Syssample.deleteobject (entity); Return DB.
SaveChanges (); }///<summary>///modifies an entity///</summary>///<param name= "D B "> Database context </param>///<param name=" entity "> Entity </param> public int Edit (Syssample entit Y) {using (Dbcontainer db = new Dbcontainer ()) {db.
Syssample.attach (entity); Db. Objectstatemanager.changeobjectstaTE (entity, entitystate.modified); Return DB.
SaveChanges (); }///<summary>///obtains an entity///</summary>///<param name= "id"
;id</param>///<returns> entity </returns> public syssample GetByID (string id) { using (Dbcontainer db = new Dbcontainer ()) {return db.
Syssample.singleordefault (a => a.id = Id); }///<summary>///to determine if an entity exists///</summary>///<param name= "id
">id</param>///<returns> exists true or false</returns> public bool Isexist (string ID) {using (Dbcontainer db = new Dbcontainer ()) {syssample entity = GetByID
(ID);
if (entity!= null) return true;
return false; }} public void DispOSE () {}}}