Mvc+easyui Complete Implementation

Source: Internet
Author: User

Study MVC more than half a month, while a few days vacation, nothing to do to do a complete website to play, by the way and the company's methods to do a comparison. The page engine uses MVC's own features to create a view that gives the. NET render-out page (contrast: Rendered by a third-party component used by a home company); The foreground and background are interactive by Ajax (contrast: Based on the ashx+ configuration file, using reflection to achieve, more cumbersome) ORM adopts the EF6 (contrast: Using DbCommand package, the DataTable and model use reflection to implement automatic binding, do not support add an object, compared to the low end), suck the method of the home Daniel, can customize the database configuration, For example, the definition of SQL and what database to use, just use it rather lame, Daniel do not spray; page templates use Template.js before, and now try the razor syntax. Function: The permission part basically realizes the complete, the subsequent time has continued to improve. Through this practice: accumulate some experience is, a lot of seemingly simple things, if hands up, or there are a lot of details to pay attention to, this period may be an experience, an accumulation of experience, all kinds of taste can only be obtained by their own attempts.

Environment: vs2015+ SQL Server 2008+ ef6+ easyui. Note is MySQL-enabled, with EF, slightly encapsulated.

Permissions control, MVC is really very convenient, can control the page menu, can control the level of operation, likes. Here is the copy online.

public class Authorizemanageattribute:authorizeattribute    {        private int _a, _b;        private bool Yes;        Public Authorizemanageattribute (int a, int b)        {            _a = A;            _b = b;        }        First executed method        protected override bool Authorizecore (HttpContextBase HttpContext)        {            yes = _a > _b;            return yes;        }        Do        protected override void Handleunauthorizedrequest (AuthorizationContext) when the Authorizecore method returns false Filtercontext)        {            base. Handleunauthorizedrequest (filtercontext);            if (Filtercontext = = null)            {                throw new ArgumentNullException ("Filtercontext");            }            else            {                filterContext.HttpContext.Response.Redirect ("/htmlpage1.html");            }        }    }

EF, a crappy package:

