Create data access and business logic tiers in ASP.

Source: Internet
Author: User
Tags table definition sql server express

    • . NET Framework 4
When working with data in ASP, you can benefit from using the generic software model. One of these patterns is to separate the data access code from the business logic code that controls data access or provides other business rules. In this mode, these two layers are separated from the presentation layer. The presentation layer consists of pages where the site user has permission to view or change data.

ASP. NET provides a variety of ways to decouple data access, business logic, and representations. For example, the data source model, which includes server controls such as LinqDataSource and ObjectDataSource, separates the presentation layer from the data access code and business logic.

Another pattern is to include the data access logic directly in the ASP. NET page (presentation layer). For example, you can write the ADO code in the code-behind page of an ASP. NET page, or use the SqlDataSource control. This method tightly couples the data access logic with the presentation layer.

The recommended approach is to detach the data access logic from the presentation layer. This separation layer is called the data access layer. The data access layer can be implemented as a separate class library project. However, you can also use tools in Visual Web Developer to generate a data access layer.

Description

In Microsoft Visual Web Developer Express Edition, you cannot create a class library project. However, you can create a separate project by using Visual Basic Express or Visual C # Express Edition, and then include the output of the class as an assembly (DLL) in your Web site.

If your site wants to display or update data, you should create the data access layer and the business logic layer before you create the user interface.

A data-driven WEB application typically adds a data access layer using a typed dataset or entity class that represents data. It also contains a business logic layer that enforces custom business rules. Finally, it uses an ASP. NET page to add a presentation layer and create a common page layout through master pages and themes. This walkthrough shows how to create a data access layer.

The data access layer contains all the code that is specific to the underlying data source. This includes the code that creates the database connection, and the code that issues the Select, Insert, Update, and Delete commands. The data access layer typically contains classes that implement methods to access the underlying database data. Instead of processing data directly, the presentation layer calls classes and methods in the data access layer for all data requests.

This walkthrough covers the following tasks:

    • Create a SQL database and add data.

    • Add a Linq to SQL file that acts as the data access layer .

    • Create a page to use as the presentation layer.

    • Adds a LinqDataSource control to the page that communicates between the presentation layer and the data access layer .

Prerequisites

To complete this walkthrough, you need the following components:

    • Microsoft Visual Studio 2008 or Microsoft Visual Web Developer Express Edition. For download information, see the Visual Studio Developer Center (Visual Studio Dev Center) Web site.

    • . NET Framework version 3.5.

    • SQL Server Express Edition. If you have installed Microsoft SQL Server, you can also use the software directly.

Create a Web site

The first step is to create a Web site.

Create a new file system Web site
    1. Open Visual Studio 2008 or Visual Web Developer Express Edition.

    2. In the File menu, click New Web site.

      Displays the New Web Site dialog box.

    3. Under Visual Studio installed templates, select ASP.

    4. In the Location box, click

      For example, type

    5. In the Language list, click

      description:

      The programming language that you select becomes the default programming language for the Web site. However, you can also set the programming language separately for each page.

      Visual Web Developer will create the folder and a new page named default.aspx.

Connect to a database

The next step is to use the Server Explorer window to connect to the database in Visual Web Developer (in Visual Web Developer Express, the window is named Database Explorer). By Creating a database connection in Server Explorer, you can add tables, stored procedures, views, and other database elements, all in Visual Studio. Alternatively, you can view the table data or create a query that can be done manually or by using the Query Builder window.

Later in this walkthrough, you will generate a typed dataset for the data access layer, where you must create a connection to the database in Visual Web Developer. You can provide connection information manually. However, Visual Web Developer can simplify this process because it automatically populates the list of databases that have been registered in Server Explorer.

In this walkthrough, you will create a new database to track individual task items.

Create a new SQL Server Express database

In this section, you will create a new SQL Server Express database to store task information in the to-do list.

Add a database to a Web site
    1. In Solution Explorer, right-click the name of the Web site, and then select Add New Item.

      The Add New Item window is displayed.

    2. Select SQL database and name the database "Tasks.mdf".

    3. Click OK.

    4. When Visual Web Developer asks if the database should be stored in the App_Data folder, click Yes.

Creating schemas and sample data for the Tasks database

You can use the database design and editing features to create schemas for tables that store task items.

