asp.net MVC5 website Development framework model, data storage, business logic (iii) _ Practical skills

Source: Internet
Author: User
Tags inheritance static class

The level of the previous project and the call relationship are illustrated in the following diagram

The use of the three-tier architecture, the need to study the BLL layer, that the business logic can be fully implemented in the controller, there is no need to do a separate project, another layer of many will affect performance. Later on, I made the business logic independent, for the following reasons:

    • Business logic written into the controller code looks more confusing, time is too long the code is easy to be confused.
    • In controller, the direct write logic repetition code will not be more, the development efficiency is low.
    • Sub-projects facilitate the reuse of code, sometimes can be directly to other projects can be slightly modified to use.

For performance I think layering is definitely going to have an impact, but it won't be big. Now the hardware is newer than the software, the business logic is easy to deal with, multiple instantiation of several classes have little impact on performance. Generally speaking, the site is basically running a database and database process, business logic or relatively few, but now the site uses more pictures, animation, more beautiful effect. I think the efficiency bottleneck of the website mainly appears on the server's bandwidth, IO performance and Access database. What you can do with your code is to optimize access to your database. For the general project, for a few percent of the operational efficiency is far better than improve the development efficiency and easier code management is important, can achieve the demand is good, running efficiency is what Daniel to do.

For the four projects of Idal, DAL, IBLL, BLL:

Idal write a base interface, the interface fixed several database operation methods, other interfaces are inherited from this interface;

The DAL project makes a base class that implements this Idal base interface, and other classes inherit from the base class.

The same IBLL also writes a base interface, fixing several basic operations, and the other interfaces are also inherited from this base interface

IBLL also writes a base class to implement the base interface in IBLL, and other classes inherit from this base class.

Here is the basic pattern for building code in the operation of the user:

First, the model
Here three model classes are written. Open ninesk.models Add user, UserGroup, userconfig three model classes separately.

1, user Model-user class
User model or Call account model, why do you look at the following code

Using System;

Using System.ComponentModel.DataAnnotations; namespace Ninesky.models {///<summary>///user model///<remarks>///Create: 2014.02.02<br/>///Modified: 2014

  .02.05///</remarks>///</summary> public class User {[Key] public int UserID {get; set;} <summary>///username///</summary> [Required (errormessage= "required")] [Stringlength (20,minimumlength=4,er

  Rormessage= "{1} to {0} characters")] [Display (name= username)] public string UserName {get; set;} <summary>///user Group ID///</summary> [Required (errormessage = "required")] [Display (Name = "user group ID")] Pub

  Lic int GroupID {get; set;}  <summary>///Display name///</summary> [Required (errormessage = "required")] [Stringlength (minimumlength =

  2, errormessage = "{1} to {0} characters")] [Display (name = display name)]] public string DisplayName {get; set;} <summary>///password///</summary> [Required (errormessage = "required")] [Display (Name = "Password"] [DataType (Datatype.password)] public string Password {get; set;} <summary>///mailbox///</summary> [Required (errormessage = "required")] [Display (Name = "Mailbox")]] [DataType

  (datatype.emailaddress)] public string Email {get; set;} <summary>///user status <br/>///0 Normal, 1 locked, 2 not authenticated by mail, 3 not through admin///</summary> public int Status {get ; Set

  ///<summary>///Registration time///</summary> public DateTime registrationtime {get; set;}

  <summary>///Last Login time///</summary> public DateTime logintime {get; set;}

  <summary>///Last Login IP///</summary> public DateTime loginip {get; set;}

 Public virtual usergroup Group {get; set;} }
}

This model class contains only the user name, password, user group, display name, mailbox and other attributes, pure basic account information, the purpose is to allow users to register as little information as possible. Other information if you need to be able to write new classes associated with the account, users need to log in after the completion of the fill (such as: Capital data, personal information, contact details, etc.). This is not considered here first. The display name here can be used as a nickname, a real name, and so on as needed.

