CodeFluent Documentation Send comments on this topic.
A Custom ASP.NET Web Site
CodeFluent Entities > Tutorials > Using The Modeler 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 Entities 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 architecture type 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:
• Select the solution in the Solution Explorer, right-click, and select Add, then New Project...
• In the project types tree view select the Web type, and select the ASP.NET Web Application project template,
• Name it Sample.WebApplication,
• Reference the Sample project and the CodeFluent.Runtime.dll,
• Add a new Web User Control (ASCX) file, and name it CreateCustomerControl.ascx.
At this point your Sample.WebApplication should look like this:

Click to EnlargeClick to Enlarge

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:
• Right-click on the Sample.WebApplication project and select Properties,
• In the properties window, select the Web tab on the left hand side of the window,
• In the tab check the Use Local IIS Web Server,


Click to EnlargeClick to Enlarge


• Then save and when you are asked whether or not to create the virtual directory answer YES.


Click to EnlargeClick to Enlarge


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 proper rights to connect and manipulate data in the database.

Note: CodeFluent Entities does not change any security settings by default.

To do so:
• Open Microsoft SQL Management Studio,
• Connect to the SQL instance used by the BOM,
• Select the Security folder in the instance and expand it,
• Select the Logins folder in the Security folder, and expand it,
• Select the NETWORK AUTHORITY\NETWORK SERVICE, right-click and select Properties. If this login doesn’t exist, add it (right-click on Logins -> New Login)


Click to EnlargeClick to Enlarge


• In the Login Properties window, select the User Mappings page,
• Map the Sample database to the NETWORK AUTHORITY\NETWORK SERVICE account and grant it the db_owner rights and click OK.


Click to EnlargeClick to Enlarge

 

Note: CodeFluent Entities 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 Entities 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 a 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 flexible 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 Entities 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:

CreateUser.ascx

Note: you can check in the Sample database, using SQL Management Studio, that the records were actually added to the Customer table.