The architecture and basic functions of the business logic layer of ASP.net MVC5 website Development (IV.) practical skills

Source: Internet
Author: User

The business logic layer is implemented in Ninesky.core, and the main functions encapsulate some methods to provide services to the interface layer by invoking the data storage layer.

The architecture of the business logic layer

Ninesky.core contains three namespaces Ninesky.core, Ninesky.Core.Types, Ninesky.Core.General.

Ninesky.core contains model and function implementations, Ninesky.Core.Types are some of the types of definitions used in the project, Ninesky.Core.General are definitions of some of the methods used in the project.

1, the structure of Ninesky.core namespaces

nineskycontext-Data context

Contextfactory-the factory class that gets the data context

Basemanager-Basic class, implements some common data access methods, provides other management class inheritance.

category-column model.

categorymanager-column Management class.

content-content model.

contentmanager-Content Management class.

user-User Model

usermanager-User Management class

administrator-Administrator Class

administratormanager-Administrator Management Class

2, the structure of Ninesky.Core.Types namespaces

The Response response returns the class.

Paging<t> the paging data class.

Second, the realization of the basic function

1. Add Reference

(1), add Entityframewok Reference

Ninesky.core Project-> Reference "right key" –> Management NuGet Package

Select Entityframewok and install in the NuGet package management pair box.

(2), adding references to Ninesky.datalibrary items

Ninesky.core Project-> Reference "right key" –> Add Reference

Select the project-> solution->ninesky.datalibrary in the Reference manager and click OK.

2, Nineskycontext class

The Nineskycontext class is the data data context of the project, which corresponds to the table of the database and the model.

Ninesky.core item "Right key"-> Add-> class, enter class name Nineskycontext.

Introducing namespace System.Data.Entity into 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, CurrentContext () is a static function used to get the current thread dbcontext.

Ninesky.core item "Right key"-> Add-> class, enter class name Contextfactory.

Adds a reference to the System.Runtime.Remoting.Messaging in the class. Implement the CurrentContext () static method in the class to return the data context Nineskycontext. method to store the Nineskycontext through the CallContext class thread.

Using System.Runtime.Remoting.Messaging;

Namespace Ninesky.core
{
 ///<summary>
 ///Data context factory
 ///</summary> public
 class Contextfactory
 {
  ///<summary>
  ///Gets 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 commonly used method of returning data types, containing 3 properties for return code, return message, and return data.

In the Ninesky.core project, create a new folder and enter a name types.

In the Types folder [right-click]-> Add the-> class, enter the class name response in the pop-up Add New Entry dialog box. The code is as follows:

Namespace Ninesky.Core.Types
{
 ///<summary>
 /// 
 ///</summary> Public
 class Response
 {
  ///<summary>
  ///return code. 0-Failure, 1-success, others-see method return value Description
  ///</summary>
  Public int Code {get; set;}

  <summary>
  ///returns messages
  ///</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 a class that is used when querying paging data, including several properties such as 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 the-> class, enter the class name paging in the pop-up Add New Entry dialog box. The code is as follows:

Using System.Collections.Generic;

Namespace Ninesky.Core.Types
{public
 class paging<t>
 {
  ///<summary>
  ///the current page. From 1 count
  ///</summary> public
  int PageIndex {get; set;}

  <summary>
  ///The number of records per page. Default
  ///</summary> public
  int PageSize {get; set;}

  <summary>
  ///Total record number
  ///</summary> public
  int totalnumber;///<summary>
  /// Current page record list
  ///</summary> public
  list<t> the Items {get; set;}
  
  Public paging ()
  {
   PageIndex = 1;
   PageSize =;}
  }


6, Basemanager class

The Basemanager class is the base class for all management classes, and this class contains common methods for managing classes.

Rename the Class1.cs of the Ninesky.core project to BaseManager.cs

Introduce namespaces System.Data.Entity and Ninesky.Core.Types to implement common methods.

Using Ninesky.Core.Types;
Using Ninesky.datalibrary;
Using System.Data.Entity;

Using System.Linq; namespace Ninesky.core {///<summary>///Management class base class///</summary>///<typeparam name= "T" > Model class </typ Eparam> public abstract class basemanager<t> where T:class {///<summary>///Data warehousing class///</summ

  ary> protected repository<t> Repository;
  <summary>///Default constructor///</summary> public Basemanager (): This (Contextfactory.currentcontext ()) { ///<summary>///Constructors///</summary>///<param name= "DbContext" > Data context </param> publi
  C Basemanager (DbContext dbcontext) {Repository = new repository<t> (dbcontext); ///<summary>///Add///</summary>///<param name= "entity" > Entity Data </param>///<ret When Urns> succeeds, the property "data" is the added data entity </returns> public virtual Response Add (T entity) {Response _response = new Respo
   NSE (); if (Repository. ADD (Entity) >0) {_response.
    Code = 1; _response. Message = "Add Data Success!"
    "; _response.
   Data = entity; else {_response.
    Code = 0; _response. Message = "Failed to add data!"
   ";
  return _response; ///<summary>///update///</summary>///<param name= "entity" > Entity Data </param>///<ret urns> Property "Data" as an updated data entity </returns> public virtual Response update (T entity) {Response _response = new Re
   Sponse (); if (Repository.update (entity) > 0) {_response.
    Code = 1; _response. message = "Update data Successful!"
    "; _response.
   Data = entity; else {_response.
    Code = 0; _response. message = "Update data failed!"
   ";
  return _response; }///<summary>///delete///</summary>///<param name= "ID" > Primary key </param>///<returns& Gt code:0-Delete failed; 1-delete Chen Gong 10-record does not exist </returns> public virtual Response Delete (int ID) {Response _response = new Respo
   NSE ();
   var _entity = find (ID); if (_entity = = null) {_response.
    Code = 10; _response. Message = "Record does not exist!"
   "; } else {if (Repository.delete (_entity) > 0) {_response.
     Code = 1; _response. message = "Delete data success!"
    "; else {_response.
     Code = 0; _response. message = "Delete data failed!"
    ";
  } return _response; ///<summary>///Lookup entity///</summary>///<param name= "ID" > Primary key </param>///<returns
  > Entity </returns> public virtual T find (int id) {return repository.find (ID); ///<summary>///Find list of data-"all data"///</summary>///<returns> All data </returns> public iqu
  Eryable<t> findlist () {return repository.findlist (); ///<summary>///Find 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 record number///</summary>///<returns> Total record number </returns> public virtual int
  Count () {return repository.count ();

 }
 }
}

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

Code See: Https://ninesky.codeplex.com/SourceControl/latest

Code Download: https://ninesky.codeplex.com Click Source code click Download to download the original file.

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.