Writing Data Access Layer (DAL) and Business Access Layer (BAL) with CsharpGears Framework

Today I am going to show you how to write Data Access Layer (DAL) and Business Access Layer (BAL) using the CsharpGears Framework.
One of the most accepted approaches of the today's application architecture is the 3-tier architecture, or it's general version n-tier architecture. The idea is to separate the logic of the application in separate layers, each of which will be able to communicate in a standardized way with another layer. Most often, the tier that is responsible for displaying the data is called the presentation layer, which is capable of invoking the layer that holds all the business logic - namely the business layer. The business layer, however can either be invoked by the presentation layer and return the results of the execution to it. In certain situations, the business layer will need to access some amount of data in order to perform all the calculations. In that case, the business layer invokes the third layer - data layer. The data layer is responsible for retrieving the sets of data from some source - be it a database or xml file or web service - it can be literally anything.


CsharpGears Framework : Object-Relational Mapping

Reasons for Object-Relational Mapping
Since the rapid development of the relational databases and the acceptance of the Object-Oriented Programming model, a new need rose: to quickly and correctly map the persistance medium model (namely, the SQL tables) to the business objects in some high level programming language (C# for instance). Developers noticed that they invest significant amount of time writing the Data Access Layer (DAL) when starting off a new project, so they started looking for ways to decrease that time and effort. As time went by, several companies started producing software to efficiently map the SQL tables into business classes. Today, numerous of them are fairly famous, such as the NHibernate, SubSonic, Microsoft's Entity Framework, LINQ, etc. You can find more detailed list of ORM software on wikipedia.
Benefits of the ORM


CsharpGears Framework : Using Different SQL Providers

Hello, I am about to present you how easily can you connect different types of databases with the C# Gears Framework. First of all, note that the IDatabase interface implementators do not care to what type of database they are connected to, they only care about the SQL query they need to execute. For example, here is how would you use C# Gears Framework in order to connect MySQL Database with .NET:
Connecting MySQL with C#
Step 1:Download and install the MySQL provider for .NET. Simply go to MySQL's download site and create an account if you already don't have one. The installation is pretty straightforward - there is a MSI installer.
Step 2:Add the MySQL.data.dll to your project. Ordinarily create new project in Visual Studio, go to References, right click on it and choose Add Reference. Then navigate to the path where the installation of the MySQL provider resides, and choose the mentioned dll.
Step 3: Start using the MySQL data provider. Now I will paste the whole code from my Default.aspx.cs file:

CsharpGears Framework : Database Access: Output Parameters

Sometimes it is required that the stored procedures have output parameters. The CsharpGears can handle that too. There is another class in the Database namespace, that specializes working with output parameters. It is called DataBaseOutputEnabledEntity . Here is an example on how it's used:


DataBaseEntity dbEntity = new DataBaseEntity("GetProductByID");
DataBaseOutputEnabledEntity outputEnabledDbEntity = new DataBaseOutputEnabledEntity(dbEntity);
            outputEnabledDbEntity.AddCommandParameter("@NumberOfProducts", null, DbType.Int32);
            DataTable results = (DataTable)outputEnabledDbEntity.Select();
            int outputParamValue = (int)outputEnabledDbEntity.GetOuputParameterValue("@NumberOfProducts");

CsharpGears Framework : Database Access- SQL Transactions

Once you understood the basic ways of utilizing the CsharpGears Framework Database Access, now I will show you how to treat the SQL queries as transactions. Please note that in order to use the SQL transactions entities, you first need to add a reference to the System.Transactions namespace. After that, using the transaction database entity is very easy and similar to the previous examples:

TransactionDbEntity<LogMessage> tDbEntity = new TransactionDbEntity<LogMessage> ("LogRead",ConnectionString);
List<LogMessage> list = (List<LogMessage>)(tDbEntity.Select());

Here I consider retrieving the log from a database as list of LogMessage instances. The LogMessage class basically contains ID, Sender, Description and Date properties that need to be bound.This Database entity essentially creates CommitableTransaction object inside and commits it if the query passes successfully, otherwise performs rollback. But once again, my point was to show you how easy is to treat queries as transactions. Nevertheless, you can still write the transaction handling in the storet procedure's body, but just in case you have already written hundreds of procedures without database transactions, this class might come in handy.

CsharpGears Framework : Database Access

You have worked with databases, right ? Has it happened to you, to figure it out that it would be great if you could somehow magically map your SQL tables in neat C# classes ? Well, it's not that I am discovering new continents here, but I personally think that the Database Access sublibrary will effectively and easily allow you to map your database tables into C# classes. Among the other benefits, these classes can also come in handy if you wanted to treat some SQL queries as transactions and roll them back in case of error. You can also invoke stored procedures or simply pass ad-hoc queries to the database with just a few lines of code.

But what truely makes this library awesome, is it's ability to directly return lists of business objects. You don't even have to explicitely bind the class' properties to the SQL tables (as with Attributes). The Framework only requires that the name of the public property is the same as the name of the SQL table column. Have I mentioned that the library is absolutely generic ? It can accept any types of objects, as long as they have default constructor (which could be observed as limitation, but it is so simple to write it, that is no big deal). Well, enough introductory notes, let me show you what library can really do.

DatabaseEntity
This is the basic class in this library. It provides the basic functionality such as communicating with the database by executing queries. There are several constructors available which will allow you to choose your connection string to the database, the SQL provider, the type of the query (Stored procedure or ad-hoc query) etc. The DatabaseEntity class provides the four main query types in SQL: Select, Insert, Update, Delete. Please note that the Select query returns object which is actually DataTable (boxed). You need to cast it to DataTable for the sake of reusability of the code. So here is how it works.
Say you have the following table named Product  with the following columns in the database:
ID | Description | Price

Then suppose that I wanted to retrieve all Products from the SQL table Product. I write the following procedure:

The CsharpGears Framework

I introduce you my development framework: CsharpGears. It is written in C# and its purpose is to speed up the development process, especially in the fundamental levels. It is meant to make developer's life easier when it comes to writing code such as accessing databases and mapping SQL tables into business objects ( C# classes ), or also for easy work with images, or other types of services, such as loggers or quickly writing code for RSS publishes, or maybe that tedious code for sending emails...
I personally find it very time-saving, and I hope that someone else out there will find it useful. To summarize, the CsharpGears Framework currently consists of six sublibraries:
  1. Database access
  2. Logger activities
  3. E-mail sending
  4. Working with images
  5. RSS builder
  6. URL rewriting policies
Each component will be covered in details in the following posts. Examples of its usage will also be included. Remember that you are able to download the class libraries for free.

 Installation of the CsharpGears Framework
All you need to do in order to utilize the benefits of my framework is to : (1)download it from the downloads section, (2) add it as reference to any .NET project you would like to use it in.

 End Note
I would also emphasize once again that I would be very happy first if someone else decides to use my framework, but I would be even happier if you provide me with your feedback.

Free C# libraries and examples

Hello everybody!

This blog is dedicated to publishing for download some of my class libraries I have written in C#. I hope you will find my classes useful and I sincerely encourage you to send me feedback about your experience. Any suggestions, critics and ideas are welcome. All the code examples and libraries will be absolutely free. OK, so it is time to see sharp...