![]() |
||
SQL Server programming : Best practiceIn the previous article we discussed the Database Tier of the application that lays a foundation for a strong Enterprise Application. In this tutorial we will take a look at the Business Logic Tier which is also known as the Service Layer. The Business Logic Tier is where all the logic is encapsulated. This SQL Server programming tutorial will look into some of the good and bad practices for making the Business Logic Tier. We will also take a look at the Model View Controller Pattern (MVC).
The main purpose of the business layer is to encapsulate all the business logic. The business layer must be separated from the Database layer and the presentation layer. A strong business layer hides all the confidential information from the user and the user has no power to communicate with the business layer directly. The Business layer should be made in such a way that it is portable; meaning that the methods and the functionality of that layer can be used in other applications. The Business layer should also be extendible; meaning that in the future we should be able to add more methods without changing the whole architecture of the layer. SQL Server programming : Bad Practice Let's look at an example of weak business layers which allows the user to directly communicate with the service layer and jeopardize security. Let's make a simple web form which will go to the database and fetch some results depending upon the selection of the user. We will be using our database "MyDatabase" which we created in the last tutorial. Below is the image of the web form:
private void Button1_Click(object sender, System.EventArgs e)
{string query = "SELECT PhoneNumber FROM Phone myCommand.CommandType = CommandType.Text;
myConnection.Open(); You might not be able to understand all the code that is written. The code above is written in C# and the interface is drawn in Asp.net. Now, in this SQL Server programming tutorial, let's study the bad things about the code above. First of all this code is implemented inside the button click event, which means that when the button is clicked this code is fired. That means, it opens the database connection, searches for the name, and returns the phone numbers associated with the name. As you can see the user has the direct interaction with the business logic and the database tier, which is like putting our whole confidential information into the hands of the user. The other bad thing we are doing, is we are using Ad-Hoc queries. In the last tutorial I emphasized on using the stored procedures instead of the Ad-Hoc queries because stored procedures are fast and reliable. Now let’s see that how we can make this code better. SQL Server programming : Good Practice The interface of our application will remain the same. The business logic will be changed, and will be handled in a more secure way. We will also use a stored procedure for faster database access. First of all we will make a class that will access the data from the database. We will name the class "DBPerson". The use of the class will hide the business logic from the user. Below is the class; it contains a method "GetPersonPhoneNumber" which will retrieve the phone numbers from the database.
public SqlDataReader GetPersonPhoneNumber(int personID) SqlDataReader dr = null; SqlConnection myConnection = new SqlConnection(connectionString);
SqlCommand myCommand = new
try dr = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
return dr; As you can see, we have separated the business logic from the presentation layer and now users cannot access the phone numbers unless they call the GetPersonPhoneNumber method. Now, let's see how we are going to call the "GetPersonPhoneNumber" from the button click event. private void Button1_Click(object sender, System.EventArgs e)
{
if(ddlNames.SelectedItem.Value != null && } Observe how good it looks now. Now, if you want to change the way the data is retrieved from the database, you just need to change the GetPersonPhoneNumber method and that's it. This technique of separating the presentation layer, business logic layer and the database layer is also known as the MVC pattern. In Model View Controller pattern the model is the business logic which is hidden from the user. The view is the presentation layer and the controller is the events that are generated from the user interface. It’s always a good idea to put all the class files in a separate folder to organize them properly. SQL Server programming : Using Static Methods Sometimes you'll find you are repeating code over and over again which is serving a common or general purpose. If so is the case, you can use the static methods in a class. Let's see how we can perform this: We make a messages class which will display messages if the data has been inserted or not. Since the messages will be same, we will use static methods. Our new class will be called Messages.
public class Messages As you can see, the method Inserted takes a Boolean value and returns the message if the data is inserted or not. Using this method is pretty straight forward.
string messaage = Messages.Inserted(true); Using business logic layer is very important, as it hides the data that is not meant to be seen by ordinary users. Apart from hiding the data it also serves as a layer between the presentation layer and the database layer. I hope you enjoyed this SQL Server programming tutorial.
Return from SQL Server programming to Application Architecture
|
![]() Database Design FORUM
What visitors say...
"I just stumbled across your site looking for some normalization theory and I have to say it is fantastic.
Read more
Testimonials
I have been in the database field for 10+ years and I have never before come across such a useful site. Thank you for taking the time to put this site together." Mike, USA Free eBookSubscribe to my newsletter and get my ebook on Entity Relationship Modeling Principles as a free gift:![]() WorkshopOn rare occasions, I may perform a Database Design Workshop . Unfortunately, I am currently unable to, but maybe later...Influence meInfluence the content on this site: I want to know what database information you need the most: Participate in my Database Design Content investigation. I would appreciate it if you took the time... |
|
|
Theory & Practice
Worst DB Designs Database eBooks DB Normalization Analysis Phase Database Keys Software Tools DB Glossary Appl.Architecture Oracle DBA MySQL DBA SQL Server DBA Install Oracle Install SQL Server Proj.Management Oracle Constraint Programming Tips Bookstore Internet biz. Database Normalization eBook:![]() |
||
|
Copyright © 2004-2008 www.databasedesign-resource.com / Alf A. Pedersen
All rights reserved. All information contained on this website is for informational purposes only. *Disclaimer: www.databasedesign-resource.com does not warrant any company, product, service or any content contained herein. Return to top
The name Oracle is a trademark of Oracle Corporation.
|
||