[ASP. 07] First ASP.-Create a data model and repository

Source: Internet
Author: User

1. Understanding Concepts

Understand the two concepts first.

Model

A model is a structure type of data and a callable method. For object-oriented programming methods, it is actually a class. A model class is a class that describes the data. Only by describing the data in a certain way can we use it conveniently in the program.

Storage Library

The straightforward explanation is the warehouse where the data is stored. It is often used to store data in a database, and of course there are other ways of storing it, such as file storage. The role of the repository is obviously to save the data permanently.

2. Create a data model

The case project that we defined in the "Get started 06" ASP. NET application (4)-case description is simple and requires only a simple data model. Here are the steps to add this data model (class).

Operation Steps

The first step: Right-click the Partyinvites project in the Solution Explorer (Solution Explorer, remember these two words, not explained later) (Note that clicking on the project, not the Click Solution), Then choose Add→class (Class) from the pop-up menu. (another option is to choose "New Item" from the pop-up menu as well)

Tip If you do not see the Class menu item or the menu item is disabled, the Visual Studio debugger may be running. When it is running the application, Visual studio restricts you from making changes to the project. Please select Stop Debugging (Stop Debugging) from the Debug menu and try again.

Step two: After the first step, Visual studio displays the Add New Item dialog box, which contains a template for all the items that you can add to an ASP. Make sure that class template is selected, set its name to GuestResponse.cs, and then click the Add button. Visual Studio creates a new C # class file and opens the file for editing. Set the contents of the file so that it matches the contents of listing 1-4.

Code Listing 1-4 Guestreponse class

Code explanation

(1) Class Members: Properties

The above code is the definition of a C # class. Where the class name is Guestresponse, the class has 4 members, and this member has a special name in C #: Properties. It's actually equivalent to the GETXXX and SETXXX member methods in Java. The above member properties are a simplified notation, and the complete wording is:

Figure 2 Full write of the Name property

Tips
Java provides read and write functions for a member variable:class guestresponse{ private String name; Public String GetName () { return name; } Public String setname{string n}{ name = n; }}

(2) Nullable type: BOOL?

The bool type variable can be assigned a value of only two: true and false. bool is also a data type, BOOL? Variables can have a value of three: true, false, and null. A null representation is neither true nor false, and the value is null. The reason why the Willattend property chooses this data type is explained later.

3. Create a Repository

The Guestreponse class is used to show the guest's reply to the party. This requires a repository to store the Guestresponse objects that are created. In real-world applications, this repository is typically a database. When we create a more realistic ASP. NET application, we'll show you how to set up and use the database. In this chapter, we only need a simple and fast storage method, so the example stores the object in memory. The benefits of doing this are convenient, but each time you stop or restart the application, the data will be lost. This may be a strange choice for real-world Web applications, but this is a good choice for this chapter.

To define the repository, add a new class file to the project ResponseRepository.cs, which is implemented to implement the implementation of storing data in memory and reading and writing data. Make sure that the contents of the file match the contents of listing 1-5.

Code Listing 1-5:responserepository.cs

Code Analysis

The 11th sentence: Instantiate a list, the list of Guestresponse objects stored.

18th sentence-21 sentence: Returns a list containing all invitees ' information. Obviously, this is a member function, not a property or a member variable. Ienumerable<t> is an interface type, because List<t> is inherited from Ienumerable<t>, so the Resposes object returned is not a problem. This involves the use of interfaces to achieve polymorphic knowledge, not specifically developed here, please refer to the C # polymorphic Charm (virtual method, abstraction, interface implementation)

Thinking
Please learn the interface knowledge after class, understand the function and basic usage of the interface, and write a simple interface program.

23rd sentence-26: Add an invitation (Guestresponse object) to the repository. This is directly added to the memory-linked list responses object (the actual project is often written to the database).

[ASP. 07] First ASP.-Create a data model and repository

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.