Four business logic layer architecture and basic functions of MVC5 website development, mvc5 website development

Source: Internet
Author: User

Four business logic layer architecture and basic functions of MVC5 website development, mvc5 website development

I. architecture of the business logic layer

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

ContextFactoryIs a simple factory class, CurrentContext () is a static function used to obtain the current thread DbContext.

Ninesky. CoreProject [Right-click]->Add->Class, Input class nameContextFactory.

AddSystem. Runtime. Remoting. Messaging. Implement in classCurrentContext () staticMethod returned data contextNineskyContext. TheCallContextClass in-process storageNineskyContext.

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

ResponseClass is a common method to return data types, including three attributes: return code, return message, and return data.

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.

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

BaseManagerClass is the base class of all management classes. This class includes common management methods.

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> private Repository <T> Repository; /// <summary> /// default constructor /// </summary> public BaseManager (): this (ContextFac Invalid. 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 (Repositor Y. 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

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.