Create a sample 3- tier application using WCF


Looking for WCF 3 tier applications? Check out a sample WCF application. Here is the code for a 3 tier application using WCF.

Want to know how to create a 3-tier application using WCF? Before we discuss more about the sample code here lets first have a look on what is WCF.

WCF stands for Windows Communication Foundation. WCF is a set of .Net Framework based runtime application programming interfaces for developing distributed applications.

WCF service acts as Business Layer and this application includes classes for user creation. The three layers are:

  1. Applicaion layer

  2. Business layer

  3. Datalayer


First we will see Business Layer (BLL) for this application.

There will be 2 classes for BLL.
1. DataContrancts Classes
2. WCF Class for BLL functions which are used by Application Layer

DataContract Class for Users will be as follows:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;

namespace DSDCP.DataContracts
{
[DataContract]
public class User
{
[DataMember]
public string Code { get; set; }
[DataMember]
public string UserType { get; set; }
[DataMember]
public string UserName { get; set; }
[DataMember]
public string Password { get; set; }
[DataMember]
public int Status { get; set; }
}
}


The BLL Functions are inside WCF class and that will be as follows:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using DSDCP.DataContracts;
using DSDCP.DataLayer;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.EnterpriseServices;
using System.Configuration;
using System.Web;
using System.Web.Services;
using System.Runtime.InteropServices;


namespace DSDCPService
{
// NOTE: You can use the "Rename" command on the "Refactor" menu
to change the class name "Service1" in code, svc and
config file together.
public class Service1 : IDSDCPService
{

public void CreateUser(User objUser)
{
try
{
UserManager objUserManager = new UserManager();
objUserManager.CreateUser(objUser);
}
catch (Exception ex)
{
throw ex;
}
}
public DataSet SelectUser(User objUser)
{
try
{
UserManager objUserManager = new UserManager();
DataSet dset = objUserManager.SelectUser(objUser);
return dset;
}
catch (Exception ex)
{
throw ex;
}
}
public void UpdateUser(User objUser)
{
try
{
UserManager objUserManager = new UserManager();
objUserManager.UpdateUser(objUser);
}
catch (Exception ex)
{
throw ex;
}
}
public void DeleteUser(User objUser)
{
try
{
UserManager objUserManager = new UserManager();
objUserManager.DeleteUser(objUser);
}
catch (Exception ex)
{
throw ex;
}
}
}
}


There will be an interface for the class in the SVC file as follows:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using DSDCP.DataContracts;
using DSDCP.DataLayer;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.EnterpriseServices;
using System.Configuration;
using System.Web;
using System.Web.Services;
using System.Runtime.InteropServices;

namespace DSDCPService
{
// NOTE: You can use the "Rename" command on the "Refactor" menu
to change the interface name "IService1" in both code
and config file together.
[ServiceContract]
public interface IDSDCPService
{

[OperationContract]
[FaultContract(typeof(DSDCPCommunicatorException))]
void CreateUser(User objUser);
[OperationContract]
DataSet SelectUser(User objUser);
[OperationContract]
void UpdateUser(User objUser);
[OperationContract]
void DeleteUser(User objUser);

}




}




WCF invokes methods in Datalayer (DAL) and the class for User Entity in Datalayer will be as follows:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DSDCP.DataContracts;

namespace DSDCP.DataLayer
{
public class UserManager:DataManager
{
public DataSet SelectUser(User objUser)
{
try
{
DataSet dset = new DataSet();
SqlCommand cmd = GetCommand();
if (objUser.Code != null)
{
AddStringParameter(cmd, "@Code", objUser.Code);
}

if (objUser.UserName != null)
{
AddStringParameter(cmd, "@UserName", objUser.UserName);
}
if (objUser.UserType != null)
{
AddStringParameter(cmd, "@UserType", objUser.UserType);
}

cmd.CommandText = "<write stored procedure name here>";
dset = ExecuteDataset(cmd);
return dset;
}
catch (Exception ex)
{
throw ex;
}
}
public void CreateUser(User objUser)
{
try
{
SqlCommand cmd = GetCommand();
if (objUser.Code != null)
{
AddStringParameter(cmd, "@Code", objUser.Code);
}

if (objUser.UserName != null)
{
AddStringParameter(cmd, "@UserName", objUser.UserName);
}
if (objUser.UserType != null)
{
AddStringParameter(cmd, "@UserType", objUser.UserType);
}
if (objUser.Password != null)
{
AddStringParameter(cmd, "@Password", objUser.Password);
}
//if (objUser.Status != 0)
//{
AddIntegerParameter(cmd, "@Status", objUser.Status);

cmd.CommandText = "UserInsert";
ExecuteQuery(cmd);
}
catch (Exception ex)
{
throw ex;
}
}
public void UpdateUser(User objUser)
{
try
{
SqlCommand cmd = GetCommand();
if (objUser.Code != null)
{
AddStringParameter(cmd, "@Code", objUser.Code);
}

if (objUser.UserName != null)
{
AddStringParameter(cmd, "@UserName", objUser.UserName);
}
//if (objUser.UserType != null)
//{
AddStringParameter(cmd, "@UserType", objUser.UserType);

if (objUser.Password != null)
{
AddStringParameter(cmd, "@Password", objUser.Password);
}
if (objUser.Status != 0)
{
AddIntegerParameter(cmd, "@Status", objUser.Status);
}
cmd.CommandText = "UserUpdate";
ExecuteQuery(cmd);
}
catch (Exception ex)
{
throw ex;
}
}
public void DeleteUser(User objUser)
{
try
{
SqlCommand cmd = GetCommand();
if (objUser.Code != null)
{
AddStringParameter(cmd, "@Code", objUser.Code);
}

cmd.CommandText = "UserDelete";
ExecuteQuery(cmd);
}
catch (Exception ex)
{
throw ex;
}
}
}
}


We will add the reference of WCF class to our web/windows application and will use the functions defined in SVC file.


Attachments

  • Source Code (44194-15042-WCF.rar)
  • Comments



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: