Use ASP. net mvc and LINQ to create a simple blog-Part 3

Source: Internet
Author: User

Original article address: building_a_simple_blog_engine_with_aspnet_mvc_and_linq _ part_3
Author:Keyvan nayyeri

Address: http://www.cnblogs.com/QLeelulu/archive/2008/03/04/1090263.html
Author: Q. Lee. Lulu

Related Links

Use ASP. net mvc and LINQ to create a simple blog-Part 1

Use ASP. net mvc and LINQ to create a simple blog-Part 2

 

Summary
In the third part of this series, keyvan discusses the data model of his simple blog. He demonstrated some of the concepts related to LINQ in the data model, retrieved the data in the controller, and passed it to the view.

Content

    • Introduction
    • Data Model
    • Database Structure
    • LINQ to SQL
    • Retrieve Data
    • Update Controller
    • Summary

Introduction

In this seriesArticleI have discussed the MVC mode and ASP. net MVC framework, also discusses the concept of controller in MVC Mode and Its.. Net MVC framework.

In this discussion, I hope to use the MVC Framework as a simple blog engine to implement kblog and use it as an example to display the concepts of ASP. NET MVC framework.

After learning about controllers and their basic principles, we need to find a way to load data from the data storage in the data model and pass them to the view.

In the kblog, I use three simple tables of SQL express database, and you will find them later. In this Part of this series of articles, I will introduce another important component of the MVC framework data model, while in the controller, the database structure will use LINQ to SQL to load data and pass the loaded data from LINQ to the view.

Before entering the text of the article, let me talk about the data model concept in the data section and kblog and MVC, although you will see some final purposes of our data model in the article.

Data Model

The data model is one of the three important components of the MVC model, and is responsible for maintaining the status. The data model retrieves data from the data storage and transmits the data to the Controller. Status data is usually stored in the data storage system, and you can retrieve them in several ways.

