ASP. net mvc Case Study (1) (1)

Source: Internet
Author: User

Preface

ASP. net mvc, as Microsoft's official MVC solution, has been launched for some time. It can be said that since its launch, it has been widely used. After a long preview, we finally launched its beta version a few days ago. In addition, in the official documents, Microsoft declared that the final official version would not change much compared with the beta version. Therefore, it is time for developers on the. NET platform to learn ASP. net mvc.

This series of articles, as an introductory tutorial on ASP. net mvc, will not introduce the concepts and theories. Instead, we use case studies to learn ASP. net mvc. In this series of articles, I will gradually complete an "announcement publishing system ". My writing strategy is to do it first, instead of explaining the concepts and theories first.

In addition, I have several points to note:

1. In order to fully focus on ASP. net mvc, the business processing of this demo will use mock. That is, it does not actually access the database, but virtual data.

2. This demo will not consider any art issues.

Next, let's get started with ASP. net mvc. In this article, we will finish all the preparations.

Configure the environment

If ASP. net mvc has not been installed, download and install it here.

After the download, follow the prompts to install it.

Create a project

After installing ASP. net MVC, create a project in Vs, you can see that there is an "ASP. net MVC web application option, select it, create a new project, and name it "mvcdemo ".

After the project is created, you can see that many folders have been created by default. Here we will briefly describe the role of each folder.

Content-stores some resource files, fragments, and CSS required by the application.

Controllers -- stores the Controller class.

Models -- stores business model components.

Scripts -- stores Javascript script files.

Views -- stores views.

It doesn't matter if you don't know some concepts. I will explain them later.

Preparations

After creating a project, we need to create a mock business model. In this way, we will directly use these "spoofing" business models for business processing, focus on ASP. net MVC learning.

First, create three folders under models, namely entities, interfaces, and mockmodels, to store entity classes, interfaces, and mock business models respectively.

Entities have two categories: categoryinfo and announceinfo, which are the announcement category and the announcement entity class respectively. The Code is as follows:

Categoryinfo. CS:

