Monday, July 28, 2008

Creating a Business Logic Layer

The Data Access Layer (DAL) created in the first tutorial cleanly separates the data access logic from the presentation logic. However, while the DAL cleanly separates the data access details from the presentation layer, it does not enforce any business rules that may apply


In this tutorial we'll see how to centralize these business rules into a Business Logic Layer (BLL) that serves as an intermediary for data exchange between the presentation layer and the DAL. In a real-world application, the BLL should be implemented as a separate Class Library project; however, for these tutorials we'll implement the BLL as a series of classes in our App_Code folder in order to simplify the project structure. Figure 1 illustrates the architectural relationships among the presentation layer, BLL, and DAL.

Partial code for Business Logic Layer



using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

///
/// Summary description for blayer
///

public class blayer
{
public blayer()
{
//
// TODO: Add constructor logic here
//
}

///
/// insert records into database
///

public int insert(string title, string description)
{

person p1 = new person();
try
{
return p1.insert(title, description);
}
catch (Exception ex)
{
throw;
}
finally
{
p1 = null;
}
}

///
/// update records into database
///

public int update(int id, string title, string description)
{
person p1 = new person();

try
{
return p1.update(id, title, description);
}

catch
{
throw;
}

finally
{
p1 = null;
}
}
///
/// Shows records from database
///

public DataTable show()
{
person p1 = new person();

try
{
return p1.show();
}
catch
{
throw;
}
finally
{
p1 = null;

}

}
public DataTable show1(int id)
{
person p1 = new person();

try
{
return p1.show1(id);
}
catch
{
throw;
}
finally
{
p1 = null;
}
}
///
/// Delete record from database
///

public int delete(int id)
{
person p1 = new person();

try
{
return p1.delete(id);
}

catch
{
throw;

}
finally
{
p1 = null;

}
}
}

Creating a Data Access Layer

We'll start with creating a software architecture composed of a Data Access Layer (DAL) using Typed DataSets, a Business Logic Layer (BLL) that enforces custom business rules, and a presentation layer composed of ASP.NET pages that share a common page layout. Once this backend groundwork has been laid, we'll move into reporting, showing how to display, summarize, collect, and validate data from a web application

In this tutorial we'll start from the very beginning and create the Data Access Layer (DAL), followed by creating the Business Logic Layer (BLL) in the second tutorial, and working on page layout and navigation in the third. The tutorials after the third one will build upon the foundation laid in the first three

This layer is also a class which we use to get or set the data to the database back and forth. This layer only interacts with the database. We write the database queries or use stored procedures to access the data from the database or to perform any operation to the database.
Summary

  • Application layer is the form where we design using the controls like textbox, labels, command buttons etc.
  • Business layer is the class where we write the functions which get the data from the application layer and passes through the data access layer.
  • Data layer is also the class which gets the data from the business layer and sends it to the database or gets the data from the database and sends it to the business layer.
  • Property layer is the sub layer of the business layer in which we make the properties to sent or get the values from the application layer. These properties help to sustain the value in a object so that we can get these values till the object destroy
Partial Code for Data Access Layer



using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public class person
{
SqlConnection myconn = new SqlConnection("server=isa;database=CuteChat4;uid=sa;password=sa");

public person()
{
//
// TODO: Add constructor logic here
//
}
///


/// Used to insert records into database
///

public int insert(string title,string description)
{
myconn.Open();
SqlCommand cmd = new SqlCommand("insertnews", myconn);
cmd.CommandType = CommandType.StoredProcedure;

try
{
cmd.Parameters.AddWithValue("@title", title);
cmd.Parameters.AddWithValue("@description", description);
return cmd.ExecuteNonQuery();
}
catch
{
throw;
}
finally
{
cmd.Dispose();
myconn.Close();
myconn.Dispose();

}

}
///
/// Update record into database
///

public int update(int id, string title, string description)
{

myconn.Open();
SqlCommand cmd = new SqlCommand("updatenews", myconn);
cmd.CommandType = CommandType.StoredProcedure;
try
{
cmd.Parameters.AddWithValue("@id", id);
cmd.Parameters.AddWithValue("@title", title);
cmd.Parameters.AddWithValue("@description", description);
return cmd.ExecuteNonQuery();
}
catch
{
throw;
}
finally
{
cmd.Dispose();
myconn.Close();
myconn.Dispose();
}

}
///
/// Shows all records from database
///

public DataTable show()
{

SqlDataAdapter da = new SqlDataAdapter("selectnews", myconn);
da.SelectCommand.CommandType = CommandType.StoredProcedure;
DataSet ds = new DataSet();

try
{
da.Fill(ds, "news");
return ds.Tables["news"];
}
catch
{
throw;
}

finally
{
ds.Dispose();
da.Dispose();
myconn.Close();
myconn.Dispose();
}
}

public DataTable show1(int id)
{

SqlDataAdapter da = new SqlDataAdapter("select* from news where id=" + id, myconn);
da.SelectCommand.CommandType = CommandType.Text;
DataSet ds = new DataSet();

try
{
da.Fill(ds, "news");
return ds.Tables["news"];
}
catch
{
throw;
}

finally
{
ds.Dispose();
da.Dispose();
myconn.Close();
myconn.Dispose();
}

}

///
/// Delete record from database
///

public int delete(int id)
{
myconn.Open();
SqlCommand cmd = new SqlCommand("deletenews", myconn);
cmd.CommandType = CommandType.StoredProcedure;

try
{
cmd.Parameters.AddWithValue("@id", id);
return cmd.ExecuteNonQuery();
}

catch
{
throw;
}

finally
{
cmd.Dispose();
myconn.Close();
myconn.Dispose();

}

}

}

