Click here to Skip to main content
15,881,248 members
Articles / Web Development / ASP.NET

Interfaces + Factory pattern = Decoupled architecture

Rate me:
Please Sign up or sign in to vote.
3.34/5 (41 votes)
10 Nov 2008CPOL4 min read 93.7K   1.1K   53   27
How we can use interfaces and the Factory pattern to create a truly decoupled architectural framework.

Introduction

In this tutorial, we will try to understand how we can use Interfaces and the Factory pattern to create a truly decoupled architectural framework. In this sample, we will take up a simple three tier architecture and apply interfaces and the Factory pattern to see how we can transform the three tier into a truly decoupled architecture.

Everyone’s favorite, the three tier architecture. OK, everyone know what the three tier architecture is. I will talk a bit about it and then we will get down to the issues related to a three tier architecture. Basically, as we all know, in a three tier architecture, the UI code is in the client section, business objects are nothing but business classes which have business validation, and the data access layer does all the database operations. Till here, everything is good and nice.

Image 1

If we analyze these three sections, one of the important points to be noted is that business validations are highly volatile. Business rules change as the way of business changes with changing times.

The second volatile section is the client. People like to have support for multiple clients depending on user interfaces. This is not so volatile as compared to business object changes. Even if the customer wants to change, it is not on an urgent basis. Business validations need to be changed instantly.

Data access is the least volatile of all. At least in my whole project career I have still to come across a project which wants to migrate from one database to another database frequently. There are times when there is a need but that’s not urgent and is treated as a migration project.

So the conclusion is business sections are the most volatile and when there is a change, it needs to be changed instantly as compared to UI and backend databases.

Controlling change ripples

Below is a business class called clsInvoiceHeader. You can see in the code snippet that the user interface is setting the value of the business object. So the user interface is directly consuming the class.

C#
clsInvoiceDetail objInvoiceDetail = new clsInvoiceDetail ();
objInvoiceDetail.CustomerName = txtCustomerName.Text;
objInvoiceDetail.CustomerAddress = txtCustomerAddress.Text;
objInvoiceDetail.InvoiceComments = txtComments.Text;
objInvoiceDetail.InvoiceDate=Convert.ToDateTime(txtInvoiceDate.Text);

At the other end, when the businesses object connects with the data access layer, its sending the values in a normal .NET data type. You can see how InsertInvoiceDetails of the DAL component has normal .NET data types passed.

C#
public class clsInvoiceDetail
{
    public void Insert(int _InvoiceReference)
    {
        clsInvoiceDB objInvoiceDB = new clsInvoiceDB();
        objInvoiceDB.InsertInvoiceDetails(_Id, _InvoiceReference, _ProductId, _Qty);
    }
}

So what will happen if the business objects change? It will just ripple the changes across all the three layers. That means you will need to compile the whole project.

Image 2

Magic of Interfaces and the Factory pattern

So how do we control those ripple effects across all the tiers? The answer to this is by using Interfaces and the Factory pattern. So let’s first create an interface for the clsInvoiceDetail class. Below is the Interface code:

C#
public interface IInvoiceDetail
{
    string InvoiceReferenceNumber
    {
        get;
    }
    string CustomerName
    {
        set;
        get;
    }
    string CustomerAddress
    {
        set;
        get;
    }
    string InvoiceComments
    {
        set;
        get;
    }
    DateTime InvoiceDate
    {
        set;
        get;
    }
}

Secondly, we define a factory which creates the clsInvoiceDetails object. Below is the sample code:

C#
public class FactoryFinance
{
    public static IInvoiceDetail getInvoiceDetail()
    {
        return new clsInvoiceDetail();
    }
}

So the client only refers to the interface which is the proxy and the factory creates the object of the concrete class ClsInvoiceDetail. This way the client never knows about the concrete class ClsInvoiceDetail. The factory class is introduced to avoid the new keyword use in the client. If we have a new keyword client in the UI, then the UI needs to have direct contact with the concrete object which leads to heavy coupling.

C#
IInvoiceDetails objInvoiceDetail = FactoryFinance.GetInvoiceDetails();
objInvoiceDetail.CustomerName = txtCustomerName.Text;
objInvoiceDetail.CustomerAddress = txtCustomerAddress.Text;
objInvoiceDetail.InvoiceComments = txtComments.Text;
objInvoiceDetail.InvoiceDate=Convert.ToDateTime(txtInvoiceDate.Text);

The other end, i.e., the database layer, is also easy now. We need to pass the interface object in the database function rather than .NET primitive datatypes.

