編程代碼大全,編寫一份代碼,支持多種布署方式

 2023-10-21 阅读 23 评论 0

摘要:對于一個CS程序, 可以直接連接數據庫(方式一); 為了安全起見,可能會通過web service來獲取數據(方式二),此時客戶端程序在一臺機器上, Web Service在另外一臺機器上。? 編程代碼大全、? 兩者在代碼實現上也是不一樣的

對于一個CS程序, 可以直接連接數據庫(方式一);

為了安全起見,可能會通過web service來獲取數據(方式二),此時客戶端程序在一臺機器上, Web Service在另外一臺機器上。?

編程代碼大全、?

兩者在代碼實現上也是不一樣的 。

方式一:只需要實現核心代碼,然后客戶端直接引用就可以了

編寫的代碼怎么運行。方式二:除了實現核心代碼外,web服務器上需要添加相應的web service, 同時客戶端需要引用相應的web service, 客戶端使用生成的代理去發送請求。?

現在使用Spring.net后, 可以按方式一來編寫代碼, 但是部署時卻可以支持兩種布署方式。

現在使用為什么要使用AOP?中的例子來說明這個功能。

如何讓代碼運行、1.在IAccount類中加入一個新的接口DataSource, 用來表示數據的來源,其它不變。

?? ?public?interface?IAccount
????{
????????
string?DataSource?{?get;?}

?? ?}?

文號的編寫方式??

2.?Account類實現此接口

?

?? ?public?class?Account?:?Business.IAccount
????{
????????
public?string?DataSource?{?get;?private?set;?}?? ? ? ?
????}

?

3.客戶端實現代碼

static?void?Main(string[]?args)
????????{
????????????IApplicationContext?ctx?
=?ContextRegistry.GetContext();
????????????IAccount?account?
=?(IAccount)ctx.GetObject("MyAccount");

?? ? ? ? ? ?Console.WriteLine(account.DataSource);

?? ? ? ? ? ?Console.ReadKey();

?? ? ? }?

?下面就來通過配置來支持上面兩種布署方式。

App.config的配置如下:?

<configuration>
?? ?
<configSections>
????????
<sectionGroup?name="spring">
????????????
<section?name="context"?type="Spring.Context.Support.ContextHandler,?Spring.Core"/>
????????????
<section?name="objects"?type="Spring.Context.Support.DefaultSectionHandler,?Spring.Core"/>
????????
</sectionGroup>
????
</configSections>
????
<spring>
????????
<context>
????????????
<!--User?Local?-->
????????????
<resource?uri="file://config/Local.config"?/>

????????????
<!--User?Web?Service?-->
????????????
<!--<resource?uri="file://config/WebService.config"?/>-->
????????
</context>
????
</spring>
</configuration>

?方式一的配置如上面, 如果要用方式二, 則注釋掉方式一的配置,啟用方式二的配置。

?Local.config的配置如下:

<objects?xmlns="http://www.springframework.net">
????
<object?id="ConsoleLogAspect"?type="Spring.Aop.Support.NameMatchMethodPointcutAdvisor,?Spring.Aop">
????????
<property?name="Advice">
????????????
<object?type="Aspects.ConsoleLogAspect,?Aspects"?/>
????????
</property>
????????
<property?name="MappedNames">
????????????
<list>
????????????????
<value>Deposit</value>
????????????????
<value>Withdraw</value>
????????????
</list>
????????
</property>
????
</object>

????
<object?id="MyAccount"?type="Spring.Aop.Framework.ProxyFactoryObject">
????????
<property?name="Target">
????????????
<object?type="Business.Account,?Business"?>
????????????????
<property?name="DataSource"?value="Data?Source?from?Local"></property>
????????????
</object>
????????
</property>
????????
<property?name="InterceptorNames">
????????????
<list>
????????????????
<value>ConsoleLogAspect</value>
????????????
</list>
????????
</property>
????
</object>
</objects>?

Local.config中,調用方法時會輸入日志。

WebService.config?中的配置如下:?

<objects?xmlns="http://www.springframework.net">
????
<object?id="MyAccount"?type="Spring.Web.Services.WebServiceProxyFactory,?Spring.Services">
????????
<property?name="ServiceUri"?value="http://localhost:1376/AopWeb/AccountService.asmx"/>
????????
<property?name="ServiceInterface"?value="Business.IAccount,?Business"/>
????????
<property?name="ProductTemplate">
????????????
<object>
????????????????
<property?name="Timeout"?value="10000"/>
????????????????
<!--10s-->
????????????
</object>
????????
</property>
????
</object>
</objects>

?ServiceUri定義了請求的web service url

?4.為了支持方式二布署, 我們需要一個web site來發布web service。新建一個web site項目,只需要引用相應的dll, 然后配置一下就OK了, 為了簡單,配置直接寫到web.config中(配置可以寫到單獨的文件中的),web.config中的配置如下:?

<configuration>
????
<configSections>
????????
<sectionGroup?name="spring">
????????????
<section?name="context"?type="Spring.Context.Support.WebContextHandler,?Spring.Web"/>
????????????
<section?name="objects"?type="Spring.Context.Support.DefaultSectionHandler,?Spring.Core"?/>
????????
</sectionGroup>
????
</configSections>

????
<spring>
????????
<context>
????????????
<resource?uri="config://spring/objects"/>????????????
????????
</context>

????????
<objects?xmlns="http://www.springframework.net">
????????????
<object?id="MyAccount"?type="Business.Account,?Business">
????????????????
<property?name="DataSource"?value="Data?Source?from?Remote?Web?Service"></property>????????????????
????????????
</object>

????????????
<object?id="AccountService"?type="Spring.Web.Services.WebServiceExporter,?Spring.Web">
????????????????
<property?name="TargetName"?value="MyAccount"?/>
????????????????
<property?name="Namespace"?value="http://AccountService/WebServices"?/>
????????????
</object>???????????
????????
</objects>
????
</spring>

????
????
<system.web>
????????
<compilation?debug="false"></compilation>
????????
<authentication?mode="Windows"/>

????????
<httpHandlers>
????????????
<add?verb="*"?path="*.asmx"?type="Spring.Web.Services.WebServiceHandlerFactory,?Spring.Web"/>
????????
</httpHandlers>
????????
<httpModules>
????????????
<add?name="Spring"?type="Spring.Context.Support.WebSupportModule,?Spring.Web"/>
????????
</httpModules>
????
</system.web>
????
</configuration>

?現在不用添加AccountService.asmx文件,就可以訪問http://localhost:1376/WebSite/AccountService.asmx web服務了。

到這里所有的工作都做完了, 下面來看一下運行的效果。

方式一運行時:


運行時會有相應的日志輸出, 這里顯示的是加載本地數據

方式二運行時:

?

?運行時無日志輸出, 這里顯示的是從Web服務加載數據

?

結論:通過引用AOP框架, 可以在布署時決定布署方式, 只需要修改配置文件就可以實現不同的布署。

雖然CSLA.Net 也有相似的功能, 但是和Spring.net相比, CSLA.net對類具有侵入性,需要業務類繼承它的泛型基類, 但Spring.net沒有這方面的要求,只需要實現自己的接口就可以了,基于接口編程在這時就顯得特別重要了。

一直以來,業務類都沒有采用接口的方式編寫, 因為覺得業務類是不會替換的, 但業務類面向接口編程在這里顯示出了優勢, 只要實現了接口, 就可以直接配置成web服務。?

示例文件下載:AOPSample1.1 ?

?

轉載于:https://www.cnblogs.com/zq8024/archive/2011/04/02/2004087.html

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/1/154295.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息