DALC4NET (An All in One .NET Data Access Layer)


1.    Introduction

DALC4NET is an Open Source data access layer built for Microsoft .NET projects.  This enables us accessing data from SQL Server, Oracle, MySql, MS Access, MS Excel etc data bases.

DALC4NET is developed using C#.NET.  Microsoft .NET Framework 2.0 is required to use DALC4NET.

Users are free modify the source code as per their need.

For any feedback/ suggestion you can mail the author at ak.tripathi@yahoo.com with subject line ‘DALC4NET’

Note: In order to connect with MySql database you need to have MySql connector for .NET. Which may be downloaded from the below url

http://dev.mysql.com/downloads/connector/net/

2.    Various Providers

Database Provider to be used
MS SQL Server System.Data.SqlClient
Oracle System.Data.OracleClient
MySQL MySql.Data.MySqlClient
MS Access/ MS Excel System.Data.OleDb
MS Access/ MS Excel System.Data.Odbc

3.    How to use DALC4NET?

  1. Download DALC4NET.dll from http://www.codeproject.com/dalc4net/
  2. Add reference of the DALC4NET.dll to your project
  3. Import the namespace DALC4NET (e.g. using DALC4NET;)
  4. Create instance of DBHelper class of DALC4NET library. This class facilitate us for execution of any kind of SQL Command or stored procedure.

DBHelper.cs is a singleton class and hence we will not see any constructor for DBHelper class (singleton class has private contructor). GetInstance() method can be used for creating the instance of the class. GetInstance() method has three overloads.

i. No Parameter

This instance does not require any parameter. This overload creates connection for the connection string name mentions as the default connection.

Note: For using this overload ad an appSettings key “defaultConnection” and set you appropriate connection’s name as the value for this key.  This is the most recommended overload as we need not to do any kind of code changes if we want switch over the database. E.g. Application is supposed to have three databases MS SQL Server, Oracle and MySql. Create three Connection strings into app/web.config file’s connectionString’s section say sqlCon, oracleCon, mySqlCon. If you want application to use SQL Server set value=”sqlCon” for the appSetting’s key=” defaultConnection”. In future if your client want to use oracle database then after porting the oracle database you simply need to change the defaultConnection value i.e. value = “oracleCon”

ii. Connection Name as a parameter

This overload creates instance for the connection name specified into app/web.config file.

iii. Connection String and Provider Name as parameters

This overload creates instance for the specified connection string and provider name.

4.    How Execute SQL Command/ Stored Procedures

In section 2 we created instance of the DBHelper class say _dbHelper. We can execute any Sql Command as follows

4.1   Execute SQL Command

string sqlCommand = “SELECT Count(1) FROM USERDETAILS”;

object objCont = _dbHelper.ExecuteScalar(sqlCommand);

4.2   Execute Stored Procedure with parameters

object objCont = _dbHelper.ExecuteScalar(“PROC_DALC4NET_EXECUTE_SCALAR_SINGLE_PARAM”, new DBParameter(“@FIRSTNAME”, “ashish”), CommandType.StoredProcedure);

In the similar way we may use the appropriate method and overload to execute Sql Command or stored procedure.

5.    DALC4NET Design Overview

DALC4NET is implements following design patterns Singleton, Provider and Factory design pattern.

DALC4NET has only three public classes i.e. DBHelper, DBParameter and DBParameterCollection

5.1 Singleton Design Pattern Implementation

4.1.1 DBHelper class

DBHelper is a singleton class and it has three private constructors. Appropriate constructor is called on invoking the static method GetInstance. This method first of all checks if there is any live instance of the class then that instance is returned. If instance is null (i.e. no live instance) then a new instance is created using appropriate constructor.

private static DBHelper _dbHelper = null;

public static DBHelper GetInstance()

{

if (_dbHelper == null)

_dbHelper = new DBHelper();

return _dbHelper;

}

4.1.2 AssemblyProvider class

AssemblyProvider class also is implemented as singleton as this is the class responsible for loading the appropriate assembly for the specified provider. If this class is not implemented as singleton then every time when this class is instantiated assembly is loaded, this may be costly operation for memory.

Singleton implementation is similar as above.

5.2 Provider Pattern Implementation

All the assemblies for each of the providers are maintained into Hastable data structure. There is a hashtable which contains information about assembly details of each provider.

_dbProviders Hashtable

Key Value
System.Data.SqlClient System.Data, version=2.0.0.0, culture=Neutral, PublicKeyToken=b77a5c561934e089
System.Data.OracleClient System.Data.OracleClient, version=2.0.0.0, culture=Neutral, PublicKeyToken=b77a5c561934e089
MySql.Data.MySqlClient MySql.Data, version=6.0.3.0, culture=Neutral, PublicKeyToken=c5687fc88969c44d