Sunday, July 27, 2008

3-tier Architecture with ASP.NET 2.0

Introduction

Tutorial 1: Creating a Data Access Layer
When building a Web application, creating the DAL should be one of your first steps, occurring before you start creating your presentation layer.
Visual C#


Tutorial 2: Creating a Business Logic Layer
A well-architected application is crafted into distinct layers, each of which encapsulates a particular role; in this tutorial we build a Business Logic Layer as a series of classes in our application's App_Code folder that call down into our DAL.
Visual C#


Tutorial 3: Master Pages and Site Navigation
With the site map defined and the master page complete, we now have a consistent page layout and navigation scheme for our data-related tutorials. Now updating the site-wide page layout or site navigation information is a quick and simple process.
Visual C#

Friday, July 25, 2008

Solidify Your C# Application Architecture with Design Patterns



SUMMARY A design pattern can solve many problems by providing a framework for building an application. Design patterns, which make the design process cleaner and more efficient, are especially well-suited for use in C# development because it is an object-oriented language. Existing design patterns make good templates for your objects, allowing you to build software faster. This article describes several popular design patterns you can use in your own applications, including the singleton, the decorator, the composite, and the state classes, which can improve the extensibility of your applications and the reuse of your objects


C# and Design Patterns C# is a modern programming language that promotes object-oriented software development by offering syntactic constructs and semantic support for concepts that map directly to notions in object-oriented design. This is in contrast to C++, which supports procedural as well as object-oriented (and generic) programming. Nonetheless, if you are a C++ programmer, getting up to speed with C# should be a snap—the learning curve for C++ programmers is flat. Even if you haven't seen any C# code before, you should have no problem comprehending the example code in this article. In fact, I wouldn't be surprised if you find the C# implementation of the design patterns cleaner, especially if you have used or coded the patterns before. Books and articles that discuss design patterns typically explain the problem and the context in great detail, followed by a formal description of the solution. I will take a less rigorous approach in this article, focusing on the essence of the pattern instead, and illustrating an appropriate example with some sample code in C#
Let's start with the simplest design pattern: Singleton.

Singleton Anyone who has ever written an MFC application—no matter how small—knows what a singleton is. A singleton is the sole instance of some class. To use an MFC analogy, the global instance of the CWinApp-derived application class is the singleton. Of course, while it's imperative that no additional instances of the application class be created, there really is nothing preventing you from creating additional instances. In situations like these, when you need to enforce singleton behavior for a specific class, a better alternative is to make the class itself responsible for ensuring that one and only one instance of the class can be created. Back in the MFC analogy, you see that the responsibility for keeping track of the solitary instance of the application class rests with the developers of the application. They must not inadvertently instantiate another application object.

Thursday, July 24, 2008

On the Way to Mastering ASP.NET

Be Right at Home in the World's Most Powerful Web Development Environment



In recent years, creating dynamic, server-side web applications has become the most vital part of web development. Now, thanks to ASP.NET and Visual C#, you can build cleaner, more powerful web applications, and you can do it more quickly than ever before. Mastering ASP.NET with C# is an essential guide to harnessing the power of the .NET Framework to develop and consume Web Services of all kinds. This book is packed with the skills you need to get started creating ASP.NET applications, including using Web Forms, connecting to databases with ADO.NET, and working with XML. Coverage Includes: Using the ASP.NET intrinsic objects; Employing the ASP.NET Server controls; Using HTML controls; Saving state data with cookies; Uploading files; Sending email; Retrieving and displaying data from databases; Building User and Composite controls; Building custom controls; Managing multiple ASP.NET configuration files; Creating Web Services, Web Forms