When you talk with fellow developers and you mention Service Layer people automatically hear WCF Services. Did i miss a book or some guidance document from Microsoft, stating that WCF stands for service layer?

For those who have been around for a while, back in the dark ages of .NET (pre WCF Era) there where amongst others the following communication options:

  • Web services (XML)
  • .NET Remoting (XML or Binary)
  • Communication using the Socket/SocketPool classes

Each with their specific configuration, setup and usage.

With the release of .Net Framework 3.0/3.5, Microsoft created (or bought) a way to unify all of those options into 1 product that makes it easy, well almost easy, to switch from one option to another. And they named it Windows Communication Foundation.

What is a Service Layer and when should we implement one?

A Service Layer defines an application’s boundary and its set of available operations from the perspective of interfacing client layers. It encapsulates the application’s business logic, controlling transactions and coordinating responses in the implementation of its operations. [Martin Fowler]

You typically introduce a Service Layer when you have complex use cases, which possibly will be reused by different UI’s, like for instance between a ASP.NET MVC application, a Windows Service or a Console application and you want to avoid code duplication.  Or you want to separate your use cases from your UI Flow for better testability.

How should we use WCF?

It should be an abstraction layer, you simplify the underlying relational/domain complexity into understandable ServiceContracts with their respective model (data contracts).

And if we do need WCF, it’s because we really need it YAGNI:

  • We have a need for distributed computing
  • We need to be able to communicate across the web or across the enterprise

And yes, using WCF, introduces complexity but you really needed it.

How are we using it?

Most of us are using it as a gateway into our data retrieval and crud methods, sending entire domainobject-graphs (anemic) with all their complexity over the wire.
About 90% of the time, these objects are serialized to SOAP.
Just so we can use these WCF services, hosted on a IIS Server in our web application, hosted on the same IIS server.

Reason why we keep on doing this:

About 70% of the time the answer is: It’s so easy when we need to make our WCF publicly available. (other possible answer is Simply because we can)

Please consider following scenario and hopefully you will rethink your answer:

public class Product
  public Product()

  public double Prise { get; set; }

Which off course is nicely used in following WCF Service:

public interface IProductService
  Product GetProductById(int id);


Armand (Architect): John, Lisa has made a screw up in code, you need to rename the property Prise to Price in our product model.
John ( Senior Developer): Can’t do Armand!
Armand (Architect): Why not, John?
John (Senior Developer) : [Hesitates] Well … erm … because we have to call all of our customers and tell them we have a breaking API change.

Definition taken from MSDN

#region lets you specify a block of code that you can expand or collapse when using the outlining feature of the Visual Studio Code Editor. In longer code files, it is convenient to be able to collapse or hide one or more regions so that you can focus on the part of the file that you are currently working on.

Mind-blowing example

public class PieceTheResistence
   #region SomeNameShamelesslyPointingAtTheIntendOfTheWrappedCode
      public void MethodOne()
         // Piece of Art code
      public void MethodTwo()
         // Another Piece of Art

   #region SomeOtherNameShamelesslyPointingAtTheIntendOfTheWrappedCode
      public void SecondMethodOne()
         // Piece of Art code
      public void SecondMethodTwo()
         // Another Piece of Art

Ah, i see, i can collapse the region i am not working on, which will keep me focused on the code i am working on.

Why am i having problems with keeping me focused on the code i need to work on ?

Could be that i kept up all night, writing some blog posts or playing on my XBOX, PS3. Being deprived of sleep can have negative effects on concentration.

But a much more common scenario is that the class simply has too much code, in other words the class has too many responsibilities.

Create classes from the regions, so you’ll end up with classes with names revealing their intend. IMHO you don’t need regions if you design classes following the Single responsibility principle (SRP)

Half a decade ago, my interest began to grow for Test Driven Development. After some research i stumbled my way into the wonderful world of NUnit.

After one year of experimenting with TDD, i began to grasp that the last D in TDD should have been standing for “Design“ and not for “Development“. As creating testable classes means making use of dependency injection in some way, making classes that do only one thing and do it great,…

But wait isn’t that the SOLID Design principle?

After that i took a leap into ORM, because i was tired of repeating myself, and writing the same “SELECT x,y,z FROM TableX” over and over again. Got introduced into NHibernate, as for the time being it was the only existing ORM and it enabled me to use JAVA tech’s as a reference.

After that i started reading about Domain Driven Design and later on Command Query Separation.

And now about 3 years later, i begin to wonder, wouldn’t i have been a happier person if i didn’t learn about all of this. Because the only thing i see people do is :

  • Methods with an average of 300 lines of code
  • Grouping those methods in classes with an average 2k lines of code
  • Making “domain models” that are bound to the table design with no behavior in them only get/set properties
  • Making classes/methods that have so many responsibilities that they hide the implementation
  • Not separating business logic from infrastructure code

And when it’s all in place trying to apply TDD to it.

So it makes me wonder …