Business Façade Logic in distributed n-tier applications






2.30/5 (12 votes)
Explains how to implement a Business Façade logic layer between the Presentation logic and the Business logic in an n-tier application
Introduction
If you are focused on implementing a distributed application architecture in .Net and if you are
looking for an interface between the Presentation tier and the Business Logic tier,
then the Business Façade classes would provide you with the interface you need, along with
its advantages.
What's a Façade
Façade is a Design Pattern. It provides a unified interface to a set of interfaces in a subsystem. It defines a higher-level interface that makes the subsystem easier to use.
If the Façade is placed between the Business logic and the Client interface, it offers a single, simple interface to the Business logic subsystem.
Why do I have to use the Façade
The purpose of using the façade class is to eliminate the difficulties that the client would otherwise have in sending requests to a complex set of service objects. The client sends its requests to an instance of the façade class, which acts as an agent manipulating the requests as necessary and sending them on to the set of business classes.At the cost of one class, the Façade makes it very easy for the Presentation Tier to access the methods of the Business Tier without having to navigate a bunch of potentially complex interfaces. The Façade class also promotes a weak coupling between the Business Rules Tier and the Presentation tier and hence lets you vary the components of the Business Rules without affecting the Presentation tier.
Façade Logic
The Façade layer used in the sample source has the following components- Factory Class -
MyFactory
- Façade Interface -
IMyFacade
- Façade Class -
MyFacade
MyFactory
MyFactory
is a Factory Class. This is another Design Pattern. Factory class lets a
class defer instantiation to subclasses. It is an interface for creating
objects of the Façade Class. The Factory class implements a method called CreateFacade
which is solely responsible for instantiating objects of the IMyFacade
interface class.
IMyFacade
The IMyFacade
is the Façade Interface that defines the methods implemented in the Façade
classes. IMyFacade
delegates the requests to appropriate classes that implement the methods.
QJFacade
This class contains different subclasses:
UIFacade
- used by component located on Web ServerWSFacade
- used by Web ServicesNRFacade
- used through .Net Remoting
Each of these classes contain their own implementation of the methods that are defined in the
interface IMyFacade
. These methods make use of the Business Logic functionality. It is
upto these classes to handle the interaction between the Business Logic.
Class Diagram
Implementation
Dim iFacade As IMyFacade ' Facade Interface
Dim Factory As MyFactory ' Factory Class
' Create a new factory class
Factory = New MyFactory()
' Create a new Facade class. In this case we create a new instance of UIFacade
iFacade = Factory.CreateFacade(MyFactory.MyFType.UIF) ' Type UIF
' Call the required methods which inturn make use of the Business Logic methods
Dim strReturn As String
strReturn = iFacade.DoSomeLogic()
The Presentation layer creates an instance of the Factory class MyFactory
.This
instance is used to call the Factory method CreateFacade
. CreateFacade
requires a Enumerated type as a parameter which will decide the type of the Façade class to be
instantiated.
The method calling the CreateFacade
recieves an
object of type specified in the parameter. This object is then used to call the
relevant method in the Façade class.
For example :
iFacade = Factory.CreateFacade(UIF)
returns an instance of UIFacade
.
iFacade = Factory.CreateFacade(WSF)
returns an instance of WSFacade
iFacade = Factory.CreateFacade(NRF)
returns an instance of NRFacade
The CreateFacade
is implemented as follows
Public Function CreateFacade(ByVal nType As Integer) As Object
' Create an instance of the Façade classes based on the Type required.
If (nType = MyFType.UIF) Then
Return New MyFacade.UIFacade()
ElseIf (nType = MyFType.WSF) Then
Return New MyFacade.WSFacade()
ElseIf (nType = MyFType.NRF) Then
Return New MyFacade.NRFacade()
End If
End Function
That's it
The source code contains a sample console application illustrating the use of the Façade Classes.
The Façade logic can be used as an interface between your Presentation Tier and Business Logic giving
you the following advantages
- Provides you with a level of isolation between the Presentation Logic and the Business Logic. The Façade shields the Presentation Logic from the inner workings of the Business Logic.
- Interfacing between many modules and classes is made more manageable. It makes the architecture more simpler.
- Minimizes the communication and dependency between subsystems. Provides an amount of weak coupling between the Presentation Logic and the Business Logic
The Presentation Logic will now be only involved in presenting the UI. The Business Logic will only be involved in delivering the Business related logic. All the communication between the Presentation Layer and the Business Logic Layer will now be fully handled by the Business Façade, thus isolating the components of the application in terms of functionality.