CodeFluent Documentation Send comments on this topic.
A Custom ASP.NET Web Site
See Also
CodeFluent Entities > Tutorials > Using The Core Edition > The Persistence and the Business Layers > A Custom ASP.NET Web Site

Glossary Item Box

Creating the foundations of your application

Before taking care of the user interface, we must set up a data tier. The data tier is the place where the application will persist all of its data and is therefore commonly referred to as the persistence layer. On top of that layer, CodeFluent allows us to generate a middle tier which will contain a set of objects enabling developers to:

  1. manipulate the data objects stored in the persistence layer,

  2. define the business logic of our application.

Therefore, this middle tier is known as the Business Object Model (BOM). In the end, whatever the user interface or the type of architecture is, the persistence layer (containing the data) and the business layer (containing the business logic) are always the fundamental pillars of an application. Since those two layers are common to all types of applications, their creation won't be detailed in this article; instead this article extends the The Persistence and the Business Layers one by using them in an ASP.NET web site.

Preparing the ASP.NET web site development environment

 First of all we're going to add a new Web Application project to our solution. To do so:

At this point your Sample.WebApplication should look like this:

Project

Before coding our web site, we need to configure it properly. First, we're going to add a configuration section to the Web.config file of our project that indicates our Business Object Model (BOM) which database to connect to.

Open the Web.config file in Visual Studio and add the following below the configuration XML node:

Web.config Copy Code
<configSections>
   
<section name="Sample" type="CodeFluent.Runtime.CodeFluentConfigurationSectionHandler, CodeFluent.Runtime" />
 
</configSections>
 
<Sample connectionString="server=(local);database=Sample;Integrated Security=true"/>

Now that our Business Object Model (BOM) knows where to connect, we're going to specify where it's being hosted. By default, Visual Studio web sites applications are hosted in a development ASP.NET server. To get closer to a production environment we'll set up the project to be hosted in Internet Information Services (IIS). To do so:

By default, web applications hosted in IIS are ran under the NETWORK AUTHORITY\NETWORK SERVICE account. Since the web application uses our object model to connect to the Sample database, we need to give the NETWORK AUTHORITY\NETWORK SERVICE user account the correct rights to connect and manipulate data in the database.

Note: For security reasons, CodeFluent purposely does not change any security settings.

To do so:

Note: CodeFluent doesn't necessarily need the db_owner rights on a database. For simplicity's sake, this tutorial grants them in order to avoid any unsufficient permission errors later on.

Now that our environment is properly configured (our application is hosted in IIS, has a connection string pointing to its database, and is executed under an account with the rights to read and update data), we can develop our User Interface (UI) that uses our CodeFluent generated components.

Developing the UI

The UI layer should only contain presentation code, all of the business logic should be in the Business Object Model (BOM). Hence, BOM objects are made to be data bound to, and provide advanced features to ease presentation layer developments such as validation capabilities, paging, sorting, caching, and so on.

In this tutorial, we'll focus on a basic example that illustrates the data binding principle. We're going to create a user control (CreateCustomerControl.ascx that we created earlier) which allows end-users to create customers in the database. You'll see that thanks to the BOM, no code behind is needed, the presentation layer will contain exclusively presentation code: this is a capital concern in order to create highly evolutive and maintainable applications. Thanks to this architecture, adding user interfaces is made easier, since only the way of presenting the same business data and logic is changed.

In the Default.aspx page, we'll display our CreateCustomerControl. To do so, copy and paste the following code:

Default.aspx Copy Code
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Sample.WebApplication._Default" %>
<
%@ Register TagPrefix="uc" TagName="CreateCustomerControl" Src="./CreateCustomerControl.ascx"%>
<
!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<
head runat="server">
   
<title>Sample</title>
</
head>
<
body>
   
<form id="form1" runat="server">
   
<div>
       
<uc:CreateCustomerControl ID="CreateCustomerControl1" runat="server"/>
   
</div>
   
</form>
</
body>
</
html>

In the CreateCustomerControl, we'll implement the customer creation user interface. Since CodeFluent generates business objects compliant with the standard .NET object ObjectDataSource, the CreateCustomerControl code we'll bound to create an ObjectDataSource that will provide the data to which other standard .NET controls can bind to.

Here's the source code:

CreateCustomerControl Copy Code
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="CreateCustomerControl.ascx.cs" Inherits="Sample.WebApplication.CreateCustomerControl" %>
<
asp:ObjectDataSource ID="CustomerDataSource" runat="server"
                     
TypeName="Sample.Customer"
                     
DataObjectTypeName="Sample.Customer"
                     
SelectMethod="Load" InsertMethod="Insert" UpdateMethod="Save">
</
asp:ObjectDataSource>
<
asp:FormView ID="FormView" runat="server"
             
DefaultMode="Insert" DataSourceID="CustomerDataSource">
 
<InsertItemTemplate >
       
<asp:Label ID="NameLabel" runat="server" Text="Name: " /><br />
       
<asp:TextBox ID="NameTextBox" runat="server" Text='<%#Bind("Name")%>'/><br />
       
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
           
ErrorMessage="This name is required."
           
ControlToValidate="NameTextBox" ValidationGroup="InsertGroup"/><br />
      
       
<br /><asp:Label ID="AddressLabel" runat="server" Text="Address: " /><br />
       
<asp:TextBox ID="AddressTextBox" runat="server" Text='<%#Bind("Address")%>'/><br />
       
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
           
ErrorMessage="The address is required."
           
ControlToValidate="AddressTextBox" ValidationGroup="InsertGroup"/><br />
              
       
<asp:Button ID="SaveButton" runat="server"
                   
CommandName="Insert" CausesValidation="true"
                   
Text="Create" ValidationGroup="InsertGroup" />
 
</InsertItemTemplate>
</
asp:FormView>

Finally, here's the web site that allows end-users to create customers in the database:

UI

 

 

See Also