WedeNet2018.BussinessLogic-业务逻辑层:
结构如下:

基类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WedeNet2018.Infrastructure;
using WedeNet2018.Infrastructure.Components; namespace WedeNet2018.BussinessLogic
{
/// <summary>
/// 业务逻辑层父类
/// </summary>
public abstract class AbsBussinessLogic
{
public AbsBussinessLogic()
{
} /// <summary>
/// 接受一个实现了IUnitOfWorks接口的工作单元实例
/// </summary>
protected IUnitOfWorks _works { get; set; } /// <summary>
/// 当前工作单元的提交方法,可在子类中重写。
/// </summary>
/// <returns></returns>
public virtual int Commit()
{
return _works.Commit();
}
}
}

这个基类的目的主要是实现UnitOfWorks的事务性提交,对于具体业务性的操作放在派生类中。当应用层调用不同的BussinessLogic进行对应的业务处理完毕后,可调用该数据上下文对应的UnitOfWorks实例的Commit()方法统一提交。

实现类如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WedeNet2018.Infrastructure;
using WedeNet2018.Infrastructure.Components; namespace WedeNet2018.BussinessLogic
{
public class OrdersBussinessLogic : AbsBussinessLogic
{
public OrdersBussinessLogic(IWedeUnitOfWorks works)
{
_works = works;
} public IQueryable<Orders> GetOrders(int orderType){
IQueryable<Orders> ret = null;
ret = _works.All<Orders>().Where(r=>r.OrderType.Equals(orderType)); return ret;
} public void Add(Orders order) {
_works.Add<Orders>(order);
} public Orders Find(int id) {
Orders order = _works.Find<Orders>(id);
return order;
} public void Update(Orders order)
{
_works.Update<Orders>(order);
} public void Delete(int id)
{
Orders order = _works.Find<Orders>(id);
_works.Delete<Orders>(order);
}
}
}

异常处理
系统异常(不论是预期的或非预期的)都要抛至业务逻辑层为止,业务层对捕获的异常进行处理。
规则为:
1、使用RealProxy动态织入业务层指定的方法;
2、自定义异常类和Attribute;
3、业务逻辑层以下发生的异常层层上抛至业务逻辑层,并且要日志记录异常发生具体信息;
4、业务逻辑层捕获到下层抛出的异常后可以区分是预期异常还是非预期异常;
5、如果全局异常处理器捕获到了非预期的异常,则统一抛出“未知错误”;
6、可以对捕获的异常再次处理返给客户端;

业务层实现代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WedeNet2018.Common;
using WedeNet2018.Common.Attributes;
using WedeNet2018.Common.Exceptions;
using WedeNet2018.Infrastructure;
using WedeNet2018.Infrastructure.Components;
using WedeNet2018.Infrastructure.Components.aop; namespace WedeNet2018.BussinessLogic
{
public class OrdersBussinessLogic : AbsBussinessLogic
{
public OrdersBussinessLogic(IWedeUnitOfWorks works)
{
log = LoggerHelper.WedeNetLogger;
//_works = works; #region AOP动态织入
var dynamicProxy = new DynamicProxy<IWedeUnitOfWorks>(works);
dynamicProxy.BeforeExecute += (s, e) =>
{
log.Info(e.MethodName + "方法执行前");
};
dynamicProxy.AfterExecute += (s, e) =>
{
log.Info(e.MethodName + "方法执行后");
};
dynamicProxy.ErrorExecuting += (s, e) =>
{
log.Info(e.MethodName + "方法执行异常");
Type t = works.GetType();
//标注了[CustomFilter]注解的IUnitOfWorks类全局异常处理才生效
if (t.IsDefined(typeof(CustomFilterAttribute), false))
{
throw new CustomException(dynamicProxy.MyException.Message,
dynamicProxy.MyException.InnerException);
} };
//过滤不需要织入的方法
dynamicProxy.Filter = m => (!m.Name.StartsWith("Get") || !m.Name.StartsWith("Find"));
_works = dynamicProxy.GetTransparentProxy() as IWedeUnitOfWorks;
#endregion
} public IQueryable<Orders> GetOrders(int orderType){
IQueryable<Orders> ret = null;
try
{
ret = _works.All<Orders>().Where(r => r.OrderType.Equals(orderType));
}
catch (Exception ex)
{
#region 异常处理
if (ex is CustomException)
{
log.Info("自定义异常:" + ex.Message);
if (ex.InnerException != null)
{
log.Info(ex.InnerException.Message + "发生于" + ex.InnerException.TargetSite.ReflectedType + "类的" + ex.InnerException.TargetSite.Name + "方法。");
}
}
else
{
log.Info("未知异常:" + ex.Message);
if (ex.InnerException != null)
{
log.Info(ex.InnerException.Message + "发生于" + ex.InnerException.TargetSite.ReflectedType + "类的" + ex.InnerException.TargetSite.Name + "方法。");
}
}
#endregion
}
return ret;
} public bool Add(Orders order) {
try
{
_works.Add<Orders>(order);
return true;
}
catch (Exception ex) {
#region 异常处理
if (ex is CustomException)
{
log.Info("自定义异常:" + ex.Message);
if (ex.InnerException != null)
{
log.Info(ex.InnerException.Message + "发生于" + ex.InnerException.TargetSite.ReflectedType + "类的" + ex.InnerException.TargetSite.Name + "方法。");
}
}
else
{
log.Info("未知异常:" + ex.Message);
if (ex.InnerException != null)
{
log.Info(ex.InnerException.Message + "发生于" + ex.InnerException.TargetSite.ReflectedType + "类的" + ex.InnerException.TargetSite.Name + "方法。");
}
}
#endregion
return false;
}
} public Orders Find(int id) {
Orders order = _works.Find<Orders>(id);
return order;
} public bool Update(Orders order)
{
try
{
_works.Update<Orders>(order);
return true;
}
catch (Exception ex)
{
#region 异常处理
if (ex is CustomException)
{
log.Info("自定义异常:" + ex.Message);
if (ex.InnerException != null)
{
log.Info(ex.InnerException.Message + "发生于" + ex.InnerException.TargetSite.ReflectedType + "类的" + ex.InnerException.TargetSite.Name + "方法。");
}
}
else
{
log.Info("未知异常:" + ex.Message);
if (ex.InnerException != null)
{
log.Info(ex.InnerException.Message + "发生于" + ex.InnerException.TargetSite.ReflectedType + "类的" + ex.InnerException.TargetSite.Name + "方法。");
}
}
#endregion
return false;
}
} public bool Delete(int id)
{ Orders order = _works.Find<Orders>(id);
try
{
_works.Delete<Orders>(order);
return true;
}
catch (Exception ex)
{
#region 异常处理
if (ex is CustomException)
{
log.Info("自定义异常:" + ex.Message);
if (ex.InnerException != null)
{
log.Info(ex.InnerException.Message + "发生于" + ex.InnerException.TargetSite.ReflectedType + "类的" + ex.InnerException.TargetSite.Name + "方法。");
}
}
else
{
log.Info("未知异常:" + ex.Message);
if (ex.InnerException != null)
{
log.Info(ex.InnerException.Message + "发生于" + ex.InnerException.TargetSite.ReflectedType + "类的" + ex.InnerException.TargetSite.Name + "方法。");
}
}
#endregion
return false;
}
} }
}