using System;using System.Collections.Generic;using System.Linq;using System.Web;
Namespace mvcdemo. models. entities {// <summary> /// category object class /// </Summary> public class categoryinfo {public int ID {Get; set;} public string name {Get; set ;}}}

Announceinfo. CS:

using System;using System.Collections.Generic;using System.Linq;using System.Web;
Namespace mvcdemo. models. entities {/// <summary> /// announcement entity class // </Summary> public class announceinfo {public int ID {Get; set;} Public String title {Get; set ;}public string content {Get; Set ;}}}

Next, we will define two interfaces that must be implemented by the announcement class service and announcement service. These two interfaces are placed in interfaces.

Icategoryservice. CS:

Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. text;
Using mvcdemo. Models. Entities; namespace mvcdemo. Models. Interfaces
{
/// <Summary>
/// Classification service component interface
/// </Summary>
Public interface icategoryservice
{
/// <Summary>
/// Add a category
/// </Summary>
/// <Param name = "category"> </param>
Void add (categoryinfo category); // <summary>
/// Modify the category name
/// </Summary>
/// <Param name = "ID"> </param>
/// <Param name = "name"> </param>
Void changename (int id, string name); // <summary>
/// Delete a category
/// </Summary>
/// <Param name = "ID"> </param>
Void remove (int id); // <summary>
/// Obtain detailed information about a specific category
/// </Summary>
/// <Param name = "ID"> </param>
/// <Returns> </returns>
Categoryinfo getdetail (int id); // <summary>
/// Retrieve all categories
/// </Summary>
/// <Returns> </returns>
List <categoryinfo> getall ();
}
}

Iannounceservice. CS

Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. text;
Using mvcdemo. Models. Entities; namespace mvcdemo. Models. Interfaces
{
/// <Summary>
/// Announcement service component interface
/// </Summary>
Public interface iannounceservice
{
/// <Summary>
/// Publish an announcement
/// </Summary>
/// <Param name = "announce"> </param>
Void release (announceinfo announce); // <summary>
/// Modify announcement Information
/// </Summary>
/// <Param name = "announce"> </param>
Void notify (announceinfo announce); // <summary>
/// Delete an announcement
/// </Summary>
/// <Param name = "ID"> </param>
Void remove (int id); // <summary>
/// Obtain the details of the announcement
/// </Summary>
/// <Param name = "ID"> </param>
/// <Returns> </returns>
Announceinfo getdetail (int id); // <summary>
/// Obtain all announcements under a specific category
/// </Summary>
/// <Param name = "categoryid"> </param>
/// <Returns> </returns>
List <announceinfo> getbycategory (categoryinfo category );
}
}

Then, we create two mock business logic service models under mockmodels. Note that they must implement their own interfaces.

Mockcategoryservice. CS:

Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. Web;
Using mvcdemo. Models. interfaces;
Using mvcdemo. Models. Entities; namespace mvcdemo. Models. mockmodels
{
/// <Summary>
/// "Spoofing" service component, used to simulate classified business services
/// </Summary>
Public class mockcategoryservice: icategoryservice
{
/// <Summary>
/// Add a category
/// </Summary>
/// <Param name = "category"> </param>
Public void add (categoryinfo category)
{
Return;
} // <Summary>
/// Modify the category name
/// </Summary>
/// <Param name = "ID"> </param>
/// <Param name = "name"> </param>
Public void changename (int id, string name)
{
Return;
} // <Summary>
/// Delete a category
/// </Summary>
/// <Param name = "ID"> </param>
Public void remove (int id)
{
Return;
} // <Summary>
/// Obtain detailed information about a specific category
/// </Summary>
/// <Param name = "ID"> </param>
/// <Returns> </returns>
Public categoryinfo getdetail (int id)
{
Return new categoryinfo
{
Id = ID,
Name = "latest announcement ",
};
} // <Summary>
/// Retrieve all categories
/// </Summary>
/// <Returns> </returns>
Public list <categoryinfo> getall ()
{
List <categoryinfo> categories = new list <categoryinfo> ();
For (INT I = 1; I <= 5; I ++)
{
Categoryinfo Category = new categoryinfo
{
Id = I,
Name = "announcement type" + I,
}; Categories. Add (category );
} Return categories;
}
}
}

Mockannounceservice. CS

Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. Web;
Using mvcdemo. Models. interfaces;
Using mvcdemo. Models. Entities; namespace mvcdemo. Models. mockmodels
{
/// <Summary>
/// "Spoofing" service component, used to simulate announcement business services
/// </Summary>
Public class mockannounceservice: iannounceservice
{
/// <Summary>
/// Publish an announcement
/// </Summary>
/// <Param name = "announce"> </param>
Public void release (announceinfo announce)
{
Return;
} // <Summary>
/// Modify announcement Information
/// </Summary>
/// <Param name = "announce"> </param>
Public void Policy (announceinfo announce)
{
Return;
} // <Summary>
/// Delete an announcement
/// </Summary>
/// <Param name = "ID"> </param>
Public void remove (int id)
{
Return;
} // <Summary>
/// Obtain the details of the announcement
/// </Summary>
/// <Param name = "ID"> </param>
/// <Returns> </returns>
Public announceinfo getdetail (int id)
{
Return new announceinfo
{
Id = ID,
Title = "Announcement on" + ID + ",
Content = "all students work together to perform push-ups at tomorrow morning! ",
};
} // <Summary>
/// Obtain all announcements under a specific category
/// </Summary>
/// <Param name = "categoryid"> </param>
/// <Returns> </returns>
Public list <announceinfo> getbycategory (categoryinfo category)
{
List <announceinfo> announces = new list <announceinfo> ();
For (INT I = 1; I <= 10; I ++)
{
Announceinfo announce = new announceinfo
{
Id = I,
Title = category. Name + "Announcement on" + I + ",
Content = "all students work together to perform push-ups at tomorrow morning! ",
}; Announces. Add (announce );
} Return announces;
}
}
}

We can see that these two classes did not access the database, nor did they implement real business logic. Instead, they just "fabricated" some data to cheat our presentation layer.

Finally, we need to build a generator for generating the business logic model to achieve decoupling between the presentation layer and the business logic layer. Of course, dependency injection is not used here to simplify the complexity. Below is the code of our generator. This class is directly placed under models.

using System;using System.Collections.Generic;using System.Linq;using System.Web;using MVCDemo.Models.Interfaces;using MVCDemo.Models.MockModels;
Namespace mvcdemo. models {// <summary> // service component generation class, used to generate business service components /// </Summary> Public sealed class servicebuilder {// <summary> // create a category service component /// </Summary> /// <returns> classification service component </returns> Public static icategoryservice buildcategoryservice () {return New mockcategoryservice ();}
/// <Summary> /// create an announcement service component /// </Summary> /// <returns> announcement service component </returns> Public static iannounceservice buildannounceservice () {return New mockannounceservice ();}}}

OK. Now we have finished the preparation. After completing these steps, the system's directory structure is shown in:

Summary

In this article, we only talked about what to do in this series of articles and made some preparations for the case. Starting from the next article, we will officially start using ASP. net mvc to complete this case.

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.