2, user group Model-usergroup class
This class is aware of the grouptype, which is used to classify the user group, easy to manage, in fact, no special significance. My idea is that the normal type will put the normal user group, if the large site allows users to upgrade, limited to this type of user group. A privileged group can place a group of users, such as VIPs, who need to be given by an administrator to distinguish between ordinary user groups but without administrative authority. Admin type user groups need to be given by the backend administrator to manage articles, comments, and consulting.

 using System.ComponentModel.DataAnnotations; namespace Ninesky.models {///<summary>///user group///<remarks>///created: 2014.02.02///modified: 2014.02.08///&

  Lt;/remarks>///</summary> public class UserGroup {[Key] public int GroupID {get; set;}  <summary>///name///</summary> [Required (errormessage= "required")] [Stringlength (minimumlength = 2,

  ErrorMessage = "{1} to {0}")] [Display (name= name)] public string name {get; set;} <summary>///user group type <br/>///0 Normal type (normal registered user), 1 privilege type (such as VIP type), 3 administrative type (type of administrative permission)///</summary> [

  Required (errormessage = "required")] [Display (Name = user group type)] public int GroupType {get; set;} <summary>///Description///</summary> [Required (errormessage = "required")] [Stringlength (errormessage =]
 Less than {0} characters ")] [Display (Name = description]]] public string Description {get; set;} }
}

3, User Configuration model class-userconfig class
This class is a User configuration information (temporarily only consider registration settings), in the background administrator to set up.

