A common practice observed during creating a service is heavy usage of ServiceFacade. By Façade definition, A facade is an object that provides a simplified interface to a larger body of code, such as a class library. In this post I’ll be discussing about the challenge that are faced with ServiceFacade classes while working on SOA (Service Oriented Architecture).
Note: In this article we’ll be using WCF service contracts for sampling. The language is C# and code editing tool used is Visual studio 2013.
Let’s take a look at the high level design of Façade pattern.

Usually when the services are created we only concentrate on developing the server side functionality without thinking of fact that how service could grow in future because of the business. A simple example, creating a service that manages products of an eCommerce site. Suppose the product service provides the following operations:
- Create a product
- Delete a product
- Edit product
- GetProduct
- GetProductsList
- Search product
- Miscellaneous other operation
Now, let’s take a look at the service contract. I’ll be taking the sample of WCF service contracts for demonstration,
- public interface IProductService
- {
- [OperationContract]
- IEnumerable < Product > GetProductsList(Guid categoryId, int pageIndex, int pageSize);
- [OperationContract]
- Product Edit(Model.Product product);
- [OperationContract]
- Product Get(Guid productId);
- [OperationContract]
- Product Create(Model.Product product);
- [OperationContract]
- IEnumerable < Product > SearchProduct(SearchCriteria criteria);
- [OperationContract]
- void Delete(Guid productId);
- }
- public class ProductService: IProductService
- {
- IRepository < Product > product;
- ILogger logger;
- InventoryService service;
- //...
- //...
- // And any other dependencies required to fulfill
- // operations requirement here.
- public IEnumerable < Product > GetProductsList(Guid categoryId, int pageIndex, int pageSize)
- {
- // A try..catch{} for error/exception handling
- // Check inventory step
- // Get inventory availabilities step
- // Prepare products lists step
- // ... bunch of other stuff.
- // Log, if required etc..
- // return the response.
- }
- public Product Edit(Product product)
- {
- // Actual core logic of Editing a product.
- }
- public Product Get(Guid productId)
- {
- // Actual core logic of Fetching a product.
- }
- public Product Create(Product product)
- {
- // Actual core logic of Adding a product.
- }
- public IEnumerable < Product > SearchProduct(SearchCriteria criteria)
- {
- // Actual core logic of Seraching products.
- }
- public void Delete(Guid productId)
- {
- // Actual core logic of Deleting a product.
- }
- }
The following figure illustrating the coupling of CoreLogic and ServiceContracts:

Let’s take a look at the downside of such Facades in SOA especially on services side:
Architect’s point of view
The coupling of the core service logic to contracts and implementation resources can inhibit its evolution and negatively impact service consumers.
“When a service is subject to change either due to changes in the contract or in its underlying implementation, this core service logic can find itself extended and augmented to accommodate that change. As a result, the initial bundling of core service logic with contract-specific or implementation-specific processing logic can eventually result in design-time and runtime challenges.”
[Ref. SOA Design Patterns by Thomas Erl (Chapter 12)].
Developer’s point of view
- A huge bodied class carrying out all business logics (or core service logic).
- Class became larger and larger as more methods will get added.
- Hard to Unit test due to lengthy methods.
- A centralize place where multiple developers will be working on it fix bugs/adding new operations. Now you can imagine how this central place can easily be polluted with stuff.
- Bug prone!!! Fixing a bug could end up introducing multiple bugs also known as “Rebugging code”. See Fallacy of resue.
Considering both scenarios; ServiceFacades are problems and may become problem in future for maintenance so I decided to write this post as reference to developer seeking solution to this problem.
Solution approach
The problem can be solved by separating core logic from service contracts(façade) to separate self-operating entities. The following figure illustrates the idea of separating the Contracts from CoreLogic:

From implementation point of view let’s try to solve this problem using Command Pattern.
Why command pattern?
A command can represent individual operation of service contract. Then we can use a dispatcher to invoke a particular command. Also the Core logic of individual operation can reside in commands. This is “An approach” to solve above problem.
Note: The implementation of CommandPattern that I’ll use in this post might differ from the samples given on various sources. Design patterns are Notion to use the power of Object Oriented world. Implementation can vary but it shouldn’t violate principles.
Simplifying ServiceFacade
The following figure shows the idea of using commands and redirecting each operation request to dedicated command via command dispatcher.

Now let’s go back to the code and create a new project which will consists of Commands representing each operation from Service Contract. So let’s create a class library project and name the project “ServiceOperations”. Well I just named it because “Naming is one of the hardest thing in computer programming world”.

In this project we have 3 project folders which I have created for separating the classes by their respective responsibilities.
- Base – Consists of Abstract classes and interfaces.
- Commands – Consists of all commands and command result(we’ll be discussing these two).
- Handlers – Consists of Handlers which will execute the commands.
Now we’ll start adding commands with respect to operations from service contract. For sampling we’ll create a command for Get(Guid ProductId) operation from service contract. To start with we need some foundation classes to implement command pattern under Base folder.
BaseCommand
- /// <summary>
- /// Each command would be dervied from Base command
- /// </summary>
- public abstract class BaseCommand
- {
- /// <summary>
- /// Logical name to represent the command.
- /// [optional] and can be used for other pusposes like logging/error reporting etc.
- /// </summary>
- public string Name { get; protected set; }
- }
- /// <summary>
- /// Each command must have a result to send back.
- /// Any command restul must inherit from this class.
- /// </summary>
- public abstract class BaseCommandResult
- {
- /// <summary>
- /// Contains error details, if occurred during execution.
- /// </summary>
- public ErrorDetails Error { get; set; }
- }
- /// <summary>
- /// Represent error details happened during command execution.
- /// </summary>
- public class ErrorDetails
- {
- /// <summary>
- /// Error code, For production level debugging purpose(representing technical issue type).
- /// </summary>
- public int Code { get; set; }
- /// <summary>
- /// Application friendly error message.
- /// </summary>
- public string Message { get; set; }
- }


Banketeshvar NarayanPosted Nov 18, 2015, 4:24 AM
Nice Share
Upendra Pratap ShahiPosted Nov 17, 2015, 10:13 AM
nice info.....
Humayun Kabir MamunPosted Nov 17, 2015, 6:56 AM
Nice...
Sibeesh VenuPosted Nov 17, 2015, 3:55 AM
Nice Share
Former memberPosted Nov 17, 2015, 3:45 AM
nice one
Harshad PansuriyaPosted Nov 17, 2015, 3:28 AM
Nice one
Santhakumar MunuswamyPosted Nov 17, 2015, 2:55 AM
Good article