When GetInstance method of this class is called then above hashtable is used to know and load the appropriate assembly for the requested provider using concept of reflection.

LoadAssembly method is responsible for loading the appropriate method

private void LoadAssembly(string providerName)

{

string assemblyName =  _dbProviders[providerName].ToString();

_assemblyName = new AssemblyName(assemblyName);

_assembly = Assembly.Load(_assemblyName);

}

First of all this method gets the name of the assembly from the hashtable, then it instantiates the _assemblyName and then it loads the assembly.

6.    DALC4NET Help

Use DALC4NET tester to see how Sql Command/ Stored Procedures are executed. Here you may find the example for execution of various kind of sql command/ stored procedure execution and uses of their result.

In order to use the DALC4NET Test application

1. Download the appropriate database backup (SQL Server/ My Sql)

2. Restore the backup with name DALC4NET_DB

Now you can play around with the sample code.

AccountPlus (A Complete Expense Management System)

Download

1.    AccountPlus Overview

AccountPlus

1.1   Introduction

Technical Details

AccountPlus is developed using Microsoft.NET technology. It uses C# as a code language. AccountPlus is specially designed and shared for the beginners and intermediate level programmers to understand, design and implement the entire SDLC (Software Development Life Cycle). Also it may be helpful to understand

  • Implementation of Data Access Layer which supports multiple databases
  • Global Logging/ Tracing using log4Net
  • Better use of .NET winform controls
  • Globalization and localization
  • Componentization of any application.
  • Use of XML
  • Design Pattern (Used in Data Access Layer)

Functional Overview

AccountPlus is an application which can be used for shared account/expense management with proper reporting and general analytics.

In order to justify the significance of AccountPlus, we should compare the traditional account management system (paper based account management) with the features of AccountPlus.

Paper Based Account Management: Many times, it happens that multiple people share their daily expenditures. To maintain all these accounts, usually we use paper to keep track of all the individual expenses.

Drawbacks of Paper Based Account Management System

  • At the end of the month, it involves a tedious calculation to find out who had expensed what, and how much amount someone is either supposed to pay or got from others.
  • Another major drawback of paper based account management system is that we won’t be able to analyze on which item our expense is increasing so that we can control the same.
  • Paper based Account Management system has some security flaws like someone can very easily alter the expenses entered by others.

Like this, there are many disadvantages of paper based account management. But with the help of AccountPlus, at every instant we are capable of analyzing the expenses and doing the proper kind of analysis to have a better control on expenditure with full security.

1.2   User Roles and their Rights

AccountPlus has two user roles,

  1. Admin : Admin is the super user for the application who is mainly responsible for
    1. Creating/ modifying the user details to access the system.
    2. Finalizing the expenses.
  2. General User: General user is the user who accesses the system very frequently. General users are responsible for,
    1. Add/ Update/ Delete the expenses. (User can’t edit or delete expense of other users)
    2. Generating and analyzing the expenses.

2.    AccountPlus Design Overview

AccountPlus application is mainly divided into three layers

  1. 1. User Interface Layer

User interface layer of account plus contains all the forms and base class to provide style to the user interface. User interface of the application interacts with Business Layer and very rarely with Data Access Layer to fulfill the request or business action made by the user. Also this layer interacts with Messaging component of the AccountPlus very frequently for displaying the messages to the user interface.

  1. 2. Business Layer

Business Layer is the core part of the application. This component contains all the business logics being used by the application. This component interacts very frequently with the Data Access Layer for writing and reading the data to or from the AccountPlus database.

  1. 3. Data Access Layer

Data Access Layer is one among the most important components of the application. This is an independent component. This layer enables the application to interact with the AccountPlus data base in an efficient manner.  Data Access Layer is independent of database i.e. it may support all kind of databases e.g. MS Access, Excel, SQL Server, MySql, Oracle and etc. This component may be as it is used for any other application also.

Also it has three more small components for Messaging, Configuration and Formatting. Following are the details of these components

Messaging

This component is used to make application scalable in terms of achieving Globalization and Localization. Current version mainly uses this component as a message repository. This helps the application in managing the various application from a single place. This holds application into a resource file.

Configuration

This component is used to for reading the application level configuration i.e. the configuration details stored into App.config file.

Formatting

This component is responsible for formatting various data types being used by the application.

3.    AccountPlus Block Diagram

Following is the block representation of various components of AccountPlus

AccountPlusBlock

  • Logger class of the AccountPlus Business Layer enables the application to do global error/ exception logging and events tracing.
  • DataSecurity class of the Business Layer helps the application for encryption and decryption of sensitive data

4.    AccountPlus Pre-Requisites

To run Account Plus, the target machine should have,

  • Microsoft .NET Framework 2.0. To download the version, click the link below:

http://www.microsoft.com/downloads/details.aspx?FamilyID=0856eacb-4362-4b0d-

