Web Application Architectures: Simple 2 Layer, Standard 3 Layer and Distributed 3 Layer
2006-07-19 14:54
465 查看
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).
namespace NorthwindEntities
2<?xml version="1.0"?>
2<configuration>
3 <configSections>
4 <!--Enable this if you want Url rewrite support, you should ensure NBear.Web.dll is refered.-->
5 <!--<section name="UrlRewriteRules" type="NBear.Web.Modules.UrlRewriteRules, NBear.Web"/>-->
6 </configSections>
7 <appSettings>
8 <add key="RootUrl" value="/website/"/>
9 </appSettings>
10 <connectionStrings>
11 <add name="TestAccessDb" connectionString="Provider=Microsoft.Jet.OLEDB.4.0; Data Source=C:\Teddy\NBear\skeleton\Simple\website\App_Data\TestAccessDb.mdb" providerName="NBear.Data.MsAccess.AccessDbProvider"/>
12 <add name="Northwind" connectionString="Server=(local);Database=Northwind;Uid=sa;Pwd=sa" providerName="NBear.Data.SqlServer.SqlDbProvider"/>
13 </connectionStrings>
14 <system.web>
15 <compilation debug="true">
16 <assemblies>
17 <add assembly="System.Runtime.Serialization.Formatters.Soap, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
18 </assemblies>
19 </compilation>
20 <authentication mode="Windows"/>
21 <httpModules>
22 <!--Enable this if you want Url rewrite support, you should ensure NBear.Web.dll is refered.-->
23 <!--<add type="NBear.Web.Modules.UrlRewriteModule, NBear.Web" name="UrlRewriteModule"/>-->
24 </httpModules>
25 </system.web>
26 <!--Enable this if you want Url rewrite support, you should ensure NBear.Web.dll is refered.-->
27 <!--
28 <UrlRewriteRules>
29 <Rule key="^/Category/(.+).html" value="/Category.aspx?ID=$1" />
30 <Rule key="^/(.+).html" value="/$1.aspx" />
31 </UrlRewriteRules>
32 -->
33</configuration>
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.
public abstract class Gateways
5public partial class _Default : System.Web.UI.Page
2namespace ServiceInterfaces
2namespace ServiceImpls
2public abstract class ServiceFactory
2public partial class _Default : System.Web.UI.Page
2<?xml version="1.0"?>
2<configuration>
3 <configSections>
4 <!--Enable this if you want Url rewrite support, you should ensure NBear.Web.dll is refered.-->
5 <!--<section name="UrlRewriteRules" type="NBear.Web.Modules.UrlRewriteRules, NBear.Web"/>-->
6 <section name="castle" type="Castle.Windsor.Configuration.AppDomain.CastleSectionHandler, Castle.Windsor" />
7 </configSections>
8 <castle>
9 <components>
10 <!--You can use standard castle component decleration schema to define service interface impls here-->
11 <component id="sample service" service="ServiceInterfaces.ISampleService, ServiceInterfaces" type="ServiceImpls.SampleServiceImpl, ServiceImpls" />
12 </components>
13 </castle>
14 <appSettings>
15 <add key="RootUrl" value="/website/"/>
16 </appSettings>
17 <connectionStrings>
18 <add name="TestAccessDb" connectionString="Provider=Microsoft.Jet.OLEDB.4.0; Data Source=C:\Teddy\NBear\skeleton\Simple\website\App_Data\TestAccessDb.mdb" providerName="NBear.Data.MsAccess.AccessDbProvider"/>
19 <add name="Northwind" connectionString="Server=(local);Database=Northwind;Uid=sa;Pwd=sa" providerName="NBear.Data.SqlServer.SqlDbProvider"/>
20 </connectionStrings>
21 <system.web>
22 <compilation debug="true">
23 <assemblies>
24 <add assembly="System.Runtime.Serialization.Formatters.Soap, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
25 </assemblies>
26 </compilation>
27 <authentication mode="Windows"/>
28 <httpModules>
29 <!--Enable this if you want Url rewrite support, you should ensure NBear.Web.dll is refered.-->
30 <!--<add type="NBear.Web.Modules.UrlRewriteModule, NBear.Web" name="UrlRewriteModule"/>-->
31 </httpModules>
32 </system.web>
33 <!--Enable this if you want Url rewrite support, you should ensure NBear.Web.dll is refered.-->
34 <!--
35 <UrlRewriteRules>
36 <Rule key="^/Category/(.+).html" value="/Category.aspx?ID=$1" />
37 <Rule key="^/(.+).html" value="/$1.aspx" />
38 </UrlRewriteRules>
39 -->
40</configuration>
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).
namespace ServiceMQServer
2namespace ServiceHost
2<?xml version="1.0" encoding="utf-8" ?>
2<configuration>
3 <configSections>
4 <section name="castle"
5 type="Castle.Windsor.Configuration.AppDomain.CastleSectionHandler, Castle.Windsor" />
6 </configSections>
7 <castle>
8 <components>
9 <!--You can use standard castle component decleration schema to define service interface impls here-->
10 <component id="sample service" service="ServiceInterfaces.ISampleService, ServiceInterfaces" type="ServiceImpls.SampleServiceImpl, ServiceImpls"/>
11 </components>
12 </castle>
13 <connectionStrings>
14 <add name="TestAccessDb" connectionString="Provider=Microsoft.Jet.OLEDB.4.0; Data Source=C:\Teddy\NBear\skeleton\Simple\website\App_Data\TestAccessDb.mdb" providerName="NBear.Data.MsAccess.AccessDbProvider"/>
15 <add name="Northwind" connectionString="Server=(local);Database=Northwind;Uid=sa;Pwd=sa" providerName="NBear.Data.SqlServer.SqlDbProvider"/>
16 </connectionStrings>
17 <appSettings>
18 <add key="Protocol" value="HTTP"/>
19 <add key="ServerAddress" value="127.0.0.1"/>
20 <add key="ServerPort" value="8000"/>
21 </appSettings>
22</configuration>
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).
public abstract class ServiceFactory
2{
3 private ServiceFactory()
4 {
5 }
6
7 //description:
8
9 //You should use standard castle component decleration schema to define service interface impls in Web.config
10 //for more information about castle project and its IoC Configuration,
11 //please visit: http://www.castleproject.org/index.php/IoC:Configuration 12
13 private static NBear.IoC.Service.ServiceFactory factory;
14
15 public static IServiceInterfaceType GetService<IServiceInterfaceType>()
16 where IServiceInterfaceType : IServiceInterface
17 {
18 if (factory == null)
19 {
20 string serverAddress = System.Configuration.ConfigurationManager.AppSettings["ServerAddress"];
21 int serverPort = int.Parse(System.Configuration.ConfigurationManager.AppSettings["ServerPort"]);
22 RemotingChannelType protocol = System.Configuration.ConfigurationManager.AppSettings["ServerAddress"].ToUpper() == ("HTTP").ToUpper() ? RemotingChannelType.HTTP : RemotingChannelType.TCP;
23
24 IServiceMQ mq = new RemotingClientHelper(protocol, serverAddress, serverPort, 0, null).GetClientInstance<IServiceMQ>("MMQ");
25 IServiceContainer container = new SimpleServiceContainer(mq, null);
26
27 factory = new NBear.IoC.Service.ServiceFactory(container);
28 }
29
30 return factory.GetService<IServiceInterfaceType>();
31 }
32}
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.
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).
namespace NorthwindEntities
2<?xml version="1.0"?>
2<configuration>
3 <configSections>
4 <!--Enable this if you want Url rewrite support, you should ensure NBear.Web.dll is refered.-->
5 <!--<section name="UrlRewriteRules" type="NBear.Web.Modules.UrlRewriteRules, NBear.Web"/>-->
6 </configSections>
7 <appSettings>
8 <add key="RootUrl" value="/website/"/>
9 </appSettings>
10 <connectionStrings>
11 <add name="TestAccessDb" connectionString="Provider=Microsoft.Jet.OLEDB.4.0; Data Source=C:\Teddy\NBear\skeleton\Simple\website\App_Data\TestAccessDb.mdb" providerName="NBear.Data.MsAccess.AccessDbProvider"/>
12 <add name="Northwind" connectionString="Server=(local);Database=Northwind;Uid=sa;Pwd=sa" providerName="NBear.Data.SqlServer.SqlDbProvider"/>
13 </connectionStrings>
14 <system.web>
15 <compilation debug="true">
16 <assemblies>
17 <add assembly="System.Runtime.Serialization.Formatters.Soap, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
18 </assemblies>
19 </compilation>
20 <authentication mode="Windows"/>
21 <httpModules>
22 <!--Enable this if you want Url rewrite support, you should ensure NBear.Web.dll is refered.-->
23 <!--<add type="NBear.Web.Modules.UrlRewriteModule, NBear.Web" name="UrlRewriteModule"/>-->
24 </httpModules>
25 </system.web>
26 <!--Enable this if you want Url rewrite support, you should ensure NBear.Web.dll is refered.-->
27 <!--
28 <UrlRewriteRules>
29 <Rule key="^/Category/(.+).html" value="/Category.aspx?ID=$1" />
30 <Rule key="^/(.+).html" value="/$1.aspx" />
31 </UrlRewriteRules>
32 -->
33</configuration>
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.
public abstract class Gateways
5public partial class _Default : System.Web.UI.Page
2namespace ServiceInterfaces
2namespace ServiceImpls
2public abstract class ServiceFactory
2public partial class _Default : System.Web.UI.Page
2<?xml version="1.0"?>
2<configuration>
3 <configSections>
4 <!--Enable this if you want Url rewrite support, you should ensure NBear.Web.dll is refered.-->
5 <!--<section name="UrlRewriteRules" type="NBear.Web.Modules.UrlRewriteRules, NBear.Web"/>-->
6 <section name="castle" type="Castle.Windsor.Configuration.AppDomain.CastleSectionHandler, Castle.Windsor" />
7 </configSections>
8 <castle>
9 <components>
10 <!--You can use standard castle component decleration schema to define service interface impls here-->
11 <component id="sample service" service="ServiceInterfaces.ISampleService, ServiceInterfaces" type="ServiceImpls.SampleServiceImpl, ServiceImpls" />
12 </components>
13 </castle>
14 <appSettings>
15 <add key="RootUrl" value="/website/"/>
16 </appSettings>
17 <connectionStrings>
18 <add name="TestAccessDb" connectionString="Provider=Microsoft.Jet.OLEDB.4.0; Data Source=C:\Teddy\NBear\skeleton\Simple\website\App_Data\TestAccessDb.mdb" providerName="NBear.Data.MsAccess.AccessDbProvider"/>
19 <add name="Northwind" connectionString="Server=(local);Database=Northwind;Uid=sa;Pwd=sa" providerName="NBear.Data.SqlServer.SqlDbProvider"/>
20 </connectionStrings>
21 <system.web>
22 <compilation debug="true">
23 <assemblies>
24 <add assembly="System.Runtime.Serialization.Formatters.Soap, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
25 </assemblies>
26 </compilation>
27 <authentication mode="Windows"/>
28 <httpModules>
29 <!--Enable this if you want Url rewrite support, you should ensure NBear.Web.dll is refered.-->
30 <!--<add type="NBear.Web.Modules.UrlRewriteModule, NBear.Web" name="UrlRewriteModule"/>-->
31 </httpModules>
32 </system.web>
33 <!--Enable this if you want Url rewrite support, you should ensure NBear.Web.dll is refered.-->
34 <!--
35 <UrlRewriteRules>
36 <Rule key="^/Category/(.+).html" value="/Category.aspx?ID=$1" />
37 <Rule key="^/(.+).html" value="/$1.aspx" />
38 </UrlRewriteRules>
39 -->
40</configuration>
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).
namespace ServiceMQServer
2namespace ServiceHost
2<?xml version="1.0" encoding="utf-8" ?>
2<configuration>
3 <configSections>
4 <section name="castle"
5 type="Castle.Windsor.Configuration.AppDomain.CastleSectionHandler, Castle.Windsor" />
6 </configSections>
7 <castle>
8 <components>
9 <!--You can use standard castle component decleration schema to define service interface impls here-->
10 <component id="sample service" service="ServiceInterfaces.ISampleService, ServiceInterfaces" type="ServiceImpls.SampleServiceImpl, ServiceImpls"/>
11 </components>
12 </castle>
13 <connectionStrings>
14 <add name="TestAccessDb" connectionString="Provider=Microsoft.Jet.OLEDB.4.0; Data Source=C:\Teddy\NBear\skeleton\Simple\website\App_Data\TestAccessDb.mdb" providerName="NBear.Data.MsAccess.AccessDbProvider"/>
15 <add name="Northwind" connectionString="Server=(local);Database=Northwind;Uid=sa;Pwd=sa" providerName="NBear.Data.SqlServer.SqlDbProvider"/>
16 </connectionStrings>
17 <appSettings>
18 <add key="Protocol" value="HTTP"/>
19 <add key="ServerAddress" value="127.0.0.1"/>
20 <add key="ServerPort" value="8000"/>
21 </appSettings>
22</configuration>
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).
public abstract class ServiceFactory
2{
3 private ServiceFactory()
4 {
5 }
6
7 //description:
8
9 //You should use standard castle component decleration schema to define service interface impls in Web.config
10 //for more information about castle project and its IoC Configuration,
11 //please visit: http://www.castleproject.org/index.php/IoC:Configuration 12
13 private static NBear.IoC.Service.ServiceFactory factory;
14
15 public static IServiceInterfaceType GetService<IServiceInterfaceType>()
16 where IServiceInterfaceType : IServiceInterface
17 {
18 if (factory == null)
19 {
20 string serverAddress = System.Configuration.ConfigurationManager.AppSettings["ServerAddress"];
21 int serverPort = int.Parse(System.Configuration.ConfigurationManager.AppSettings["ServerPort"]);
22 RemotingChannelType protocol = System.Configuration.ConfigurationManager.AppSettings["ServerAddress"].ToUpper() == ("HTTP").ToUpper() ? RemotingChannelType.HTTP : RemotingChannelType.TCP;
23
24 IServiceMQ mq = new RemotingClientHelper(protocol, serverAddress, serverPort, 0, null).GetClientInstance<IServiceMQ>("MMQ");
25 IServiceContainer container = new SimpleServiceContainer(mq, null);
26
27 factory = new NBear.IoC.Service.ServiceFactory(container);
28 }
29
30 return factory.GetService<IServiceInterfaceType>();
31 }
32}
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.
相关文章推荐
- Web Application Architectures: Simple 2 Layer, Standard 3 Layer and Distributed 3 Layer
- Creating a simple Web application and deploying it to Tomcat
- applicationcontext and webapplicationcontext
- 5 advice for developing RIA and WEB application in Flex
- Web Application UI(一):Web List Component: Beyond Simple HTML Table
- Q and A: J2EE 1.4: The Gold Standard for Web Services
- MS Web Services and other distributed technoleges site
- Jersey(1.19.1) - WebApplicationException and Mapping Exceptions to Responses
- Agile Web Application Development with Yii 1.1 and PHP5 读后总结(5)Task的所有...
- Notes for "Next.Generation.Application.Integration.From.Simple.Information.To.Web.Services"
- How to Configure Selenium Webdriver in Eclipse and execute some simple test script.
- Simple Authentication and Security Layer 简单的认证及加密层
- Application Layer ISO OSI Functionality and Protocols
- Web Application Architectures @Coursera 学习笔记(一)
- 可扩展的网络架构和分布式系统 --Scalable Web Architecture and Distributed Systems
- A Book Store Application Using AngularJS and Asp.Net Web Api
- [ZT]Web Standard and ASP.NET – Part1 XHTML Quick Start
- A simple application using I/O Completion Ports and WinSock Swarajya Pendharkar, 30 Mar 2007
- Windows Azure Web Sites: How Application Strings and Connection Strings Work
- 可扩展Web架构与分布式系统 英文原文:Scalable Web Architecture and Distributed Systems