Architecture and basic functions of the business logic layer of ASP. NET MVC5 website development (4) mvc5 website development
The business logic layer is implemented in Ninesky. Core. The main function encapsulates some methods to provide services to the interface layer by calling the data storage layer.
I. architecture of the business logic layer
Ninesky. Core contains three namespaces: Ninesky. Core, Ninesky. Core. Types, Ninesky. Core. General.
Ninesky. Core contains the model and function implementation. Ninesky. Core. Types is the definition of some Types used by the project. Ninesky. Core. General is the definition of some methods used by the project.
1. Structure of Ninesky. Core namespace
NineskyContext-data context
ContextFactory-factory class for retrieving data context
BaseManager-basic class, which implements some common data access methods and provides inheritance for other management classes.
Category-topic model.
CategoryManager-column management class.
Content-Content model.
ContentManager-content management class.
User-User model
UserManager-user management
Administrator-Administrator class
AdministratorManager-administrator management
2. Structure of the Ninesky. Core. Types namespace
Response class.
Paging <T> Paging data class.
II. Implementation of basic functions
1. Add reference
(1) Add EntityFramewok reference
Ninesky. Core Project-> reference [Right-click]-> Manage NuGet packages
In the NuGet package management peer box, select EntityFramewok and install it.
(2) Add references to the Ninesky. DataLibrary Project
Ninesky. Core Project-> reference [Right-click]-> Add reference
In the reference manager, select project> solution> Ninesky. DataLibrary and click OK.
2. NineskyContext class
The NineskyContext class is the data context of the project, so that the model corresponds to the database table.
Ninesky. Core Project [Right-click]-> Add-> class, and enter the class name NineskyContext.
Introduce the namespace System. Data. Entity in the class;
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Data.Entity;namespace Ninesky.Core{ public class NineskyContext:DbContext { public NineskyContext():base("DefaultConnection") { Database.SetInitializer<NineskyContext>(new CreateDatabaseIfNotExists<NineskyContext>()); } }}
3. ContextFactory class
ContextFactory is a simple factory class, And CurrentContext () is a static function used to obtain the current thread DbContext.
Ninesky. Core Project [Right-click]-> Add-> class, and enter the class name ContextFactory.
Add a reference to System. Runtime. Remoting. Messaging in the class. Implement the CurrentContext () Static Method in the class to return the data context NineskyContext. The method stores NineskyContext in the CallContext thread.
Using System. runtime. remoting. messaging; namespace Ninesky. core {// <summary> /// data context factory /// </summary> public class ContextFactory {/// <summary> /// obtain the data context of the current thread /// </summary> /// <returns> data context </returns> public static NineskyContext CurrentContext () {NineskyContext _ nContext = CallContext. getData ("NineskyContext") as NineskyContext; if (_ nContext = null) {_ nContext = new NineskyContext (); CallContext. setData ("NineskyContext", _ nContext) ;}return _ nContext ;}}}
4. Response class
The Response class is a common method to return data types, including three attributes: return code, return message, and return data.
In the Ninesky. Core Project [Right-click] to create a folder and enter the name Types.
In the Types folder, [Right-click]-> Add-> class, and enter the class name Response in the Add new project dialog box that appears. The Code is as follows:
Namespace Ninesky. core. types {// <summary> ///// </summary> public class Response {/// <summary> // return code. 0-failed, 1-succeeded, others-For details, see descriptions of return values of Methods /// </summary> public int Code {get; set ;} /// <summary> /// return Message /// </summary> public string Message {get; set ;} /// <summary> /// return Data /// </summary> public dynamic Data {get; set;} public Response () {Code = 0 ;}}}
5. Paging <T> class
The Paging <T> class is used to query Paging data, including the current page, the number of records per page, the total number of records, and the current page data list.
In the Types folder, [Right-click]-> Add-> class, and enter the class name Paging in the Add new project dialog box that appears. The Code is as follows:
Using System. Collections. Generic; namespace Ninesky. Core. Types {public class Paging <T >{/// <summary> // the current page. Count from 1 /// </summary> public int PageIndex {get; set ;}/// <summary> /// the number of records on each page. 20 by default /// </summary> public int PageSize {get; set ;} /// <summary> /// total number of records /// </summary> public int TotalNumber; /// <summary> /// List of records on the current page /// </summary> public List <T> Items {get; set;} public Paging () {PageIndex = 1; PageSize = 20 ;}}}
6. BaseManager class
The BaseManager class is the base class of all management classes, which includes common management methods.
Rename the Class1.cs of the Ninesky. Core Project to BaseManager. cs.
Introduce the namespace System. Data. Entity and Ninesky. Core. Types to implement a common method.
Using Ninesky. core. types; using Ninesky. dataLibrary; using System. data. entity; using System. linq; namespace Ninesky. core {/// <summary> /// basic class of the management class /// </summary> /// <typeparam name = "T"> model class </typeparam> public abstract class BaseManager <T> where T: class {// <summary> /// Data Warehouse class /// </summary> protected Repository <T> Repository; /// <summary> /// default constructor /// </summary> public BaseManager (): this (ContextF Acloud. currentContext ()) {}/// <summary> /// constructor /// </summary> /// <param name = "dbContext"> data context </param> public BaseManager (dbContext dbContext) {Repository = new Repository <T> (dbContext );} /// <summary> /// add /// </summary> /// <param name = "entity"> entity Data </param> /// <returns> when successful, attribute [Data] is the added Data entity </returns> public virtual Response Add (T entity) {Response _ response = new Response (); if (Reposit Ory. Add (entity)> 0) {_ response. Code = 1; _ response. Message = "data added successfully! "; _ Response. Data = entity;} else {_ response. Code = 0; _ response. Message =" failed to add Data! ";} Return _ response ;} /// <summary> /// update /// </summary> /// <param name = "entity"> entity Data </param> /// <returns> when successful, the attribute [Data] is the updated Data entity </returns> public virtual Response Update (T entity) {Response _ response = new Response (); if (Repository. update (entity)> 0) {_ response. code = 1; _ response. message = "data updated! "; _ Response. Data = entity;} else {_ response. Code = 0; _ response. Message =" An error occurred while updating the Data! ";} Return _ response ;} /// <summary> /// Delete /// </summary> /// <param name = "ID"> Primary Key </param> /// <returns> Code: 0-failed to Delete; 1-failed to Delete; 10-record does not exist </returns> public virtual Response Delete (int ID) {Response _ response = new Response (); var _ entity = Find (ID); if (_ entity = null) {_ response. code = 10; _ response. message = "the record does not exist! ";}Else {if (Repository. Delete (_ entity)> 0) {_ response. Code = 1; _ response. Message =" data deleted! ";}Else {_ response. Code = 0; _ response. Message =" An error occurred while deleting the data! ";}} Return _ response ;} /// <summary> /// find the object /// </summary> /// <param name = "ID"> Primary Key </param> /// <returns> entity </returns> public virtual T Find (int ID) {return Repository. find (ID );} /// <summary> /// query the data list-[All data] /// </summary> /// <returns> all data </returns> public IQueryable <T> FindList () {return Repository. findList ();} /// <summary> /// query paging data /// </summary> /// <param name = "paging"> paging data </param> /// <returns> Paging data </returns> public Paging <T> FindPageList (paging <T> Paging) {paging. items = Repository. findPageList (paging. pageSize, paging. pageIndex, out paging. totalNumber ). toList (); return paging ;} /// <summary> /// total number of records /// </summary> /// <returns> total number of records </returns> public virtual int Count () {return Repository. count ();}}}
============================================
See: https://ninesky.codeplex.com/SourceControl/latest for code
CODE Download: https://ninesky.codeplex.com click source code click Download to Download the SOURCE file.
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.