The ASP. net mvc Framework does not care about your memory system, but what you need to do is to transfer your data to the Controller class in some way. This can be done through the underlying APIs of ADO. net, such as datareader and dataset, or through the LINQ to SQL class. Here, kblog will be used in the near future, and will become popular with the LINQ to SQL (now we don't think it is a common thing in real-world engineering ).

In general, the data model is a proxy class that loads data as an object and can transmit data to the Controller class. For ease, many developers prefer to use some additional help classes to simplify the work of using data models and encapsulate internal operations to a certain extent. You will see this at the end of the article.

Database Structure

The database structure of kblog is very simple. It has three tables named posts, comments, and categories. They have some obvious relationships, as shown in figure 1.

Figure 1

The structure of this database is very clear, and everyone who has a very basic understanding of the blog engine can know what is going on, so I will not waste your time describing this database.

LINQ to SQL

In kblog, I want to use a LINQ to SQL mechanism to load data to the data model and pass it to the Controller's actions. Using LINQ to SQL is quite easy and will become a common. net developer's general knowledge, but now it is a new technology, let me point out that you can add a new LINQ to SQL class project to your asp.. Net MVC project in the models folder. 2:

Figure 2

After adding this item to your project, you can drag the database table to the designer without having to do anything else, because this is what we need in this example.

Your LINQ to SQL designer looks like Figure 3:

Figure 3

Retrieve Data

As mentioned in the previous sections of this series, developers usually prefer to use a proxy class that hides internal data operations to load data. So I create a class and name it kblogdatacontext to define some methods, which will load data objects for me and I can easily use them in my controller action methods.

The methods included in this class use my LINQ to SQL and LINQ commands to load data. This classCodeIn listing 1.

Listing 1

Using System;
Using System. Data;
Using System. LINQ;
Using System. Collections. Generic;
 
Namespace Kblog. Models
{
Public   Class Kblogdatacontext
{
Public List < Post > Getcategoryposts ( String Categoryname)
{
Kblogdbdatacontext Context
= NewKblogdbdatacontext ();
ReturnContext. Posts. Where (
P
=>P. Category. Title=Categoryname). tolist ();
}

 
Public List < Post > Getrecentposts ( Int Count)
{
Kblogdbdatacontext Context
= NewKblogdbdatacontext ();
ReturnContext. Posts. Take (
Count). orderbydescending (P
=>P. publisheddate). tolist ();
}

 
Public Post getpost ( Int ID)
{
Kblogdbdatacontext Context
= NewKblogdbdatacontext ();
ReturnContext. Posts. Where (P=>P. ID=ID). Single ();
}

 
Public List < Category > Getcategories ()
{
Kblogdbdatacontext Context
= NewKblogdbdatacontext ();
ReturnContext. categories. tolist ();
}

}

}

Note that I use some simple LINQ expressions in this example, but I am not involved in their details because they are easy to read and understand and beyond the scope of our article.

Four methods are defined here:

· Getcategoryposts: returns a post list of specific categories by category name.

· Getrecentposts: returns the last n posts of the blog to be displayed on the homepage.

· Getpost: returns a single post using an identifier.

· Getcategories: returns a list of all categories.

Although I can remove this class, I strongly recommend that youProgramIn the same way to get good encapsulation for your data operations in the Controller and get more simple and readable code.

Update Controller 

Good. After learning about the data model, it is time to apply the data model method to update the implementation of our controller class, load data and transmit data to the view.

If you can remember the second part of this series, we have three controller classes, which contain three action methods. Here we use the data model method to update them.

The first update is homecontroller, which is the homepage action method. The update code is in Listing 2.

Listing 2:

 

Using System;
Using System. Web;
Using System. Web. MVC;
Using Kblog. models;
Using System. Collections. Generic;
 
Namespace Kblog. Controllers
{
Public   Class Homecontroller: Controller
{
// Sample URL:/default. aspx
[Controlleraction]
Public   Void Index ()
{
Kblogdatacontext datacontext
=   New Kblogdatacontext ();
List
< Post > Posts = Datacontext. getrecentposts ( 10 );
 
Renderview (
" Index " , Posts );
}

}

}

As you can see, I use the kblogdatacontext class and its getrecentposts method to retrieve a posts list and pass it to the renderview method. Renderview is a method for obtaining status data and view names and passing data to the view. The remaining work will be processed in the view, which we will find in future articles.

The second controller is categoriescontroller. Here we retrieve all posts data of a specific category. The updated code is shown in listing 3.

Listing 3:

Using System;
Using System. Web;
Using System. Web. MVC;
Using Kblog. models;
Using System. Collections. Generic;
 
Namespace Kblog. Controllers
{
Public   Class Categoriescontroller: Controller
{
// Sample URL:/category/DOTNET
[Controlleraction]
Public   Void Category ( String Name)
{
Kblogdatacontext datacontext
=   New Kblogdatacontext ();
List
< Post > Posts = Datacontext. getcategoryposts (name );
 
Renderview (
" Category " , Posts );
}

}

}

The last controller is postscontroller, which loads individual post data and passes it to the corresponding view. (Liting 4 ).

Listing 4:

 

Using System;
Using System. Web;
Using System. Web. MVC;
Using Kblog. models;
Using System. Collections. Generic;
 
Namespace Kblog. Controllers
{
Public   Class Postscontroller: Controller
{
// Sample URL:/post/25
[Controlleraction]
Public   Void Post ( Int ID)
{
Kblogdatacontext datacontext
= NewKblogdatacontext ();
Post post
=Datacontext. getpost (ID );
 
Renderview (
"Post", Post );
}

}

}

There is only one thing you may ask about the details of the renderview method and view. We will discuss more details about the view in the next article, so don't worry.

Summary

The third part of this series of articles about ASP. Net MVC framework is all about the data model components of the MVC model. First, I introduced you to the data model, and then gave an overview of the database structure. After that, you will see a quick overview of using LINQ to SQL to load data, the next topic is to apply the data in the data model to retrieve data from the database, and finally use the data model in the Controller to transmit data to the view.

Later, you will read more about unit testing, views, and URL routing.

In the next section, I will publish the kblog for anyone who wants to write their own code.Source code.

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.