Web Application Architectures: Simple 2 Layer, Standard 3 Layer and Distributed 3 Layer
Abstract:
This article discusses three common web application architectures: Simple 2 Layer, Standard 3 Layer and Distributed 3 Layer. For each architecture, it gives an ASP.NET 2.0 sample implementaion with NBear framework.
Table of Contents:
1. Simple 2 Layer
2. Standard 3 Layer
3. Distributed 3 Layer
4. More about NBear
5. Get Source Code of NBear and All Samples
6. Summary
1. Simple 2 Layer
This is the simplest web application architecture (See Figure 1.1).
Figure 1.1
The presentaion layer directly calls the data access layer to read/write data in database.
A classic example of this architecture is used by most ASP applications, people use Transaction Scripts read/write data in database through ADO. In the ASP case, ASP is the Web Presentaion Layer, and ADO is the Data Access Layer.
Sample 1
To apply this architecture with NBear is super easy. Set connection strings in Web.config; create your website project and reference to the NBear framework .DLLs; use EntityGen tool (provided by NBear) to generate entities automatically; and then you can read/write data in databases for free. Here NBear.Data works as the Data Access Layer, which makes data accessing much easier than using ADO.NET directly. The implementation architecture is like figure 1.2.
Figure 1.2
Entities Definition are interface style entities code generated by NBear.Tools.EntityGen.exe. (You can manually write them or modify them based on the gerated code, as well.) Code 1.1 is some sample entities code of the simple sample:
Notice, each entity is an interface inherited from NBear.Common.IEntity. An entity maps to a database table or view. Each property of entity maps to a database table column. Data column's read/write abilities are reflected by properties' get and set. You can add .Net Attributes (TableAttribute, ColumnNameAttribute and PrimaryKeyAttribute) to entities to change the default mapping. By default, the names of entity/table, property/column are same, you can change them taking advantage of these Attributes.
Connection strings can be set like Code 1.2 in Web.config (for windows form application, of couse you should set them in App.config).
Ensuring your ASP.NET 2.0 website project referencing to NBear.Common, NBear.Data, and you can freely read/write data in database using NBear.Data.Gateway now. For easier to call gateways, in this sample we define a Gateways class in App_Code so that we can call gateways in it freely without adding "using NBear.Data" statement in each .cs files you want to call it. Code 1.3 is the Gateways.cs and Code 1.4 shows how to call gateway and read/write data in database. All return results may be strong type Enitity (Array). And you can apply transactions to queries through NBear.Data.Gateway.BeginTransaction() or you can use the new .Net 2.0 classes in System.Tansactions namespaces to realize transactions, too.
Is it easy enough as you can imagine? :)
2. Standard 3 Layer
The Standard 3 Layer architecture is the most common web application architecture. Figure 2.1 shows its structure.
Figure 2.1
The main difference from the Simple 2 Layer architecture is a new Domain/Service Layer is added. This layer includes resuable business domain logic and services. Separating this layer makes our code more reusable and easier to test. With Unit Testing tools, We can easily test code in this layer independently and automatically. When applying this architecture, for easier to manage/test code in this layer and to decouple the web presentaion layer from this layer, it is suggested to design services with interfaces. The following sample will show you how to do this and what's the benefits.
Sample 2
Let's see the implementaion architecture (see Figure 2.2) and compared with Sample 2.
Figure 2.2
Comparing it with sample 1, we can see the differences are 3 new components are added. Service Interfaces Definition is what I mentioned above that is the interfaces of services you want to provide. Services Implementaion of course are the code of implementing the service interfaces.
Which really interesting is the Service Factory. Service Factory is a factory used to produce services, you can pass the service interface to it and it returns you service interface implementaion. The Service Factory is provided by NBear.IoC component, it is designed based on a service container (taking advantage of the Castle IoC container). As you can see in Figure 2.2, the web application layer only depends on the Service Factory, Service Interfaces Definition and the Entities Definition, which means you can use inversion of control to configurate service implementaions in configuration file (Web.config/App.config), and Service Factory will discover and load these implementaions at runtime and return to web presentaion layer. This kind of design decouples the web application layer from the service implementaions so that in team development, web presentaion layer development/testing and service development/testing can be synchronized and independent, which really saves costs.
Code 2.1 is a sample service interface, and Code 2.2 is the implementaion of the service interfaces. The Service Factory is initiated in ServiceFactory.cs (see Code 2.3) file under App_Code folder of website. Notice, it calls components in NBear.IoC. And in Default.aspx.cs (see Code 2.4), this is the same logic as sample 1, this time we uses service factory to create service and call services rather than reading data from database directly like code in sample 1. To be discovered by service container, service implementaions must be configurated in Web.config like Code 2.5.
3. Distributed 3 Layer
Now we come to the distributed one. Do not breathe too heavy when seeing the word distributed. It doesn't mean complexity and hard to understand all the time, no mather in this article or in other system. Distributed doesn't means different business logic. The only difference that distributed application architecture has from non-distributed architectures is it needs some gumwater to let service implementaions deployed in different servers to adhere together. So, what I want to say is, if we have a bottle of nice (correct and suitable is enough, do not believe advertisements until you have tried by your hand) gumwater, distributed things will be as easy as non-distributed ones. I'll show you in the later distributed sample 3 in this article.
First let's begin with the common web distributed application architecture(see Figure 3.1).
Figure 3.1
Notice, difference is at the domain/service layer. Service implementaions now not only deployed on a single server. They are deployed on several servers. So we need a service factory to manage these service implementaions for us. The service factory is resposible for locating the service implementaions and decoupling the caller of service factory from service implementaions. See it is really similar to the service factory we created for only local services in sample 2. Can the service factory/service container provided by NBear.IoC deal with distributed service implementaions rather than local service implementaions only? You can guess the result -- Yes, of course. Even not only there is no need to change any web presentaion layer code where calling services, nor there is no need to change any service implementaions code. If you are using NBear, you can easily instance a service MQ server (build-in), split service implementaions to several hosts which can be deployed on different servers. With little little effort, your application become distributed. Let's see the implementaion architecture in Figure 3.2.
Figure 3.2
See, comparing to sample 2's architecture, the only difference is now service implemtaions not only deployed on a single server. How much modification of code need to make?
First, we need a service MQ Server. A service MQ is used to manage all the service requests and responses. There is a MemoryServiceMQ defined in NBear.IoC, if you want, you can implement your Service MQ (implement IServiceMQ interface) to using MSMQ, ActiveMQ or any commercial MQs yourself and substitute it for the build-in lightweight MemoryServiceMQ. Although MemoryServiceMQ is correct and suitable enough for most system I think. Code 3.1 is a simple service MQ server based on .Net remoting. (Now NBear only contains the .Net remoting broker, in the futher, it will provides more brokers. In fact it is really not so hard to implement brokers such as standard socket, web service or XML-RPC. I will talk about them in futher articles.)
In the code, we can see, it easily instanced a MemoryServiceMQ object and publish it to HTTP with specified address and port taking advantage of .Net Remoting support. NBear.Net.Remoting contains some helper classes to make calling .net remoting components easier, see RemotingServiceHelper class which is used in this clip of code. That's all for the service MQ server.
Next, we move the service implementaions configuration from Web.config to some service hosts. Code 3.2 is a simple service host whose only resposibility is register service implementaions to service MQ server. The same service implementaions configuration now are in the host application's App.config (see Code 3.3).
There is only a final place need to modify, that is the ServiceFactory.cs of website, we should modify it to discover service from service MQ server. The interfaces for service caller is exact same, what we need to do is to get the Service MQ published by service MQ server through .Net Remoting channel just like what we do in Service Host application (see Code 3.4).
Well, no other modification is needed. If you visit the Default.aspx of the website, you can see it runs exactly the same as Sample 2 and Sample 1. Let's go through the developers of this distributed's work when developing this application again to see is there any effort compared with sample2. Designing website is same, generating entities is some, defining service interfaces is same, implementing services is same... See, except very little modification for publishing and accessing the remote service MQ instead of the local one in sample2, everything is completely same. Is distributed application easy? Yes it is the gumwater's contribution, right? :)
4. More about NBear
Following is the official description of NBear:
A rapid/High Performance/Distributed application development framework based on .NET 2.0.
V2 of NBear is a big refactor and enhancement from V1. V2 reserves the strenths of V1 and
brings new flexible architecture. V2 focuses on not only RAD but also high performance and
scalability to make people's development life easier. V2 try hard to lead a nice development
process. That is to rapidly (Configless) develop application core functions and to append
performance ehancement/adjustment through additional configuration and build-in helper tools
later.
License: BSD
Copyright: 2005-2006 Ilungasoft.com/teddy.cn
Contact: Teddy (shijie.ma(at)gmail.com)
Blog: http://sf.net/projects/nbear
5. Get Source Code of NBear and All Samples
You can download the latest version of NBear from http://sf.net/projects/nbear, when writing this article, the latest version of NBear is V2.0.0. The downloaded zip file contains all the source code of NBear components and all the source code of sample 1-3 discussed in this article. These samples are located at the skeleton folder. I suggest you reference or directly begin from a copy of one of this three skeleton sample for your own development based on NBear.
6. Summary
In this article, we discussed 3 common web application architectures and sample implementaions for them based on NBear framework. Although application architectures discussed are all about web application, it should be similar for other type of applications, such as windows form application. Rename "web presentaion" to "winform presentaion" and Web.config to App.config, then most things are same enough.
Thanks for read from top to this bottom line. With best wishes.