Using System.ComponentModel.DataAnnotations; namespace Ninesky.models {///&lt;summary&gt;///User Configuration///&lt;remarks&gt;///Create: 2014.02.06///&lt;/remarks&gt;/

  &lt;/summary&gt; public class Userconfig {[Key] public int Configid {get; set;} &lt;summary&gt;///Enable registration///&lt;/summary&gt; [Display (Name = "Enable registration")] [Required (errormessage= "required")] public

  BOOL Enabled {get; set;} &lt;summary&gt;///user name &lt;br/&gt;///username "|"

  Separate///&lt;/summary&gt; [Display (name = "User name not allowed]]" public string Prohibitusername {get; set;}
  &lt;summary&gt;///Enable Administrator authentication///&lt;/summary&gt; [Display (Name = "Enable Administrator Authentication")] [Required (errormessage = "required")]

  public bool Enableadminverify {get; set;} &lt;summary&gt;///Enable mail verification///&lt;/summary&gt; [Display (Name = "Enable mail Authentication")] [Required (errormessage = "required")] P

  ublic bool Enableemailverify {get; set;} &lt;summary&gt;///Default user group ID///&lt;/summary&gt; [Display (Name = "SilentUser group ID ")] [Required (errormessage =" required ")] public int Defaultgroupid {get; set;} }
}

Second, the data storage layer
The data storage layer is responsible for dealing with the database, resulting in two project Dal and idal using the interface. Idal is an interface project, and the DAL is an implementation project for the interface.

In the database with the convenience of some common operations, such as add, modify, delete, query and so on. Do not want to write the code in the actual time in the user class to write these things again, the user group class to write again, later articles, comments are repeated to write the code. What to do, get a base class. Later, other classes inherit from the base class and inherit these public methods.

1. Idal Project
First open the Idal project, add Class Interfacebaserepository, code as follows.

Using System;
Using System.Linq;

Using System.Linq.Expressions; namespace Ninesky.idal {///&lt;summary&gt;///interface base class///&lt;remarks&gt; Create: 2014.02.03 &lt;br/&gt;///modified: 2014.02.09& lt;/remarks&gt;///&lt;/summary&gt;///&lt;typeparam name= "T" &gt; Type &lt;/typeparam&gt; public interface Interfacebase repository&lt;t&gt; {///&lt;summary&gt;///add///&lt;/summary&gt;///&lt;param name= "entity" &gt; Data entity &lt;/par

  Am&gt;///&lt;returns&gt; The added data entity &lt;/returns&gt; t Add (t entity); &lt;summary&gt;///Query record number///&lt;/summary&gt;///&lt;param name= "predicate" &gt; conditional expression &lt;/param&gt;///&lt;r

  eturns&gt; record number &lt;/returns&gt; int count (expression&lt;func&lt;t, bool&gt;&gt; predicate); &lt;summary&gt;///update///&lt;/summary&gt;///&lt;param name= "entity" &gt; Data entity &lt;/param&gt;///&lt;returns&

  gt; Success &lt;/returns&gt; BOOL Update (T entity); &lt;summary&gt;///Delete///&lt;/summary&gt;///&lt;param name= "entity" &gt; Data entity &lt;/param&gt;
  &lt;returns&gt; Success &lt;/returns&gt; BOOL Delete (T entity); &lt;summary&gt;///exists///&lt;/summary&gt;///&lt;param name= "Anylambda" &gt; Query expression &lt;/param&gt;///&lt;re

  turns&gt; boolean value &lt;/returns&gt; bool Exist (expression&lt;func&lt;t, bool&gt;&gt; anylambda); &lt;summary&gt;///query data///&lt;/summary&gt;///&lt;param name= "Wherelambda" &gt; Query expression &lt;/param&gt;///&lt;

  returns&gt; entity &lt;/returns&gt; T Find (expression&lt;func&lt;t, bool&gt;&gt; wherelambda); &lt;summary&gt;///Find data list///&lt;/summary&gt;///&lt;typeparam name= "S" &gt; Sort &lt;/typeparam&gt;///&lt;par Am Name= "WHERELAMDBA" &gt; Query expression &lt;/param&gt;///&lt;param name= "ISASC" &gt; whether ascending &lt;/param&gt;///&lt;param name= "Ord Erlamdba "&gt; Sort expression &lt;/param&gt;///&lt;returns&gt;&lt;/returns&gt; iqueryable&lt;t&gt; findlist&lt;s&gt; (
  
  Expression&lt;func&lt;t, bool&gt;&gt; WHERELAMDBA, bool Isasc, expression&lt;func&lt;t, s&gt;&gt; orderLamdba); &lt;summary&gt;/// Find pagination Data list///&lt;/summary&gt;///&lt;typeparam name= "S" &gt; Sort &lt;/typeparam&gt;///&lt;param name= "PageIndex" &gt; Current page &lt;/param&gt;///&lt;param name= "pageSize" &gt; per page record &lt;/param&gt;///&lt;param name= "Totalrecord" &gt; Total record number &lt;/ param&gt;///&lt;param name= "WHERELAMDBA" &gt; Query expression &lt;/param&gt;///&lt;param name= "ISASC" &gt; whether ascending &lt;/param&gt;/ &lt;param name= "orderlamdba" &gt; Sort expression &lt;/param&gt;///&lt;returns&gt;&lt;/returns&gt; iqueryable&lt;t&gt; findpagelist&lt;s&gt; (int pageIndex, int pageSize, out int totalrecord, expression&lt;func&lt;t, bool&gt;&gt;

 WHERELAMDBA, bool Isasc, expression&lt;func&lt;t, s&gt;&gt; orderlamdba); }
}

Here defines the Add, delete, change, Judge existence, return the model query, return the collection of queries, return to the page collection query 7 public methods. These methods basically meet the general needs, and special methods are added at the time of inheritance.
Generics are also used, which can be inherited directly by passing in entity types at the time of inheritance. A concrete look at the Interfaceuserrepository interface is clear.

Using Ninesky.models;
Namespace Ninesky.idal
{
 ///<summary>
 ///user interface
 ///<remarks> Create: 2014.02.03</remarks >
 ///</summary> public
 Interface interfaceuserrepository:interfacebaserepository<user>
 {
 }
}

Simply, inherit from the Interfacebaserepository interface and pass in the entity class user. We see in Class View whether the interface of the base class is inherited.

2, DAL project
The DAL project is the implementation of the Idal project interface, the project to create the DbContext class, for the DbContext class many people discussed its efficiency in database access, the MSDN said it is lightweight, the creation does not require a lot of overhead, it is not a thread-safe object, and has the nature of the data container (tracking), so many people think that it should not be static, a single case. But it is only reasonable to realize dbcontext for a single request of a user. Look at the code first, it's very simple.