C#
public class clsInvoiceDetail : IInvoiceDetail
{
    public void UpDateToDatabase()
    {
        clsInvoiceDB objInvoiceDB = new clsInvoiceDB();
        objInvoiceDB.InsertInvoiceDetails(this);
    }
}

So here’s what we have done: the UI is isolated using Factory and a business object interface. The database layer is isolated using the interface. So in short, when we change any logic, we do not need to recompile the whole project and thus we have controlled the business logic ripple effect in the business logic project itself.

Image 3

We have also uploaded the source code for an invoice project to demonstrate how the above decoupling works. One project is a simple three tier architecture while the other shows how we have used the Factory pattern and Interface to increase decoupling between the tiers.

You can get the source code from the link above.

For Further reading do watch  the below interview preparation videos and step by step video series.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect https://www.questpond.com
India India

Comments and Discussions

 
GeneralMy vote of 3 Pin
usernametakenalready14-Dec-11 17:45
usernametakenalready14-Dec-11 17:45 
Generalmama really i searching till date, i confused about it, Pin
jameschowdary28-Jan-10 21:57
jameschowdary28-Jan-10 21:57 
QuestionNew to design patterns Pin
Micl11-Jun-09 5:33
Micl11-Jun-09 5:33 
Generalsample is not working Pin
Member 428981425-Mar-09 6:56
Member 428981425-Mar-09 6:56 
GeneralA good article is which makes you think....And this is good Pin
ivanshettigar11-Nov-08 4:04
ivanshettigar11-Nov-08 4:04 
GeneralIMHO this is not a good example of a factory, or even dependency injection[modified] Pin
Tom Janssens11-Nov-08 2:42
Tom Janssens11-Nov-08 2:42 
GeneralRe: IMHO this is not a good example of a factory, or even dependency injectio Pin
Shivprasad koirala11-Nov-08 3:51
Shivprasad koirala11-Nov-08 3:51 
Generalthis is just more code to handle... Pin
Seishin#11-Nov-08 2:36
Seishin#11-Nov-08 2:36 
GeneralRe: this is just more code to handle... Pin
Shivprasad koirala11-Nov-08 3:59
Shivprasad koirala11-Nov-08 3:59 
GeneralRe: this is just more code to handle... Pin
Seishin#11-Nov-08 4:15
Seishin#11-Nov-08 4:15 
GeneralRe: this is just more code to handle... Pin
Shivprasad koirala11-Nov-08 6:38
Shivprasad koirala11-Nov-08 6:38 
GeneralRe: this is just more code to handle... Pin
terragenegeystur11-Nov-08 6:42
terragenegeystur11-Nov-08 6:42 
GeneralRe: this is just more code to handle... Pin
Seishin#11-Nov-08 6:54
Seishin#11-Nov-08 6:54 
GeneralRe: this is just more code to handle... Pin
terragenegeystur11-Nov-08 7:16
terragenegeystur11-Nov-08 7:16 
GeneralRe: this is just more code to handle... Pin
Seishin#11-Nov-08 7:29
Seishin#11-Nov-08 7:29 
GeneralRe: this is just more code to handle... Pin
terragenegeystur11-Nov-08 8:20
terragenegeystur11-Nov-08 8:20 
Hahahaha good logic so i said your logic is so clear and nice.
GeneralRe: this is just more code to handle... Pin
Shivprasad koirala11-Nov-08 16:50
Shivprasad koirala11-Nov-08 16:50 
GeneralRe: this is just more code to handle... Pin
terragenegeystur11-Nov-08 19:46
terragenegeystur11-Nov-08 19:46 
GeneralRe: this is just more code to handle... Pin
Seishin#11-Nov-08 6:50
Seishin#11-Nov-08 6:50 
GeneralRe: this is just more code to handle... Pin
Shivprasad koirala11-Nov-08 7:10
Shivprasad koirala11-Nov-08 7:10 
GeneralTests ... Pin
Jammer11-Nov-08 2:35
Jammer11-Nov-08 2:35 
GeneralGood one. Pin
hemasarangpani10-Nov-08 21:50
hemasarangpani10-Nov-08 21:50 
GeneralRe: Good one. Pin
Shivprasad koirala10-Nov-08 21:53
Shivprasad koirala10-Nov-08 21:53 
GeneralI was confused if we not use Interfaces+Factory pattern. Pin
leegool10-Nov-08 21:08
leegool10-Nov-08 21:08 
GeneralRe: I was confused if we not use Interfaces+Factory pattern. Pin
Shivprasad koirala10-Nov-08 21:46
Shivprasad koirala10-Nov-08 21:46 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.