一、Unity 中的拦截功能
当我们想保持原先的程序不动,但又要扩展一些业务的时候(也就是所谓的开放-封闭原则),我们应该考虑一种叫Unity 中的拦截功能, 若要变得可拦截,类必须实现接口或者继承自 MarshalByRefObject。 那么实现了ICallHandler接口的类才能够拦截所需的对象或者是所需属性等等,具体可以参考一下连接中的内容:http://msdn.microsoft.com/zh-cn/magazine/gg535676.aspx。
以下是一个简单的例子(仅供参考)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QH.CRM.BLL;
using QH.CRM.Common;
using QH.CRM.Model;
using QH.CRM.BLL.Log;
namespace QH.CRM.BLL.Trade
{
public class TradeService :MarshalByRefObject
{
public ILogger<LogEntry> Logger { get; set; }
#region 定单状态更新请求
///<summary>
/// 更新定单状态
///</summary>
///<returns></returns>
public UpdateOrderStatusResponse UpdateOrderStatus(UpdateOrderStatusRequest request)
{
UpdateOrderStatusResponse response = new UpdateOrderStatusResponse() { IsSuccess = true };
TradeRecordManager manager =OB.R<TradeRecordManager>();
var model = manager.GetModel("orderid=" + request.OrderId + "","OrderId DESC");
if (model == null)
{
Logger.WriteLog(new LogEntry { Message = "未找到对应定单记录,OrderId:" + request.OrderId, Priority = 7 , Severity=System.Diagnostics.TraceEventType.Warning });
response.IsSuccess = false;
return response;
}
#region 更新定单状态
model.LastEditTime = DateTime.Now;
//model.Status = request.Status;
//model.MoneyAmount = request.Money;
//response.TradeSuccess = true;
if (request.Status == 1) //已付款
{
model.Status = 1;
model.MoneyAmount = request.Money;
response.TradeSuccess = true;
}
else
{
model.Status = request.Status;
model.MoneyAmount = request.Money;
response.TradeSuccess = false;
}
manager.Update(model);
response.Record = model;
#endregion
return response;
}
public class UpdateOrderStatusRequest
{
public long OrderId{get;set;}
///<summary>
/// 1交易成功,2关闭/失败
///</summary>
public int Status{get;set;}
public decimal Money { get; set; }
}
public class UpdateOrderStatusResponse
{
public bool IsSuccess { get; set; }
///<summary>
/// 交易是否达成
///</summary>
public bool TradeSuccess { get; set; }
public ModelTradeRecord Record { get; set; }
}
#endregion
}
}
public class OrderStatusChangedHandler :ICallHandler,IDisposable
{
public ILogger<LogEntry> Logger { get; set; }
#region IDisposable 成员
public void Dispose()
{
}
#endregion
#region ICallHandler 成员
public IMethodReturn Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext)
{
var methodReturn = getNext().Invoke(input, getNext); //此方法是为了拦截方法或者是所需属性的,一般都放在第一句
if (methodReturn.Exception == null)
{
var response = methodReturn.ReturnValue as TradeService.UpdateOrderStatusResponse;
if (response != null && response.TradeSuccess)
{
try
{
ClientInfoByResponse(input, response);
}
catch (Exception ex)
{
methodReturn.Exception = ex;
}
}
//else
//{
// try
// {
// ClientInfoByResponse(input, response);
// }
// catch (Exception ex)
// {
// methodReturn.Exception = ex;
// }
//}
}
return methodReturn;
}
private void ClientInfoByResponse(IMethodInvocation input, TradeService.UpdateOrderStatusResponse response)
{
#region 将查询卖家和买家的所对应的Clientele信息
var record = response.Record;
var clientShop = OB.R<ClienteleManager>().GetModel("UserId=" + record.BuyerId, "");
//店部分
if (clientShop != null)
{
clientShop.PreShopStatus = clientShop.ShopStatus;
clientShop.ShopStatus = ShopStatus.Actived_Locked_Sober.ToString();
clientShop.ShopStatusChangedTime = DateTime.Now;
clientShop.LastTradeTime = DateTime.Now;
OB.R<ClienteleManager>().Update(clientShop);
input.InvocationContext["ClientShop"] = clientShop;
}
else
{
Logger.WriteLog(
new LogEntry
{
Message = string.Format("UserId:{0},的店家记录不存于Clientele表中!", record.BuyerId),
Priority = 6,
Severity = System.Diagnostics.TraceEventType.Warning
}
);
}
#endregion
}
public int Order
{
get;
set;
}
#endregion
}
还有就是如果您的应用程序需要 AOP,请务必通过 IoC 容器获得。 如果将配置的详细信息移到 app.config 文件(如果是 Web 应用程序则是 web.config)中,就可以用更灵活的方式实现相同的解决方案。附带主要的几个文件: Trade.rar,将其配置到web.config当中即可。