动态代理代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.Remoting.Messaging;
using System.Runtime.Remoting.Proxies;
using System.Text;
using System.Threading.Tasks; namespace WedeNet2018.Infrastructure.Components.aop
{
public class DynamicProxy<T> : RealProxy
{
private readonly T _decorated;
private Predicate<MethodInfo> _filter;
public event EventHandler<IMethodCallMessage> BeforeExecute;
public event EventHandler<IMethodCallMessage> AfterExecute;
public event EventHandler<IMethodCallMessage> ErrorExecuting;
public DynamicProxy(T decorated)
: base(typeof(T))
{
_decorated = decorated;
Filter = m => true;
}
public Predicate<MethodInfo> Filter
{
get { return _filter; }
set
{
if (value == null)
_filter = m => true;
else
_filter = value;
}
}
private void OnBeforeExecute(IMethodCallMessage methodCall)
{
if (BeforeExecute != null)
{
var methodInfo = methodCall.MethodBase as MethodInfo;
if (_filter(methodInfo))
BeforeExecute(this, methodCall);
}
}
private void OnAfterExecute(IMethodCallMessage methodCall)
{
if (AfterExecute != null)
{
var methodInfo = methodCall.MethodBase as MethodInfo;
if (_filter(methodInfo))
AfterExecute(this, methodCall);
}
}
private void OnErrorExecuting(IMethodCallMessage methodCall, Exception ex)
{
if (ErrorExecuting != null)
{
this.MyException = ex;
var methodInfo = methodCall.MethodBase as MethodInfo;
if (_filter(methodInfo))
ErrorExecuting(this, methodCall);
}
} public Exception MyException
{
get;
set;
} public override IMessage Invoke(IMessage msg)
{
var methodCall = msg as IMethodCallMessage;
var methodInfo = methodCall.MethodBase as MethodInfo;
OnBeforeExecute(methodCall);
try
{
var result = methodInfo.Invoke(_decorated, methodCall.InArgs);
OnAfterExecute(methodCall);
return new ReturnMessage(
result, null, 0, methodCall.LogicalCallContext, methodCall);
}
catch (Exception e)
{
//CustomException customException = null; Exception ex = null;
//if (typeof(T).IsDefined(typeof(CustomFilterAttribute), true)) {
// customException = new CustomException(e.Message, e.InnerException);
//}
//if (customException != null)
// ex = customException;
//else
// ex = e; OnErrorExecuting(methodCall, e);
return new ReturnMessage(e, methodCall);
}
} }
}

