Blue Theme Orange Theme Green Theme Red Theme
 
Home | Forums | Videos | Photos | Downloads | Blogs | E-Books | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article Submit a Blog 
 Login Close
User Id:
Password:
 
Forgot Password
Forgot Username
Why Register
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
Dundas Dashboard
 Resources  
Close
 Our Network  
Close
Search :       Advanced Search »
Home » .NET 3.0/3.5 » Interfaces + Factory pattern = Decoupled architecture

Interfaces + Factory pattern = Decoupled architecture

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.

Author Rank:
Technologies: .NET 2.0, ASP.NET 3.5,Visual C# .NET
Total downloads : 90
Total page views :  2403
Rating :
 4.5/5
This article has been rated :  2 times
   Print Read/Post comments Post a comment  Rate  
   Email to a friend  Bookmark  Similar Articles  Author's other articles  
Download Files:
InterfaceFactory.zip
 
Become a Sponsor


Related EbooksTop Videos


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.

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.

1.jpg 

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.

2.jpg

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.

3.jpg

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.

Please find the Source code at the top of the article.


Login to add your contents and source code to this article
 [Top] Rate this article
 About the author
 
Shivprasad
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.
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Go.NET
Build custom interactive diagrams, network, workflow editors, flowcharts, or software design tools. Includes many predefined kinds of nodes, links, and basic shapes. Supports layers, scrolling, zooming, selection, drag-and-drop, clipboard, in-place editing, tooltips, grids, printing, overview window, palette. 100% implemented in C# as a managed .NET Control. Document/View/Tool architecture with many properties&events. Optional automatic layout.
Dundas Software
Dundas Chart for .NET is the most advanced .NET charting package available today.  With an extremely complete feature set, elegant architecture and easy implementation, Dundas Chart can quickly add advanced Charting functionality to enhance and transform ASP.NET and Windows Forms applications.  Whether you are implementing charting into internal projects, or building applications for clients, Dundas Chart offers advanced technology and advanced results to get the most out of data.
Clickatell's SMS Gateway
Clickatell's Developer Solutions allow you to SMS enable any website or application via a range of API's. Learn More about our API connections.
Free access to .NET Memory Management video
Everything you need to know about Garbage Collection, Temporary Objects, Fragmentation, Finalization and common causes of memory leaks in .NET. Watch the video here.
Microsoft Visual Studio 2010
Microsoft Visual Studio 2010 offers more to developers than any other Visual Studio release. Work more productively and collaboratively-with greater control over your work at every step. The Beta 2 can give you a head start on achieving efficiency.
 
   Print Read/Post comments Post a comment  Rate  
   Email to a friend  Bookmark  Similar Articles  Author's other articles  
Download Files:
InterfaceFactory.zip
 
 Post a Feedback, Comment, or Question about this article
Subject:  
Comment:  
Dundas Dashboard
Become a Sponsor
 Comments
Missing .mdf file by sudheer On March 24, 2009
hi i am getting errors due to the missing of INVOICE.mdf file in zip file ..pls add that one to zip file...
Reply | Email | Delete | Modify | 

 Hosted by MaximumASP  |  Found a broken link?  |  Contact Us  |  Terms & conditions  |  Privacy Policy  |  Site Map  |  Suggest an Idea  |  Media Kit
Current Version: 5.2009.6.2
 © 1999 - 2009  Mindcracker LLC. All Rights Reserved