Click here to Skip to main content
5,785,816 members and growing! (18,443 online)
Email Password   helpLost your password?
Web Development » ASP.NET » General     Beginner License: The Code Project Open License (CPOL)

Interfaces + Factory pattern = Decoupled architecture

By Shivprasad koirala

Interfaces + Factory pattern = Decoupled architecture
C# (C# 1.0, C# 2.0, C# 3.0, C#), .NET (.NET, .NET 3.5, .NET 3.0, .NET 1.0, .NET 1.1, .NET 2.0), ASP.NET, Architect

Posted: 10 Nov 2008
Updated: 10 Nov 2008
Views: 4,727
Bookmarked: 21 times
Note: This is an unedited reader contribution
Announcements
Loading...



Search    
Advanced Search
Sitemap
32 votes for this Article.
Popularity: 4.14 Rating: 2.75 out of 5
12 votes, 37.5%
1
2 votes, 6.3%
2
1 vote, 3.1%
3
2 votes, 6.3%
4
15 votes, 46.9%
5
Note: This is an unedited contribution. If this article is inappropriate, needs attention or copies someone else's work without reference then please Report This Article

Interfaces + Factory pattern = Decoupled architecture

 

Introduction
 

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

For past some days I have been writing and recording videos in design patterns, UML, FPA, Enterprise blocks and lot you can watch the videos at http://www.questpond.com 

You can download my 400 .NET FAQ EBook from http://www.questpond.com/SampleDotNetInterviewQuestionBook.zip 

Everyone’s favorite the three tier architecture

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

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 would like to have support for multiple clients depending on user interfaces. This is not so volatile as compared to business objects changes. Even if the customer wants to change its not on urgent basis. Business validations needs 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 project who wants to migrate from one database to other 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 business sections are the most volatile and when there is a change it needs to be changed instantly as compared to UI and back end databases.
 

Controlling change ripples


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

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 it’s sending the values in a normal .NET data type. You can see how the ‘InsertInvoiceDetails’ of the DAL component had normal .NET data types passed.

 

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 changes across all the three layers. That means you need compile the whole project.
 

Magic of Interfaces and factory pattern
 

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

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

 

Second we define a factory which creates the ‘clsInvoiceDetails’ object. Below is the sample code for the same.
 

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

 

So the client only refers the interface which is the proxy and the factory creates the object of the concrete class ‘ClsInvoiceDetail’. In 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 new keyword client in the UI then the UI needs to be direct contact with the concrete object which leads to heavy coupling.
 

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.
 

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 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.
 

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

You can get the source code in this link Click here
 

License

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

About the Author

Shivprasad koirala


I am currently a CEO of a small E-learning company in India. We are very much active in making training videos , writing books and corporate trainings. You can visit about my organization at www.questpond.com and also enjoy the videos uploaded for Design patter, FPA , UML , Project and lot. I am also actively involved in RFC which is a financial open source madei in C#. It has modules like accounting , invoicing , purchase , stocks etc.
Occupation: Architect
Company: http://www.questpond.com
Location: India India

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 23 of 23 (Total in Forum: 23) (Refresh)FirstPrevNext
GeneralA good article is which makes you think....And this is goodmemberivanshettigar5:04 11 Nov '08  
GeneralIMHO this is not a good example of a factory, or even dependency injection[modified]memberTom Janssens3:42 11 Nov '08  
GeneralRe: IMHO this is not a good example of a factory, or even dependency injectiomemberShivprasad koirala4:51 11 Nov '08  
Generalthis is just more code to handle...memberSeishin#3:36 11 Nov '08  
GeneralRe: this is just more code to handle...memberShivprasad koirala4:59 11 Nov '08  
GeneralRe: this is just more code to handle...memberSeishin#5:15 11 Nov '08  
GeneralRe: this is just more code to handle...memberShivprasad koirala7:38 11 Nov '08  
GeneralRe: this is just more code to handle...memberterragenegeystur7:42 11 Nov '08  
GeneralRe: this is just more code to handle...memberSeishin#7:54 11 Nov '08  
GeneralRe: this is just more code to handle...memberterragenegeystur8:16 11 Nov '08  
GeneralRe: this is just more code to handle...memberSeishin#8:29 11 Nov '08  
GeneralRe: this is just more code to handle...memberterragenegeystur9:20 11 Nov '08  
GeneralRe: this is just more code to handle...memberShivprasad koirala17:50 11 Nov '08  
GeneralRe: this is just more code to handle...memberterragenegeystur20:46 11 Nov '08  
GeneralRe: this is just more code to handle...memberSeishin#7:50 11 Nov '08  
GeneralRe: this is just more code to handle...memberShivprasad koirala8:10 11 Nov '08  
GeneralTests ...memberJammer3:35 11 Nov '08  
GeneralGood one.memberhemasarangpani22:50 10 Nov '08  
GeneralRe: Good one.memberShivprasad koirala22:53 10 Nov '08  
GeneralI was confused if we not use Interfaces+Factory pattern.memberleegool22:08 10 Nov '08  
GeneralRe: I was confused if we not use Interfaces+Factory pattern.memberShivprasad koirala22:46 10 Nov '08  
GeneralCongrats on 52th article ,,,But nothing newmemberBAIJUMAX22:02 10 Nov '08  
GeneralRe: Congrats on 52th article ,,,But nothing newmemberShivprasad koirala22:47 10 Nov '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 10 Nov 2008
Editor:
Copyright 2008 by Shivprasad koirala
Everything else Copyright © CodeProject, 1999-2009
Web13 | Advertise on the Code Project