Thursday, March 6, 2014

Single Instance Window in WPF

Today we are going to implement the single instance in wpf.
That is the created WPF application is opened only once.
The code for the App.xaml is

<Application x:Class="MyProject.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             Startup="Application_Startup" >
    
</Application>

and the code behind for the Application_Startup is

 private void Application_Startup(object senderStartupEventArgs e)
        {
            var appProcessName = Process.GetCurrentProcess().ProcessName;
            var matchingProcesses = Process.GetProcessesByName(appProcessName);
            if (matchingProcesses.Count() == 1)
            {
                MainWindow mainWindow = new MainWindow();
                mainWindow.Show();
            }
            else
            {
                Application.Current.Shutdown();
            }
        }

When the above code implemented, Rebuild and create the application setup now run the application many times but it will be open only once.

Wednesday, February 26, 2014

3-Tier Architecture Implementation

Introduction

3 tier architectures are use to make the application more understandable, easy to maintain the application and most important is easy to modify the application i.e. flexible application.  

Background

3 tier architecture includes 3 main layers. They are, 
  1. Application Layer or Presentation Layer
  2. Business Logic Layer(BLL) or Business Access Layer(BAL)
  3. Data Access Layer(DAL)  

Application Layer or Presentation Layer 

Application Layer or Presentation Layer is nothing but the User interface. This layer has no interaction with the Database and Servers and all. It is used for the interaction between the user and Application. 

Business Logic Layer(BLL) or Business Access Layer(BAL) 

This layer contains the business logic, calculations related with the data and validating data. It acts as the interface between the Application layer and Data Access Layer 

Data Access Layer(DAL)   

Data Access Layer is the only layer which interact with the server or database, all the backend operation in the Application is done in this layer only. Lets see the coding part, so that it will understand easily. 
Before creating application make sure that you created the database with tables and stored procedures.
Note: The stored procedure parameters(name, type and size) must be same  in the DAL and Stored Procedures.
The 3 tier Architecture will like the following, or you can use separate class libraries for DAL and BLL. 

Using the code  

When the validation and all finished pass the values to the BLL  by creating an object for the BL class in the UI and send the data to the method. 

BL blObj = new BL();
val = blObj.AddUser(txtName.Text, txtFatherName.Text);
Then pass the data to the DBServices class. Why i am not executing the queries directly in this class itself is, suppose if you are working in a project and you have to execute many queries it will lead to create a query for each time. Instead of that i created queries in a separate class and called it whenever i need.

List<SqlParameter> paraList = new List<SqlParameter>();
paraList.Add(Db.CreateParameter("@Name", name, SqlDbType.VarChar, 50));
paraList.Add(Db.CreateParameter("@FatherName", fatherName, SqlDbType.VarChar, 50));
integerValue = Convert.ToInt32(Db.ExecuteNonQuery("SP_InsertUser", paraList));
Where SP_InsertUser is my StoredProcedure name.
This will sql type create parameter and insert in the table as i written the insert query in my stored procedure itself. The connection string is provided from the web.config and the query will be executed using the DB class.
public static int ExecuteNonQuery(string spName,                                                     List parameters)
        {
            try
            {
                using (SqlConnection sqlConnection = new SqlConnection())
                {
                    sqlConnection.ConnectionString = connectionString;
                    using (SqlCommand command = new SqlCommand())
                    {
                        command.Connection = sqlConnection;
                        command.CommandType = CommandType.StoredProcedure;
                        command.CommandText = spName;
                        if ((parameters.Count > 0))
                        {
                            foreach (SqlParameter param in parameters)
                            {
                                command.Parameters.Add(param);
                            }
                        }
                        command.Connection.Open();
                        return command.ExecuteNonQuery();
                    }
                }
            }
            catch (SqlException ex)
            {
                return 0;
            }
        } 
Now the data will be inserted and if it success means will returns a integer value 1 else 0.
The stored procedure will be
Create procedure SP_InsertUser(@Name varchar(50),@FatherName varchar(50))
as
begin
Insert into UserTable(Name,FatherName)values(@Name,@FatherName)
end  

Points of Interest
I wrote the method of sending string data only. In case if you are sending integer value to the database means
just change the type as int and size as 0.
paraList.Add(Db.CreateParameter("@SNo", SNo, SqlDbType.Int, 0)); 
Similarly if it is binary data(like image means create a separate method in DB class for that and perform the operation as similar in create parameter except the parameter type.

The Source code for this article can be found in here