8edd- aab15c5e04f5&displaylang=en

  • Acrobat Reader. To download the version, click the link below:

http://get.adobe.com/reader/

  • Any of the following database
  1. i.            MS Access
  2. ii.            SQL Server
  3. iii.            My Sql
  4. iv.            Oracle

5.    AccountPlus Installation

  • Download AccountPlus2.0.
  • Unzip the directory AccountPlus2.0.
  • Find for the directory Setup
  • Double click to run AccountPlus setup. Keep on clicking next.

Note: Do remember to enter appropriate currency in Step 4, i.e. Rs. Or $ or anything so that your expenses are represented into the same currency. By default installer configures MS Access database, however it may be customized for any database.

Step4

6.    How to Change AccountPlus Database

AccountPlus may work with any of the database for example MS Access, MySql, SQL Server and Oracle. By default installer configures MS Access database for the application however it may be changed to any database. Downloaded package contains the db scripts for MySQL and SQL Server Database.

Follow the steps below to change the database.

  1. I. Prepare the MySQL or SQL Server database by running the scripts present into the database directory of the downloaded files.
  2. II. Go to the installation directory of AccountPlus. (Usually is C:\Program Files\AccountPlus)
  3. III. Find and open the ‘AccountPlus.UI.exe.config’ file in any suitable editor.
  4. IV. Locate ‘connectionStrings’ section. There you may get the connection strings for MySQL and SQL Server with the name mySqlCon and sqlServerCon respectively. Modify the connection string attribute with suitable values i.e. Appropriate Database name, User Id, Password, Server and Port etc.
  5. V. Locate ‘appSettings’ setion in the ‘AccountPlus.UI.exe.config’ file. Modify the value of ‘defaultConnection’ with the name of the connection String modified by you in the previous step.

Note: Make sure that while changing/ adding new connection string you are using correct providerName. E.g.

Database

Provider name

MS Access providerName=”MSACCESS”
MySQL providerName=”MYSQL”
Oracle providerName=”ORACLE”
SQL Server providerName=”SQLSERVER”
OLEDB providerName=”OLEDB”
ODBC providerName=”ODBC”

7.    Working with AccountPlus

1. Login

Provide the User name and password and click on ‘Login’ button to login into the application.

If you are selecting ‘Remember me’ then for next time application will automatically populate Username and Password.

Login

Logon Information

Click on info button to know the last log on information.

LogOnInfo

2. User Management

User management i.e. adding new users and modifying user details right is reserved for the admin user role only.

2.1 Creating new user

Login to the application as an admin user role,

  1. 1. Press ‘ALT+U’ or Go to Fileà Addà User

NewUser

  1. 2. A new window would appear. Select the desired user role and enter the user details. Click on ‘Add’ button to create user.

CreateNewUser

2.2 Searching any user

Press ‘ALT+U’ or go to File –>Add–>User. Into the window opened enter any of the details and click on search button.

Note: All the fields are not mandatory you may enter any of the details to search.

2.3 Editing user information

‘ALT+U’ or go to File –>Add–>User. Select the user from the grid whose information you want to edit. Selection of any row containing user information will make the user details available to the upper area where details may be changed. Click on ‘Update’ button to save the changes.

DeleteUserInformation

2.4 De activating user

‘ALT+U’ or go to File –>Add–>User. Select the user from the grid want to deactivate. Selection of any row containing user information will make the user details available to the upper area. Uncheck the ‘Active’ check box and click on ‘Update’ button to save the changes.

2.5 Activating user

‘ALT+U’ or go to File–>Add–>User. Select the user from the grid want to activate. Check the ‘Active’ check box and click on ‘Update’ button to save the changes.

3. Item Management

Item management i.e. adding new item and modifying item details right is given to all the user roles.

3.1 Creating new Item

  1. 1. Press ‘ALT+I’ or Go to File–> Add–> Item

CreateNewItem

  1. 2. A new window would appear. Enter the item details. Click on ‘Add’ button to create new item.

NewItem

3.2 Searching an item

Press ‘ALT+I’ or go to File –>Add–>Item. Into the window opened enter any of the details and click on search button.

Note: All the fields are not mandatory you may enter any of the details to search.

3.3 Editing item details

‘ALT+U’ or go to File –>Add–>User. Select the user from the grid whose information you want to edit. Selection of any row containing user information will make the user details available to the upper area where details may be changed. Click on ‘Update’ button to save the changes.

EditItemInformation

4. Expense Management

Only general users can add, update or delete the expenses. Admin can’t enter any expense. Users can edit or delete only their own expenses not other user’s expenses but can view all the users expense.

4.1 Adding new expense

Select the item against which you want to enter the expense. Selection of item will automatically populate the expense description where as user can change the same.  Enter other information like amount and date. Click on the ‘Add’ button to save the changes.