搭建自己的框架WedeNet(三)的更多相关文章

  1. 搭建自己的框架WedeNet(五)

    WedeNet2018.WedeWcfServices-WCF服务层:结构如下: 就是定义了服务契约接口和服务类,以OrderServices为例,如下: using System; using Sy ...

  2. 搭建自己的框架WedeNet(四)

    WedeNet2018.Web-UI层:结构如下: 首先,在Controller中定义BaseController,以便加入统一处理逻辑,如下: using log4net; using System ...

  3. 搭建自己的框架WedeNet(一)

    框架用到的技术: EF.UnitOfWork+Repository.Ninject.log4net.WCF.MVC.T4.windows服务.AOP前端技术:Bootstrap.layer.jQuer ...

  4. 搭建自己的框架WedeNet(二)

    WedeNet2018.Infrastructure-基础设施层:结构如下: Tools结构如下: 考虑到系统可能会有多个数据上下文(暂时以两个为例),所以根据需要定义两个T4模板用来生成对应的ent ...

  5. 如何搭建MVC + EF 框架

    1.搭建MVC框架 1.1 VS2010:需要安装WPI 安装 ASP.NET MVC 4 和Visual Studio 2010 系统必备组件 如果上述链接无法打开,请访问:http://www.a ...

  6. Xvfb+YSlow+ShowSlow搭建前端性能测试框架 - 前端技术 | TaoBaoUED

    Xvfb+YSlow+ShowSlow搭建前端性能测试框架 - 前端技术 | TaoBaoUED Xvfb+YSlow+ShowSlow搭建前端性能测试框架 作者:黑三 | 时间:2010-07-07 ...

  7. Windows环境搭建Web自动化测试框架Watir

    Windows环境搭建Web自动化测试框架Watir 一.前言     Web自动化测试一直是一个比较迫切的问题,对于现在web开发的敏捷开发,却没有相对应的敏捷测试,故开此主题,一边研究,一边将We ...

  8. 搭建App主流框架_纯代码搭建(OC)

    转载自:http://my.oschina.net/hejunbinlan/blog/529778?fromerr=EmSuX7PR 搭建主流框架界面 源码地址在文章末尾 达成效果 效果图 注:本文部 ...

  9. 【微服务】使用spring cloud搭建微服务框架,整理学习资料

    写在前面 使用spring cloud搭建微服务框架,是我最近最主要的工作之一,一开始我使用bubbo加zookeeper制作了一个基于dubbo的微服务框架,然后被架构师否了,架构师曰:此物过时.随 ...

随机推荐

  1. cannot open clipboard 解决办法

    对于电脑本身或者一些应用程序操作的时候,会出现cannot open clipboard的问题,这是你系统没有剪切板程序 首先: 在开始->运行中输入clipbrd 回车, 如果系统弹出了剪切板 ...

  2. Android自定义权限与使用

    1. 如何自定义权限 Android允许我们使用permission标签,在Manifest文件中定义属于自己的权限,一个例子如下, <?xml version="1.0" ...

  3. 升级到Android Studio3.x遇到的问题及解决方案

    升级到Android Studio3.x遇到的问题及解决方案 转 https://www.2cto.com/kf/201711/695736.html 升级到Android Studio3.0遇到的问 ...

  4. 深度学习之强化学习Q-Learning

    1.知识点 """ 1.强化学习:学习系统没有像很多其他形式的机器学习方法一样被告知应该做什么行为, 必须在尝试之后才能发现哪些行为会导致奖励的最大化,当前的行为可能不仅 ...

  5. 如何修改Zend Studio代码字体和大小

    Zend Studio的默认字体非常小,看起来很费神,这里教大家怎么修改它的字体和大小.   工具/原料   Zend Studio 方法/步骤     Window-->preferences ...

  6. AngularJS 指令(Directivce )一

    指令(Directive),是AngularJS中一块比较重要的内容,在实践中,我们提倡将控制器(Controller)写得短小精悍,逻辑代码尽量少,要实现这种目的,我们主要通过将逻辑代码抽取到服务( ...

  7. 【C# in depth 第三版】温故而知新(1) (转)

    声明 本文欢迎转载,原文地址:http://www.cnblogs.com/DjlNet/p/7192354.html 前言 关于这本书(<深入理解C# 第三版>)的详细情况以及好坏,自行 ...

  8. Android View重绘和更新: invalidate和requestLayout 总结的不错 赶紧复制。。哈哈

    总述:View有两个很重要的方法:invalidate和requestLayout,常用于View重绘和更新. Invalidate:To farce a view to draw,call inva ...

  9. Linux学习—mysql安装配置及远程连接

    安装前准备 1.检查是否已经安装过mysql 执行命令 [root@localhost /]# rpm -qa | grep mysql  从执行结果,可以看出我们已经安装了mysql-libs-5. ...

  10. What happens when you type an URL in the browser and press enter?

    What happens when you type an URL in the browser and press enter? 1. You type maps.google.com into t ...