Define schemas and add data for the Tasks database
  1. Open The App_Data folder in Solution Explorer, and then double-click Tasks.mdf.

    The Tasks database node opens in Server Explorer.

  2. Right -click the Tables folder, and then select Add New table.

    The table Definition window is displayed.

  3. In the table, create the following columns:

    Column Name

    Data type

    Property

    TaskId

    Int

    is NOT NULL

    Name

    nvarchar (50)

    is NOT NULL

    dateCreated

    Datetime

    is NOT NULL

    Iscomplete

    Bit

    is NOT NULL

  4. Select the row where the TaskId column is located, right-click the row, and then click Set Primary key.

  5. Select the taskid row, and then locate the Identity specification [Identify specification] section in the Column Properties window.

  6. Open the section, and then set (is identity) to yes.

  7. Save the table, name it "TasksList", and then close the Table Definition window.

  8. In Server Explorer, right-click the table, and then select Show Table data.

    An edit window is displayed where you can view, add, and edit data.

  9. Add four to five records to the table.

    You do not need to specify the value of the TaskId column because it is an identity column, so it is automatically assigned.

  10. Close the editing window.

Create a data access layer and a business logic layer

There are several ways to create a data access layer and a business logic layer for the database you just created. In this walkthrough, you will create a class that represents the database entity. You can then add your own business logic to these generated classes (in this walkthrough, you will not add business logic to the Class).

In this walkthrough, you will work with the data using language-Integrated query (LINQ). LINQ applies object-oriented programming principles to relational data. LINQ provides a unified programming model for querying and updating data in different types of data sources, and extends data functionality directly into the C # and Visual Basic languages. For more information about LINQ, see language-Integrated Query (LINQ).

You will use the LINQ to SQL class as the data access layer. In addition, you use the Object Relational Designer window in Visual Web Developer to generate entity classes that represent data.

Mapping the Tasks database to the SQL data context class

To create a data access layer, you first add a typed dataset to your project.

To create a class for the Tasks table
  1. If your site does not yet have an App_Code folder, in Solution Explorer, right-click the project, click Add ASP. Net Folder, and then click App_Code.

  2. Right click the App_Code folder, and then click Add New Item.

    Displays the Add New Item dialog box.

  3. under Visual Studio installed templates, select the LINQ to SQL class template, and then rename the file to "Tasks.dbml".

  4. Click Add.

    The Object Relational Designer window is displayed.

  5. In Server Explorer, drag the TasksList table to the Object Relational Designer window.

  6. Save the Tasks.dbml file.

    Visual Web Developer will create a Tasks.dbml.layout file under Tasks.dbml in the App_Code folder. In addition, it creates Tasks.designer.cs or Tasks.designer.vb, depending on the programming language you are using.

  7. In Solution Explorer, open the Tasks.designer.cs or Tasks.designer.vb file.

    Note that the code contains classes named TasksDataContext and TasksList. The TasksDataContext class represents a database, andthe TasksList class represents a database table. the parameterless constructor of the TasksDataContext class reads the database connection string from the Web site's configuration file (Web. config).

  8. Open the Web. config file.

    Note that the connection string for the Tasks database has been added to the connectionStrings element.

  9. Close the class file and the Web. config file.

Creating and configuring LinqDataSource Controls

Now that you have a database table and several classes that represent database entities, you can use LinqDataSource on the ASP. NET page to access the database. the LinqDataSource control provides LINQ functionality to WEB developers through the ASP. NET data source control structure.

The LinqDataSource control is used to create code that selects, inserts, updates, and deletes objects in the database. Business logic can call these classes to execute database functions and apply business logic rules.

Creating and configuring LinqDataSource Controls
    1. To open or switch to the Default.aspx page.

    2. Switches to Design view.

    3. Drag the LinqDataSource control onto the Web page from the Toolbox.

      You can leave the ID attribute as linqdatasource1.

    4. In LinqDataSource task [LinqDataSource Tasks] Smart tag panel, click

    5. In the list of context objects, select "Next".

    6. In the list, select TasksList (table <TasksList>) [Taskslists (table< taskslist>)], and then click "Finish".

    7. In LinqDataSource Tasks smart tag panel, select Enable Delete, "Enable Insert" and "Enable updates" check box.

      Note that you do not have to specify any database commands for the selection data.

    8. To run the page.

      This page displays the data that you entered earlier in this walkthrough.

Next Steps

This walkthrough illustrates how to use the LINQ to SQL class template and the LinqDataSource server control to create the data access layer and the business logic layer for your application. You create a way to access data for a site page that is flexible and not directly bound to the site presentation layer.

Topic Walkthrough: Creating an AJAX-enabled data application using the LinqDataSource control to create an AJAX-enabled WEB application for displaying and updating information in the Tasks database.

Please see TaskWalkthrough: Creating an AJAX-enabled data applicationConceptASP. NET Walkthrough featured

Create a data access and business Logic Layer (RPM) in ASP.

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.