Build a back-end management system for ASP. Mvc4+ef5+easyui+unity2.x Injection (5)-ef Add and delete change by bad code

Source: Internet
Author: User
Tags jqgrid actionlink connectionstrings

In the previous session we created a series of solutions, and we looked at the relationship between layers with an example.

We separated the controllers from the BLL layer and the DAL layer.

The BLL focuses on business processes.

The DAL focuses on the processing of the data access layer

and the controller interacts with the view clearly.

We've already added an entity to EF in the last talk Syssample

Now let's create the IDAL,DAL,IBLL,BLL code.

Using app.models;using system.linq;namespace app.idal{public interface Isyssamplerepository {//<summar y>///Get a list//</summary>//<param name= "db" > Database context </param>//<re        turns> data List </returns> iqueryable<syssample> GetList (Dbcontainer db);        <summary>///Create an entity///</summary>//<param Name= "entity" > Entities </param>        int Create (syssample entity);         <summary>///delete an entity///</summary>//<param Name= "entity" > Primary key id</param>               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> entities </returns>     Syssample GetById (string id);    <summary>///Determine if 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 SysSam Plerepository:isyssamplerepository, IDisposable {//<summary>//Get a list//</summary&        Gt <param name= "DB" > Database context </param>//<returns> data list </returns> public iqueryable&lt ; Syssample> GetList (Dbcontainer db) {iqueryable<syssample> list = db.            Syssample.asqueryable ();        return list; }///<summary>//Create an entity///</summary>//<param name= "db" > Database context </pa            ram>//<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///lt;/summary>//<param name= "db" > Database context </param>//<param Name= "entity" > Primary key Id</para                m> public int Delete (string id) {using (Dbcontainer db = new Dbcontainer ()) { Syssample entity = db.                Syssample.singleordefault (A = a.ID = = Id); if (Entity! = NULL) {db.                Syssample.deleteobject (entity); } return DB.            SaveChanges (); }}///<summary>//Modify an entity///</summary>//<param name= "db" > Database            Below </param>//<param Name= "entity" > Entity </param> public int Edit (syssample entity) { using (Dbcontainer db = new Dbcontainer ()) {db.                Syssample.attach (entity); Db.                Objectstatemanager.changeobjectstate (entity, entitystate.modified); Return DB.  SaveChanges ();          }}///<summary>//To get an entity///</summary>//<param name= "I            D ">id</param>///<returns> entities </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> presence 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 () {}}}syssamplerepository.cs

------------------------------------------------------------------------------//<auto-generated>// This code is automatically generated by the T4 template//Build time 2013-03-12 11:19:12 by app//changes to this file may result in incorrect behavior, and if//regenerate code, these changes will be lost. </auto-generated>//------------------------------------------------------------------------------using system.collections.generic;using app.models;namespace app.ibll{Public interface Isyssamplebll {//<summ  ary>///Get a list//</summary>//<param name= "pager" >jqgrid page </param>//  <param name= "Querystr" > Search conditions </param>//<returns> list </returns> list<syssample>        GetList (string querystr); <summary>///Create an entity///</summary>//<param name= "errors" > Persistent error message </param&        Gt  <param name= "model" > Model </param>//<returns> Success </returns> bool Create (syssample        model);<summary>///delete an entity///</summary>//<param name= "errors" > Persistent error message </param&        Gt              <param name= "id" >id</param>///<returns> successful </returns> bool Delete (string ID); <summary>///Modify an entity///</summary>//<param name= "errors" > Persistent error        Info </param>//<param name= "model" > Models </param>//<returns> Success </returns>        BOOL Edit (syssample model); <summary>///The model entity is obtained by ID///</summary>//<param name= "id" >id</param&        Gt        <returns>model entities </returns> Syssample GetById (string id);        <summary>///Determine if there are entities///</summary>//<param name= "id" > Primary key id</param>    Whether <returns> exists </returns> bool Isexist (string ID); }}isyssamplebll

Using system;using system.collections.generic;using system.linq;using app.models;using App.Common;using App.IBLL; Using app.idal;using app.dal;namespace app.bll{public class Syssamplebll:isyssamplebll {Dbcontainer db = n        EW Dbcontainer ();        Isyssamplerepository Rep = new Syssamplerepository (); <summary>///Get a list//</summary>//<param name= "pager" >jqgrid pagination </param&gt        ; <param name= "Querystr" > Search conditions </param>//<returns> list </returns> public List<sys Sample> GetList (String querystr) {iqueryable<syssample> querydata =rep.getlist (db)            ;        return Querydata.tolist (); }///<summary>//Create an entity///</summary>//<param name= "errors" > Persistent        Error message </param>//<param name= "model" > Models </param>//<returns> Success </returns> PublicBOOL Create (syssample entity) {try {if (rep.create (entity) = = 1)                {return true;                } else {return false;                }} catch (Exception ex) {//exceptionhander.writeexception (ex);            return false; }}///<summary>//Delete an entity///</summary>//<param name= "Errors" >        Persistent error message </param>//<param name= "id" >id</param>//<returns> Success </returns>                public bool Delete (string id) {try {if (rep.delete (id) = = 1)                {return true;                } else {return false; }} catch (Exception ex) {                Exceptionhander.writeexception (ex);            return false; }}///<summary>//Modify an entity///</summary>//<param name= "error S "> Persistent error messages </param>//<param name=" model "> Models </param>//<returns> Success </retur Ns> public bool Edit (syssample entity) {try {if (Rep.edit (entity)                = = 1) {return true;                } else {return false; }} catch (Exception ex) {//exceptionhander.writeexceptio                N (ex);            return false; }}///<summary>//To determine the existence of entities///</summary>//<param name= "id" > Primary key Id</param>//<returns> presence </returns> public bool Isexists (string id) {if (db).            Syssample.singleordefault (A = a.ID = = Id) = null) {return true;        } return false; }///<summary>///For an entity by ID///</summary>//<param name= "id" >id</pa Ram>//<returns> entities </returns> public syssample GetById (string id) {if                Exist (ID)) {syssample entity = Rep.getbyid (ID);            return entity;            } else {return null; }}///<summary>//To determine if an entity exists///</summary>//<param name= "id" >        Id</param>//<returns> presence true or false</returns> public bool Isexist (string ID)        {return rep.isexist (ID); }}}SYSSAMPLEBLL

  

The interface is used for integration, so BLL:IBLL Dal:idal

The class comments above are very clear. (It's just Bad code)

We create an empty controller syssample and add the index view

@model ienumerable<app.models.syssample>@{Layout = null;} <! DOCTYPE html>

  

Open the 48th line of home index and modify it to <iframe scrolling= "Auto" frameborder= "0" src= "/syssample" style= "width:100%; height:100%; " ></iframe>

We will preview the Easyui frame

After running you may error the database is not connected, because we put EF under the App.models, the App.admin Web. config needs to be modified connectionstrings

The connectionstrings containing node for the app. Config under App.models

Compiler error message: CS0012: Type ' System.Data.Objects.DataClasses.EntityObject ' is defined in an unreferenced assembly.
You must add a reference to the assembly "System.Data.Entity, version=4.0.0.0, Culture=neutral, publickeytoken=b77a5c561934e089".

Open Web. config Lookup compilation debug= "true" targetframework= "4.5"

Adding nodes

<assemblies>

<add assembly= "System.Data.Entity, version=4.0.0.0, culture=neutral, publickeytoken=b77a5c561934e089"/>
</assemblies>

This gives the Web. config

<?xml version= "1.0" encoding= "Utf-8"?><!--For more information about how to configure an ASP. NET application, go to http://go.microsoft.com/fwlink/? linkid=169433--><configuration> <configSections> <!--for more information on Entity Framework con figuration, visit http://go.microsoft.com/fwlink/? linkid=237468--<section name= "EntityFramework" type= " System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, version=5.0.0.0, Culture=neutral,        publickeytoken=b77a5c561934e089 "requirepermission=" false "/> </configSections> <connectionStrings> <add name= "Dbcontainer" connectionstring= "metadata=res://*/db.csdl|res://*/db.ssdl|res://*/db.msl;provider= System.data.sqlclient;provider connection string= "Data source=.; Initial Catalog=db;user Id=sa; [email protected]#; Multipleactiveresultsets=true;     App=entityframework "" Providername= "System.Data.EntityClient"/> </connectionStrings> <appSettings> <add key= "Webpages:veRsion "value=" 2.0.0.0 "/> <add key=" webpages:enabled "value=" false "/> <add key=" Preserveloginurl "value = "true"/> <add key= "clientvalidationenabled" value= "true"/> <add key= "unobtrusivejavascriptenabled" va          Lue= "true"/> </appSettings> <system.web> <compilation debug= "true" targetframework= "4.5" > <assemblies> <add assembly= "System.Data.Entity, version=4.0.0.0, Culture=neutral, Publickeyto  ken=b77a5c561934e089 "/> </assemblies> </compilation> 

To modify Syssamplecontroller, modify the method of index () as follows:

public class Syssamplecontroller:controller  {      ////      GET:/syssample/public      actionresult Index ()      {          Syssamplebll BLL = new Syssamplebll ();          list<syssample> list = BLL. GetList ("");          return View (list);      }   }

  

We're inserting a few records into the database, and you should see the effect.

BLL dal to add and delete changes are done, we are interested in adding a bit of it, all you said is not good.

Build a back-end management system for ASP. Mvc4+ef5+easyui+unity2.x Injection (5)-ef Add and delete change by bad code

Related Article

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.