电子商务之集成选项分析(四)

本文探讨了电子商务系统中集成选项的重要性,通过增加Shop.Operaional类库来提高系统的可伸缩性、维护性和代码重用能力。文章详细介绍了如何实现电子邮件管理、处理异常、计算销售税,并提供了关键代码片段及配置示例。

电子商务之集成选项分析(四)

集成选项对应的是Shop.Operaional类库,增加这个类库将增强系统的可伸缩性,简化调试,改进课维护性并且提供在其他项目中重用代码的能力。

首先实现电子邮件管理程序


usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Net.Mail;
usingSystem.Configuration;

namespaceShop.Operational
{
///<summary>
///发送邮件处理类
///</summary>
publicclassEmailManager
{
publicEmailManager()
{

}

publicboolIsSent{set;get;}

//读取存储在web.config文件中的数据
privatestringSMTPServerName
{
get{returnConfigurationManager.AppSettings["SMTPServer"];}
}

privatestringToAddress
{
get{returnConfigurationManager.AppSettings["ToAddress"];}
}

///<summary>
///发送方法
///</summary>
///<paramname="emailcontents">邮件内容结构体</param>
publicvoidSend(EmailContentsemailcontents)
{
//发送电子邮件类
SmtpClientclient=newSmtpClient(this.SMTPServerName);

client.UseDefaultCredentials
=true;

//发件人地址和姓名
MailAddressfrom=newMailAddress(emailcontents.FromEmailAddress,emailcontents.FromName);

//收件人的地址
MailAddressto=newMailAddress(this.ToAddress);

MailMessagemessage
=newMailMessage(from,to);
message.Subject
=emailcontents.Subject;
message.Body
=Utilities.FormatText(emailcontents.Body,true);
message.IsBodyHtml
=true;

try
{
//发送邮件
client.Send(message);
IsSent
=true;
}
catch(Exceptionex)
{
throwex;
}
}
}

///<summary>
///邮件内容结构体
///</summary>
publicstructEmailContents
{
publicstringTo;
publicstringFromName;
publicstringFromEmailAddress;
publicstringSubject;
publicstringBody;
}
}

这里只是提醒一下SmtpClient和MailAddress是在System.Net.Mail命名空间下面。SMTPServerName就是服务器名称,ToAddress就是要发送到邮件的地址,他们在web.config文件的存储形式如下:

< appSettings >
< add key ="SMTPServerName" value ="localhost" />
< add key ="ToAddress" value ="couhujia@sina.com" />
</ appSettings >

实现游客发送邮件的话就构造EmailManager类型对象的一个实例,然后赋值给这个对象的属性,在调用send发送方法就ok了。在注意一下Body属性使用了Utilities的类和名为FormatText静态方法。


usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
usingSystem.IO;
usingSystem.Web;

namespaceShop.Operational
{
publicclassUtilities
{
publicstaticstringFormatText(stringtext,boolallow)
{
stringformatted="";

StringBuildersb
=newStringBuilder(text);
sb.Replace(
"","&nbsp;");

if(!allow)
{
sb.Replace(
"<br>",Environment.NewLine);
sb.Replace(
"&nbsp;","");
formatted
=sb.ToString();
}
else
{
StringReadersr
=newStringReader(sb.ToString());
StringWritersw
=newStringWriter();

//能否读取下一个字符
while(sr.Peek()>-1)
{
stringtemp=sr.ReadLine();
sw.Write(temp
+"<br>");
}

formatted
=sw.GetStringBuilder().ToString();
}

returnformatted;
}
}
}

因为假设这里不考虑用组件而是文本框,那么输入的字符就需要将其转化为HTML格式正确的表示。allow设置为true时就是将输入到文本框里的字符串格式转化成了HTML格式。allow设置为false时就是将HTML格式转化为字符串格式.

这里想说明一下System.Environment类型定义了一个只读的NewLine属性。如果程序在Windows上运行,该属性能返回一个由这些字符构成的字符串。NewLine属性是依赖与平台的,它会根据底层平台来返回恰当的字符串。例如,将公共语言基础结构(CLI)移植到一个UNIX系统,NewLine属性会返回一只包含字符\n的字符串。所以就建议以后换行字符串用System.Environment.NewLine