Using Ninesky.models;
Using System.Data.Entity;

Namespace Ninesky.dal
{
 ///<summary>
 ///Data context
 ///<remarks> Create: 2014.02.03</remarks >
 ///</summary> public
 class Nineskydbcontext:dbcontext
 {public
  dbset<user> Users {get; set;}
  Public dbset<usergroup> usergroups {get; set;}
  Public dbset<userconfig> Userconfig {get; set;}
  Public Nineskydbcontext ()
   : Base ("DefaultConnection")
  {
  }
 }
}

The following creates a Baserepository class that inherits from Interfacebaserepository and implements the methods of its interfaces.

Using Ninesky.idal;
Using System;
Using System.Linq;

Using System.Linq.Expressions; namespace Ninesky.dal {///&lt;summary&gt;///Warehouse base class///&lt;remarks&gt; Create:2014.02.03&lt;/remarks&gt;/// &gt; public class Baserepository&lt;t&gt: interfacebaserepository&lt;t&gt; where t:class {protected Nineskydbcont

  Ext Ncontext = Contextfactory.getcurrentcontext (); Public T ADD (t entity) {ncontext.entry&lt;t&gt; (entity).
   state = System.Data.Entity.EntityState.Added;
   Ncontext.savechanges ();
  return entity; public int Count (expression&lt;func&lt;t, bool&gt;&gt; predicate) {return ncontext.set&lt;t&gt; ().
  Count (predicate); public bool Update (T entity) {ncontext.set&lt;t&gt; ().
   Attach (entity); Ncontext.entry&lt;t&gt; (entity).
   state = System.Data.Entity.EntityState.Modified;
  return ncontext.savechanges () &gt; 0; public bool Delete (T entity) {ncontext.set&lt;t&gt; ().
   Attach (entity); Ncontext.entry&lt;t&gt; (entity). State =System.Data.Entity.EntityState.Deleted;
  return ncontext.savechanges () &gt; 0; public bool Exist (expression&lt;func&lt;t, bool&gt;&gt; anylambda) {return ncontext.set&lt;t&gt; ().
  Any (ANYLAMBDA); Public T Find (expression&lt;func&lt;t, bool&gt;&gt; wherelambda) {T _entity = ncontext.set&lt;t&gt; ().
   Firstordefault&lt;t&gt; (WHERELAMBDA);
  return _entity; Public iqueryable&lt;t&gt; findlist&lt;s&gt; (expression&lt;func&lt;t, bool&gt;&gt; wherelamdba, bool ISASC, Expressi On&lt;func&lt;t, s&gt;&gt; orderlamdba) {var _list = ncontext.set&lt;t&gt; ().
   Where&lt;t&gt; (WHERELAMDBA); if (ISASC) _list = _list.
   Orderby&lt;t, s&gt; (ORDERLAMDBA); else _list = _list.
   Orderbydescending&lt;t, s&gt; (ORDERLAMDBA);
  return _list; iqueryable&lt;t&gt; findpagelist&lt;s&gt; (int pageIndex, int pageSize, out int Totalrecord, Expression&lt;func &lt;t, bool&gt;&gt; WHERELAMDBA, bool Isasc, expression&lt;func&lt;t, s&gt;&gt; orderlamdba) {var _list = NContext. Set&lt;t&gt; ().
   Where&lt;t&gt; (WHERELAMDBA); Totalrecord = _list.
   Count (); if (ISASC) _list = _list. Orderby&lt;t, S&gt; (ORDERLAMDBA). Skip&lt;t&gt; ((pageIndex-1) * pageSize).
   Take&lt;t&gt; (pageSize); else _list = _list. Orderbydescending&lt;t, S&gt; (ORDERLAMDBA). Skip&lt;t&gt; ((pageIndex-1) * pageSize).
   Take&lt;t&gt; (pageSize);
  return _list; }
 }
}

The operation of the database is all in the code. More interesting is this sentence protected nineskydbcontext Ncontext = Contextfactory.getcurrentcontext ();

Contextfactory is a simple factory class, and Getcurrentcontext () is a static function. Use a simple factory to get the current dbcontext within the request, which is the DbContext single case within the request. Add a factory class first contextfactory

Using System.Data.Entity;
Using System.Runtime.Remoting.Messaging;

Namespace Ninesky.dal
{
 ///<summary>
 ///context Simple factory
 ///<remarks>
 ///creation: 2014.02.05
 ///</remarks>
 ///</summary> public
 class Contextfactory
 {

  ///<summary>
  ///Get Current data context
  ///</summary>
  ///<returns></returns> public
  static Nineskydbcontext Getcurrentcontext ()
  {
   Nineskydbcontext _ncontext = CallContext.GetData ("NineskyContext" ) as Nineskydbcontext;
   if (_ncontext = = null)
   {
    _ncontext = new Nineskydbcontext ();
    Callcontext.setdata ("Nineskycontext", _ncontext);
   }
   return _ncontext;
  }
 }

This is to first get the nineskycontext in the CallContext, initialize a nineskycontext if it is empty, and return directly if it exists. See CALLCONTEXT,MSDN in the CallContext provides data slots that are unique to each logical thread of execution, and in Web applications, Each request happens to be a logical thread, so you can use CallContext to implement the DbContext single case within a single request.

Add the specific warehousing code below.

Add a Userrepository class to the Dal and inherit from Baserepository and Interfaceuserrepository. The goal is to inherit from the Baserepository class and implement the Interfaceuserrepositor interface.

Using Ninesky.idal;
Using Ninesky.models;
Using System.Linq;

Namespace Ninesky.dal
{
 ///<summary>
 ///user Warehouse
 ///<remarks> Create: 2014.02.03</remarks >
 ///</summary>
 class Userrepository:baserepository<user>, Interfaceuserrepository
 {
 }
}

Userrepository directly inherits the methods in the base class, the methods in the base class can meet most of the needs, userrepository no longer need to add functions, other repository classes are similar, do not paste code.

Here we are building a repository factory to return all the repository classes in the project.

Using Ninesky.idal;

Namespace Ninesky.dal
{
 ///<summary>
 ///simple factory?
 ///<remarks> Create:2014.02.03</remarks>
 ///</summary> public
 Static class Repositoryfactory
 {
  ///<summary>
  ///user warehousing
  ///</summary> public
  static Interfaceuserrepository Userrepository {get {return new userrepository ();}}}}

Later in the BLL call when you do not have to write interfaceuserrepository every time _iuserrsy = new Userrepository (), directly written interfaceuserrepository _iuserrsy = The benefit of this repositoryfactory.userrepository is that we can create a new class directly when the class that implements the Interfaceuserrepository interface in the DAL project needs to be modified, and then Repositoryfactory class to let us Errepository property returns the new class on the line.

3. IBLL Project
IBLL is the interface of business logic layer, and the operation of the business logic layer to the database is increased, deleted and changed. Also write a base interface to write these three operations, which is similar to the idal mentality.

Namespace Ninesky.ibll
{
 ///<summary>
 ///Interface base class
 ///<remarks> Create: 2014.02.03</remarks >
 ///</summary> public
 interface interfacebaseservice<t> where T:class
 {
  ///< summary>
  ///Add
  ///</summary>
  ///<param name= "entity" > Data entities </param>
  /// <returns> after the addition of the data entity </returns>
  t Add (t entity);

  <summary>
  ///update
  ///</summary>
  ///<param name= "entity" > Data entity </param>
  ///<returns> Success </returns>
  bool Update (T entity);

  <summary>
  ///Delete
  ///</summary>
  ///<param name= "entity" > Data entity </param>
  ///<returns> Success </returns>
  bool Delete (T entity);
 }


After adding a Interfaceuserservice interface, inherit from Interfacebaseservice. Add a few more methods to the interface as needed. The name of the Find method is unified here, and the name of the returned entity class is find () or findbyxxx (), the method name of the returned group of data is findlist () or findxxxlist, and the name of the paging is findpagelist () or Findxxxpagelist ()

Using Ninesky.models;

Using System.Linq; namespace Ninesky.ibll {///&lt;summary&gt;///user-related interface///&lt;remarks&gt;///creation: 2014.02.09///&lt;/remarks&gt;/ &lt;/summary&gt; public interface Interfaceuserservice:interfacebaseservice&lt;user&gt; {///&lt;summary&gt;// /user Presence///&lt;/summary&gt;///&lt;param name= "UserName" &gt; Username &lt;/param&gt;///&lt;returns&gt; boolean &lt;/returns&
  Gt

  BOOL Exist (string userName); &lt;summary&gt;///Find Users///&lt;/summary&gt;///&lt;param name= "UserID" &gt; User id&lt;/param&gt;///

  s&gt;&lt;/returns&gt; User Find (int userID); &lt;summary&gt;///Find Users///&lt;/summary&gt;///&lt;param name= "UserName" &gt; Username &lt;/param&gt;///&lt;retur

  Ns&gt;&lt;/returns&gt; User Find (string userName); &lt;summary&gt;///user list///&lt;/summary&gt;///&lt;param name= "pageIndex" &gt; page number &lt;/param&gt;///&lt;para M name= "pageSize" &gt; per page record number &lt;/param&gt;///&lt;param name= "Totalrecord" &gt; TotalRecord &lt;/param&gt;///&lt;param name= "Order" &gt; Sort: 0-id ascending (default), 1ID Descending, 2 registration time ascending, 3 registration time descending, 4 logon time ascending, 5 logon time descending &lt;/param&gt;/ &lt;returns&gt;&lt;/returns&gt; iqueryable&lt;user&gt; findpagelist (int pageIndex, int pageSize, out int Totalrecord
 , int order);

 }
}

4. BLL Project
to implement the Interfaceuserservice interface in the BLL project, first add the Baseservice

Using NINESKY.IBLL;
Using Ninesky.idal;
Namespace Ninesky.bll
{
 ///<summary>
 ///Service base class
 ///<remarks> Create: 2014.02.03</remarks >
 ///</summary> Public
 abstract class baseservice<t>: interfacebaseservice<t> where T: Class
 {
  protected interfacebaserepository<t> currentrepository {get; set;}

  Public Baseservice (interfacebaserepository<t> currentrepository) {currentrepository = currentRepository;}

  Public T-Add (t entity) {return Currentrepository.add (entity);}

  public bool Update (T entity) {return currentrepository.update (entity);}

  public bool Delete (T entity) {return Currentrepository.delete (entity);}}}

The constructor of this class to pass in a parameter is the currentrepository which is passed on at the time of inheritance. Here we still look at the user class.

Using Ninesky.dal;
Using NINESKY.IBLL;
Using Ninesky.models;

Using System.Linq;  namespace Ninesky.bll {///&lt;summary&gt;///User Service class///&lt;remarks&gt;///Create: 2014.02.12///&lt;/remarks&gt;/// &lt;/summary&gt; public class Userservice:baseservice&lt;user&gt;,interfaceuserservice {public UserService (): Base ( repositoryfactory.userrepository) {} public bool Exist (string userName) {return currentrepository.exist (U =&gt; u.use

  Rname = = UserName);}

  Public User Find (int userID) {return currentrepository.find (u =&gt; u.userid = = UserID);}

  Public User Find (string userName) {return currentrepository.find (u =&gt; u.username = = userName); Public iqueryable&lt;user&gt; findpagelist (int pageIndex, int pageSize, out int totalrecord, int order) {switch (Orde R) {case 0:return currentrepository.findpagelist (PageIndex, pageSize, out Totalrecord, U =&gt; true, true, U =&gt ;
    U.userid); Case 1:return Currentrepository.findpagelist (PageIndex, PageSize, OUT totalrecord, u =&gt; true, False, U =&gt; u.userid); Case 2:return Currentrepository.findpagelist (PageIndex, pageSize, out Totalrecord, U =&gt; true, True, u =&gt; U.registra
    Tiontime); Case 3:return Currentrepository.findpagelist (PageIndex, pageSize, out Totalrecord, U =&gt; true, False, U =&gt; u.registr
    Ationtime); Case 4:return Currentrepository.findpagelist (PageIndex, pageSize, out Totalrecord, U =&gt; true, True, u =&gt; u.logintim
    e); Case 5:return Currentrepository.findpagelist (PageIndex, pageSize, out Totalrecord, U =&gt; true, False, U =&gt; u.loginti
    ME); Default:return currentrepository.findpagelist (PageIndex, pageSize, out Totalrecord, U =&gt; true, True, U =&gt; u.userid)
   ;
 }
   
  }
 }
}

The above findpagelist code is too cumbersome to think of a good way.

5, summary
Writing here today or thinking about the implementation of the call between projects, wrote two base interfaces, two base classes, and later other classes inherit from them, writing is similar. The next time you can start to do the interface, in the Ninesky.web project is basically through the IBLL,BLL with the data to deal with.

===================================================

Findpagelist () This sort of method is really not very common, the code is modified as follows:

1, Interface Interfacebaserepository

Modify the two interface methods as shown in the Figure Red Box section.

Image

2, Baserepository class

Add the by-way method with the following code:

&lt;summary&gt;///sort///&lt;/summary&gt;///&lt;typeparam name= "T" &gt; Type &lt;/typeparam&gt;///&lt;param Name= "source" &gt; Original iqueryable&lt;/param&gt;///&lt;param name= "propertyname" &gt; Sort attribute name &lt;/param&gt;///&lt;param Name= "ISASC" &gt; whether positive sequence &lt;/param&gt;///&lt;returns&gt; sorted iqueryable&lt;t&gt;&lt;/returns&gt; Private IQueryable &lt;T&gt; (iqueryable&lt;t&gt; Source, String PropertyName, bool Isasc) {if (Source = null) throw new Argume
   Ntnullexception ("source", "cannot be empty"); if (string.
   IsNullOrEmpty (PropertyName)) return source; var _parameter = expression.parameter (source.
   ElementType);
   var _property = Expression.property (_parameter, PropertyName);
   if (_property = = null) throw new ArgumentNullException ("PropertyName", "property does not exist");
   var _lambda = Expression.lambda (_property, _parameter); var _methodname = Isasc?
   "By": "OrderByDescending"; var _resultexpression = Expression.call (typeof (Queryable), _methodname, new type[] {source. ELementtype, _property. Type}, source.
   Expression, Expression.quote (_LAMBDA)); return source.
  Provider.createquery&lt;t&gt; (_resultexpression); Modify the Findlist and Findpagelist methods, modify the following figure Image 3, modify the Findpagelist method of UserService The following code: public iqueryable&lt;user&gt; Findpag
   Elist (int pageIndex, int pageSize, out int totalrecord, int order) {bool _ISASC = true; String _ordername = String.
   Empty;
     Switch (order) {case 0: _isasc = true;
     _ordername = "UserID";
    Break
     Case 1: _isasc = false;
     _ordername = "UserID";
    Break
     Case 2: _isasc = true;
     _ordername = "Registrationtime";
    Break
     Case 3: _isasc = false;
     _ordername = "Registrationtime";
    Break
     Case 4: _isasc = true;
     _ordername = "Logintime";
    Break
     Case 5: _isasc = false;
     _ordername = "Logintime";
    Break
     Default: _isasc = false;
     _ordername = "UserID";
   Break Return Currentrepository.findpagelist (PageIndex, PageSize, out Totalrecord, u =&gt; true, _ordername, _ISASC); }

Above is the ASP.net MVC5 website Development framework model, data storage and business logic related introduction, than the previous two sections of the content is not richer, I hope this article can be helpful to everyone's study.

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.