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

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

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

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


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using System.Configuration;

namespace Shop.Operational
{
    
/// <summary>
    
/// 发送邮件处理类
    
/// </summary>
    public class EmailManager
    {
        
public EmailManager()
        {
 
        }

        
public bool IsSent { setget; }

        
//读取存储在web.config文件中的数据
        private string SMTPServerName
        {
            
get { return ConfigurationManager.AppSettings["SMTPServer"]; }
        }

        
private string ToAddress
        {
            
get { return ConfigurationManager.AppSettings["ToAddress"]; }
        }

        
/// <summary>
        
/// 发送方法
        
/// </summary>
        
/// <param name="emailcontents">邮件内容结构体</param>
        public void Send(EmailContents emailcontents)
        {
            
//发送电子邮件类
            SmtpClient client = new SmtpClient(this.SMTPServerName);

            client.UseDefaultCredentials 
= true;

            
//发件人地址和姓名
            MailAddress from = new MailAddress(emailcontents.FromEmailAddress, emailcontents.FromName);

            
//收件人的地址
            MailAddress to = new MailAddress(this.ToAddress);

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

            
try
            {
                
//发送邮件
                client.Send(message);
                IsSent 
= true;
            }
            
catch (Exception ex)
            {
                
throw ex;
            }
        }
    }

    
/// <summary>
    
/// 邮件内容结构体
    
/// </summary>
    public struct EmailContents
    {
        
public string To;
        
public string FromName;
        
public string FromEmailAddress;
        
public string Subject;
        
public string Body;
    }
}

  这里只是提醒一下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静态方法。


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Web;

namespace Shop.Operational
{
    
public class Utilities
    {
        
public static string FormatText(string text, bool allow)
        {
            
string formatted = "";

            StringBuilder sb 
= new StringBuilder(text);
            sb.Replace(
"  "" &nbsp;");

            
if (!allow)
            {
                sb.Replace(
"<br>", Environment.NewLine);
                sb.Replace(
"&nbsp;"" ");
                formatted 
= sb.ToString();
            }
            
else
            {
                StringReader sr 
= new StringReader(sb.ToString());
                StringWriter sw 
= new StringWriter();

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

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

            
return formatted;
        }
    }
}

 

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

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

 

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

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

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


<%@ Application Language="C#" %>
<%@ Import Namespace="Shop.Operational" %>

<script runat="server">

    
void Application_Start(object sender, EventArgs e) 
    {
        
// Code that runs on application startup

    }
    
    
void Application_End(object sender, EventArgs e) 
    {
        
//  Code that runs on application shutdown

    }
        
    
void Application_Error(object sender, EventArgs e) 
    { 
        
// Code that runs when an unhandled error occurs
        Exception ex = Server.GetLastError();
        Utilities.LogException(ex);
    }

    
void Session_Start(object sender, EventArgs e) 
    {
        
// Code that runs when a new session is started

    }

    
void Session_End(object sender, EventArgs e) 
    {
        
// Code that runs when a session ends. 
        
// Note: The Session_End event is raised only when the sessionstate mode
        
// is set to InProc in the Web.config file. If session mode is set to StateServer 
        
// or SQLServer, the event is not raised.

    }
       
</script>

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


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Web;

namespace Shop.Operational
{
    
public class Utilities
    {
        
public static string FormatText(string text, bool allow)
        {
            
string formatted = "";

            StringBuilder sb 
= new StringBuilder(text);
            sb.Replace(
"  "" &nbsp;");

            
if (!allow)
            {
                sb.Replace(
"<br>", Environment.NewLine);
                sb.Replace(
"&nbsp;"" ");
                formatted 
= sb.ToString();
            }
            
else
            {
                StringReader sr 
= new StringReader(sb.ToString());
                StringWriter sw 
= new StringWriter();

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

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

            
return formatted;
        }


        
public static void LogException(Exception ex)
        {
            
string path = HttpContext.Current.Server.MapPath(@"ExceptionLog\LogFile.txt");
            
using (StreamWriter sw = new StreamWriter(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页面说一些 ”非常对不起对您造成了不便,但是网站遇到了错误,技术支持小组正在修复当中"等一些话语

 

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

复制代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;

namespace Shop.Operational
{
    
public class CalculationManager
    {
        
public static decimal CalcSalesTax(decimal amount)
        {
            
decimal total;

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

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

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

 

大家可以谈谈自己在这方面的处理,让我们分享一下您的编程设计和思想。

同系列文章

参考资料

            电商架构分析

  • 9
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值