使程序能够执行一些异常处理

首先这方面是我以前不太注意的地方,但是渐渐的发现这块功能其实很重要。因为无论什么程序,可能都会出现一些我们无法预料的错误或异常。我们就要使程序能够处理这些错误或异常

我们首先记录这些错误或异常记录到/ExceptionLog/LogFile.txt文件中,其实这里大家可以自己动手改改当发生异常或错误时候用邮件通知开发者。


<%@ApplicationLanguage="C#"%>
<%@ImportNamespace="Shop.Operational"%>

<scriptrunat="server">

voidApplication_Start(objectsender,EventArgse)
{
//Codethatrunsonapplicationstartup

}

voidApplication_End(objectsender,EventArgse)
{
//Codethatrunsonapplicationshutdown

}

voidApplication_Error(objectsender,EventArgse)
{
//Codethatrunswhenanunhandlederroroccurs
Exceptionex=Server.GetLastError();
Utilities.LogException(ex);
}

voidSession_Start(objectsender,EventArgse)
{
//Codethatrunswhenanewsessionisstarted

}

voidSession_End(objectsender,EventArgse)
{
//Codethatrunswhenasessionends.
//Note:TheSession_Endeventisraisedonlywhenthesessionstatemode
//issettoInProcintheWeb.configfile.IfsessionmodeissettoStateServer
//orSQLServer,theeventisnotraised.

}

</script>

注意到调用了了Shop.Operational命名空间下的Utilities类的LogExcepion()方法


usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
usingSystem.IO;
usingSystem.Web;

namespaceShop.Operational
{
publicclassUtilities
{
publicstaticstringFormatText(stringtext,boolallow)
{
stringformatted="";

StringBuildersb
=newStringBuilder(text);
sb.Replace(
"","&nbsp;");

if(!allow)
{
sb.Replace(
"<br>",Environment.NewLine);
sb.Replace(
"&nbsp;","");
formatted
=sb.ToString();
}
else
{
StringReadersr
=newStringReader(sb.ToString());
StringWritersw
=newStringWriter();

//能否读取下一个字符
while(sr.Peek()>-1)
{
stringtemp=sr.ReadLine();
sw.Write(temp
+"<br>");
}

formatted
=sw.GetStringBuilder().ToString();
}

returnformatted;
}


publicstaticvoidLogException(Exceptionex)
{
stringpath=HttpContext.Current.Server.MapPath(@"ExceptionLog\LogFile.txt");
using(StreamWritersw=newStreamWriter(path,true))
{
sw.WriteLine(DateTime.Now
+Environment.NewLine+ex.InnerException.ToString()+Environment.NewLine+Environment.NewLine);
}
}
}
}

这里有个一个地方不太明白怎么用using(....),不知一般用于引用命名空间吗?希望明白的朋友给我讲解一下

但是这只是给我们开发人员提供了通知,我们还要给游客提供通知说明程序出现错误

首先设置Web.config文件(当然你也可以为单独一张页面出现错误制作一个错误提示,那就要在Page属性里定义错误页面了,我这是所有的页面出现错误都跳转ErrorPage.aspx页面)

< customErrors mode ="On" defaultRedirect ="ErrorPage.aspx" >
</ customErrors >

可以在ErrorPage.aspx页面说一些”非常对不起对您造成了不便,但是网站遇到了错误,技术支持小组正在修复当中"等一些话语

另外大家也知道国外买商品,需要自己另外付购物的税钱,就是购物的总价钱*税率

复制代码

usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Configuration;

namespaceShop.Operational
{
publicclassCalculationManager
{
publicstaticdecimalCalcSalesTax(decimalamount)
{
decimaltotal;

decimaltaxrate=(Convert.ToDecimal(ConfigurationManager.AppSettings["TaxRate"])/100);
total
=(amount*taxrate);

returntotal;
}
}
}
< appSettings >
< add key ="TaxRate" value ="7" />
< add key ="SMTPServerName" value ="localhost" />
< add key ="ToAddress" value ="couhujia@sina.com" />
</ appSettings >
复制代码

总之可以看见在这个类库里的代码都是一些日常常用的类。

大家可以谈谈自己在这方面的处理,让我们分享一下您的编程设计和思想。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值