namespace framewrok.common{internal class datacommandcontext<tcontext> where Tcontext:D bcontext,new () {  public void Dispose () {} public static Tcontext GetContext () {return        New Tcontext (); }}} public class datacommand<tcontext> where Tcontext:dbcontext, new () {private readonly string _cm        Dkey;        Private readonly string _server;        Private idictionary<string, sqlcmdmodel> _dictcmd = new dictionary<string, sqlcmdmodel> ();            <summary>///XML, SQLCMD's Name property///</summary> public string CmdKey {            get {return _cmdkey; }}///<summary>//database type: Supports SQL Server///</summary> public string Serve            R {get {return _server;  }}///<summary>//Current SQL statement      </summary> public string curcmdline = _dictcmd.keys.any () = = False? String. Empty: _dictcmd[_cmdkey].        CmdLine;             Public DataCommand () {} public DataCommand (string server, String sqlcmdname) {_server = server;            _cmdkey = Sqlcmdname;        Readcmmdconfig (); }///<summary>///SQL statement parameters, with simple string substitution, so you cannot use duplicate keywords in SQL string (contrast: more rigorous with setparameter)///&LT;/SU mmary>//<param name= "param" ></param>///<param Name= "Val" ></param> pub LIC void Setparameters (string param, string val) {if (_dictcmd.keys.any () ==false) throw new sqlnotfilled            Exception (); var targetcmd = _dictcmd[_cmdkey].            CmdLine; _dictcmd[_cmdkey].        CmdLine = Targetcmd.replace (param, Val); }///<summary>//Custom SQL///</summary>//<typeparam name= "T" > Based on output SQL field Define class </typeparam>///<returns> can return null</returns> public list<t> executesql<t> () {if (string .            IsNullOrEmpty (curcmdline)) return null; using (var db = Datacommandcontext<tcontext>.                GetContext ()) {var sql = Curcmdline; var res = db. database.sqlquery<t> (SQL).                ToList ();            return res; }} public void Executenoreturn () {if (string.            IsNullOrEmpty (Curcmdline)) return; using (var db = Datacommandcontext<tcontext>.                GetContext ()) {var sql = Curcmdline; Db.            Database.executesqlcommand (SQL); }}///<summary>///Add basic table content///</summary>//<typeparam name= "T" > </typeparam>//<param name= "obj" ></param>//<returns></returns> Pub      LIC static bool Add<t> (T obj) where t:class   {using (var db = Datacommandcontext<tcontext>. GetContext ()) {db. Entry (obj).                state=entitystate.added; Db.                SaveChanges ();            return true; }} public static DbContext Getcurcontext () {return datacommandcontext<tcontext>.        GetContext (); } private void Readcmmdconfig () {var dir=new DirectoryInfo (AppDomain.CurrentDomain.BaseDirectory            + "config");            if (dir==null) throw new InvalidOperationException (); var filelist=dir.            GetFiles ("*.xml");                foreach (Var fileInfo in fileList) {parsexml (fileinfo.fullname);            if (_dictcmd.keys.count > 0) return;            }} private void Parsexml (string path) {Sqlroot root;      using (var stream = new StreamReader (path)) {var xmlseri = new XmlSerializer (typeof (Sqlroot));          Root = (sqlroot) xmlseri.deserialize (stream); } if (!root.            Sqlcmdlist.any ()) throw new SqlNullValueException (); var serverfound =root. Sqlcmdlist.where (x = x.name.equals (_server)).            ToList ();            if (!serverfound.any ()) throw new SqlNullValueException (); var cmdfound = serverfound[0]. Sqlcmd.where (x = x.name.equals (_cmdkey)).            ToList ();            if (!cmdfound.any ()) throw new Sqlnotfilledexception ();        _dictcmd.add (_cmdkey,cmdfound[0]); }} [XmlRoot ("Sqlroot")] public class Sqlroot {[XmlElement ("Sqllist")] public List<sqlli    St> sqlcmdlist;  } [XmlRoot ("Sqllist")] public class Sqllist {[XmlAttribute ("name")] public string name {get; set;     } [XmlElement ("sqlcmd")] public list<sqlcmdmodel> SQLCMD {get; set;} } [XmlRoot ("SQLCMD")] public class Sqlcmdmodel {[XmlElement ("param")] public list<params>Param {get; set;}        [XmlElement ("CmdLine")] public string CmdLine {get; set;}    [XmlAttribute ("name")] public string name {get; set;}        } [XmlRoot] public class Params {[XmlAttribute (' name ')] public string name {get; set;}    [XmlAttribute ("type")] public string type {get; set;} }

When you want to customize SQL and do not want to use LINQ to SQL, you can define the structure in the configuration file, and get the SQL passed EF execution, XML structure:

<?XML version= "1.0" encoding= "Utf-8"?><Sqlroot>  <sqllistname= "SQL Server">    <SQLCMDname= "Deleterolesbyid">      <CmdLine>        <! [Cdata[Delete from Tb_usergroup where ID in (@roles)]]>      </CmdLine>      <paramname= "@roles"type= "string"/>    </SQLCMD>    <SQLCMDname= "Deleteuserbyid">      <CmdLine>        <! [Cdata[Delete from Tb_user where ID in (@roles)]]>      </CmdLine>      <paramname= "@roles"type= "string"/>    </SQLCMD>    <SQLCMDname= "Getblogall">      <CmdLine>        <! [Cdata[select * FROM blogs--where [email protected]]]>      </CmdLine>    </SQLCMD>    <SQLCMDname= "Getblogtitle">      <CmdLine>        <! [Cdata[Select P.postid, p.title,b.name from Posts pleft join Blogs b on P.blogid=b.blogid]]>      </CmdLine>    </SQLCMD>  </sqllist>  <sqllistname= "MYSQL">     </sqllist>  <sqllistname= "Oracle">  </sqllist></Sqlroot>
View Code

Before the MVC, using the company package framework, but through all the way down, check data, a lot of principles or basic is the same, from page rendering processing, front-end and backend interaction, background rights control method, ORM data layer, MVC seems more convenient, MVC is just a few routing functions, Poor little friends are still being ravaged.

Mvc+easyui Complete Implementation

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.