it:ad:design:blueprints:n_tier_services

IT:AD:Design:Blueprints:N-Tier Service Development

Summary

Separating a 3 Tier app into a 3 tier app involves

  • Adding more layers of service (namely a ServiceAgent and a ServiceFacade layer)
  • Using WCF

Note that adding Security to the Back Tier – the Application Tier – is not a concern unless the back tier's services are reachable by another means than the Front Tier's Presentation Tier.

The WCF trips up people, needlessly.

  • Advantages:
In the web, adding a middle tier has a very high cost, because the web server is already an app server. It already does database connection pooling across all users on that server. Adding an app server just adds overhead.

>The only exception here is where the web server is serving up a lot of static content and only some dynamic content. In that case, moving the data access to another machine may be beneficial because it can allow the web server to focus more on delivering the static content. It is important to realize that the dynamic content will still be delivered more slowly due to overhead, but the overall web site may work better.

>In Windows, adding a middle tier has a cost because the data needs to make two network hops to get to the user. Each network hop has a tangible cost. It would be foolish to ignore the cost of moving data over the wire, and the cost of serializing/deserializing the data on each end of the wire. These are very real, measurable costs.

>In both cases the middle tier means an extra machine to license, administer, maintain, power and cool. It is an extra point of failure, extra potential for network/CPU/memory/IO contention, etc. These costs come with every server you add into the mix. Anyone who’s ever been a manager or higher in an IT organization has a very good understanding of these costs, because they impact the budget at the end of every year.

>However, the security benefits of a middle tier are **real**.

  • Disadvantages:
    • http://www.lhotka.net/weblog/TheCostsAndSecurityBenefitsOfNtierOver2tier.aspx An important thing to remember when dealing with WCF, you only need to create a ServiceProxy for a Service you don't have access to. If you control both halves of the application – front and back – you are allowed to reuse the Service Contracts, by simply externalise the WCF Service Contracts to a Shared assembly.

O

nce externalised you gain several advantages:

  • Build Servers will instantly update both Solutions (Front and Back) and break if the contact no longer works with one or the other.
  • One can use the technique demonstrated below to debug in either 3 tier mode or 4 tier mode.

If the Service contract is externalised to a shared contract, during debugging, you can dynamically swap between modes at will:


//Retrieve a named binding from Config file
Binding binding = new BasicHttpBinding("BasicHttpBinding_IService1");

//Create a channelFactory for the remote service:
ChannelFactory<IService1> stripperFactory = new ChannelFactory<IService1>("BasicHttpBinding_IService1");

//Depending if running in 3 tier mode or 4 tier mode
use the factory to create a Service Channel:
IService1 service;
if (4Tier){
            service = stripperFactory.CreateChannel();
}else{
           Iservice = SeviceLocator.GetInstance<IService1>;
}}

service.DoSomething();

UserAgentUserAgentAppFacadeAppFacadeAppServiceAppServiceRepositoryRepositoryRetrieveGET Entity 3Get(3)Get(3)return entityreturn entityencode as JSONreturns JSONPost Updateuser edits entity ViewModelPOST Entity 3 + JSON bodyMap JSON to new (incomplete) Entity'Update(Entity entity)Get(3)Map Entity' over EntityUpdateOnCommit(entity)Commit()ACKACK

Using two difference instances of IIS, the above becomes something akin to the following:

UserAgentUserAgentF.AppFacadeF.AppFacadeF.PresServiceF.PresServiceF.SvcAgentF.SvcAgentB.SvcFacadeB.SvcFacadeB.AppServiceB.AppServiceB.RepositoryB.RepositoryRetrieveGET Entity 3Get(3)Get(3)WCF Get(3)Get (3)Get(3)return Entityreturn Entityreturn Entityreturn Entityreturn Entityencode as JSONreturns JSONPost Updateuser edits entity ViewModelPOST Entity 3 + JSON bodyMap JSON to new Entity'Entity' will be 'incomplete'Update(entity)Update(entity)Update(entity)Update(entity)Retrieve (3) as entity'Map entity' over entityOnly entity is in orm contextUpdateOnCommit(entity)Commit()ACKACKACKACKCreate JSON ACKACK

Notice in the above example there were two places the mapping could have been done, but mapping the object in the front tier would have required 3 more serializations (impacting Performance/)

In the above scenario, there is only 2 more serializations than a 3 tier scenario.