NewExpense

Once item is added successfully, newly added expense details would appear into the expense details grid shown right side of the screen.

Note: Expense date can’t be greater than today’s date.

4.2 Editing expense details

Select the particular expense from the grid needs to be edited. Selection of any row containing expense details will make the e details available to the left side of the screen where details may be edited. Click on ‘Update’ button to save the changes.

EditExpense

4.3 Deleting expense

Select the particular expense from the grid needs to be deleted. Selection of any row containing expense details will make the details available to the left side of the screen where details may be edited or deleted. Click on ‘Delete’ button to delete the expense.

DeleteExpense

5. Reports

Reporting is the most exciting part of AccountPlus. AccountPlus has mainly three reports:

·         Expense Report

·         Monthly Report

·         Analytic Report

Reports

5.1 Expense Report

Press F4 or click on the button present on tool bar to open expense report.

Once item is added successfully, newly added expense details would appear into the expense details grid shown right side of the screen.

ExpenseReport

Expense Report gives us the information about the

  • Total expense in the tenor.
  • Number of persons sharing the expense.
  • How much is the expense for each one. (Individual Contribution)

Based on these three pieces of information, AccountPlus calculates “How much each user has paid, and how much each participant is either supposed to pay to others participants or supposed to get from other participants”.

5.2 Monthly Report

Press F5 or click on the button present on tool bar to open monthly report.

Select the Month, Year for which report needs to be generated and click on ‘Generate’ button. Apart from the expense details this report will give the information about some other important things like Finalization Date, Total Expense and Days etc.

MonthlyReports

5.3 Analytic Report

Press F6 or click on the button present on tool bar to open analytic report.

Select the Month, Year for which report needs to be generated and click on ‘Generate’ button.

AnalyticReport

Same as Monthly report, Analytic Report also has two views Individual and Monthly.

Whenever we are generating the Analytic Report for a particular Month and Year, the Analytic Engine of AccountPlus checks whether expense data exists for the previous and next month data for the selected month and year. If data exists, Analytic Report will give the trend on a particular item or user about whether the expense has increased or decreased from the previous or next month.

Example

Suppose you have selected Dec, 2007 to generate the analytic report. Now, the Analytic Engine of AccountPlus will check whether the data exists for Nov, 2007 and Jan, 2008. If the data exists, then Analytic Report will compare the expense on each item or expense by users based on the view selected (Individual or Item wise). If data for previous month does not exist then Analytic Report will compare only the selected month and the next month.

6. Finalizing expense

Only admin user role has got the rights to finalize the expenses.

In order to understand the Finalization process, we will take the example of paper based account management system. In paper based account management system, at the end of the month, all the expenses occurred needs to be summed up, to calculate the total expense for the month. Then divide the total expense by number of persons supposed to share the expense. Like this, individual contribution is calculated. Now all the members will either receive an amount by another member or pay the amount to other members based on individual contribution and amount paid by each participant.

Once this process is completed, expense sheet needs to be tear up or to be cross marked.

Finalizing the expense in AccountPlus is the same process where admin makes sure that all the users have cleared their dues i.e. each participant have received or paid their dues. After finalizing the expense, current expense details would not be displayed on the home page but the data will remain in the database for analysis and reporting purposes.

How to finalize the expense?

Login as admin.

  • Press F4 to generate the expense report.
  • Click on Finalize button.

ExpenseFinalization

Expense report provides information about total expense and the dues for the particular tenor. Pay/Get is very important information in order to clear the dues, this information gives the information among the users about who is supposed to pay and who is supposed to get.

7. Edit user profile

Press ‘Alt+P’ or go to Edit–> Profile to bring the screen where user profile can be edited.

EditProfile

How to change password?

Check the check box ‘Change Password’. Enter old password, password and confirm passwords field s and click on ‘Submit’ button to save the details.

8. Database information

DBInfo

Press ‘Alt+Shift+S’ or go to Tools–> Database to open the database information window.

DBDetails

Note:

  • Only admin user role has got rights to back up the database.
  • Current version supports backup operation for MS Access database only.

.

9. System diagnostics

Press ‘Alt+Shift+D’ or go to Tools–> Diagnostics to open the diagnostics information window where application logs and trace can be viewed.

Diagnostics

  • Select the Log or Trace radio buttons to view Log or Trace files. Double click to open the file.
  • Export button may be used to export the Log/ Trace files.

10. Version information

Press F2 or go to Help–> About to open the about dialog box.

VersionInfo

11. Report a bug or contact author

Press F3 or go to Help–> Contact Admin to open window which helps the user to post their feed back or comments to the author.

ContactAdmin

  • Select appropriate option e.g. Report Bug, Feature request etc
  • Enter the message into the message text area.
  • Click ‘Send using Outlook’ to post the message to admin using Microsoft Outlook.