Step by step, learn ASP. net mvc 1.0 to create the NerdDinner sample program, Part 4

Source: Internet
Author: User

This article continues with the study and implements the NerdDinner sample program through ASP. net mvc 1.0 step by step.

Create ModelModel

In the Model-View-Controller framework, model indicates the Data Objects of an application and the corresponding business logic, including data verification and business rules. Model is the core part of the MVC application. The role of model is shown below.

ASP. net mvc Framework supports the use of any data access technology, developers can choose a large number. NET data access technology implementation model, such as LINQ to Entities, LINQ to SQL, nhib.pdf, LLBLGen Pro, SubSonic, WilsonORM, or basic ADO. NET DataReaders, DataSets, and so on.

For this example program NerdDinner, we will use LINQ to SQL to create a simple business domain model, which is very close to the database design and adds some customized validation logic and business rules. Next we will implement a repository class to help implement abstract data entities, allowing us to easily implement unit tests.

LINQ to SQL

LINQ to SQL is THE ORM (Object Relational er ER) part of. NET 3.5. LINQ to SQL provides a simple way to map data tables to. NET classes. For our NerdDinner sample program, we map Dinners and RSVP data tables to the Dinner and RSVP model classes. Columns in the Dinners and RSVP data tables are mapped to the attributes of the Dinner and RSVP model classes. Each Dinner And RSVP object represents a separate data record in the Dinners or RSVP data table.

To avoid manual creation of SQL scripts, We can manually create SQL scripts to retrieve and update Dinner And RSVP objects from the database. When you access or update database records, LINQ to SQL generates appropriate SQL Execution logic. We can write Query expressions using the LINQ languages supported by VB and C # To retrieve Dinner And RSVP objects. This can greatly reduce code lines and build clear applications.

Add LINQ to SQLClass to the Project Program

Now we can go back to our NerdDinner application! Right-click the Models folder and choose Add> New Item.

In the Add New Item dialog box, as shown in. Select the LINQ to SQL Classes template, name it NerdDinner. dbml, and click Add. Visual Studio adds the NerdDinner. dbml file to the \ Models directory and automatically opens the LINQ to SQL Object Relational designer.

Use LINQ to SQLCreate Data Model Classes

LINQ to SQL allows us to quickly type data models from existing databases. In the Server Explorer window, open the NerdDinner database and select the data table to be modeled. Drag the Dinners and RSVP tables to the NerdDinner. dbml designer, as shown in.

By default, when you create a category based on a data table, the LINQ to SQL designer automatically plural the table and column name. For example, the Dinners table in the example automatically generates the Dinner class. This type of naming helps our model classes comply with. NET naming rules. However, if you do not like the class or attribute name automatically generated by the designer, you can edit it in the designer or update it through the attribute list.

By default, the LINQ to SQL designer automatically monitors the primary key/foreign key relationships and creates associations between different model classes based on these relationships. As shown in, when we drag the Dinners and RSVP tables to the LINQ to SQL designer, one-to-many associations are automatically created, which is expressed by an arrow in the figure.

The preceding association relationship adds a strongly typed Dinner attribute to the RSVP class for LINQ to SQL. developers can use the Dinner attribute of the specified RSVP to access the associated entities. The Dinner class will also have a strong type of RSVP set attribute, allowing developers to retrieve and update the RSVP object set associated with the Dinner.

Next, let's take a look at the smart tip of Visual Studio when you create an RSVP object and add it to the Dinner's RSVP set:

From the preceding figure, we can see how to create a RSVPs set for the Dinner object using the join data table Dinner And RSVP.

If you do not like the designer's modeling or naming Association, you can change it. Click the association arrow in the designer to access the Properties window, which can be renamed, deleted, or changed. For the NerdDinner application, we will use the default settings.

Today, we are here to continue translating ASP. net mvc eBook tomorrow-developing the NerdDinner sample program wizard. Welcome to http://blog.EntLib.com blog platform for more MVC learning materials.

Download link for related resources:

1. ASP. net mvc 1.0 official version released !!!

Http://forum.entlib.com/Default.aspx? G = posts & t = 464

2. Free ASP. net mvc eBook wizard-eBook download

Http://blog.entlib.com/entlibforum/archive/2009/03/12/asp.net-mvc-ebook.aspx

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.