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. Navicat 12 for MySQL最新版激活(注册机)(转)(亲测有效)

    Navicat 12 for MySQL最新版激活(注册机)(转)(亲测有效) 一.总结 一句话总结: 1.卸载自己机器上面的Navicat,安装下载的包里面的Navicat安装包,不然可能不行 2. ...

  2. mysql if--else

    SQL之case when then用法 case具有两种格式.简单case函数和case搜索函数. --简单case函数 case sex when '1' then '男' when '2' th ...

  3. java读取XML文件,及封装XML字符串

    package com.yyl.text; import java.io.FileInputStream; import java.util.ArrayList; import org.junit.T ...

  4. 使用Python爬取mobi格式电纸书

    最近做了个微信推送kindle电子书的公众号:kindle免费书库 不过目前电子书不算非常多,所以需要使用爬虫来获取足够书籍. 于是,写了以下这个爬虫,来爬取kindle114的电子书. 值得注意的地 ...

  5. pytest+allure展示环境信息

    allure展示环境信息 要将信息添加到Environment小部件,只需在生成报告之前在目录中创建environment.properties(或environment.xml)文件allure-r ...

  6. maven 依赖原则

    maven 依赖原则 ###间接依赖路径最短优先 a->b->c1.0 a->e->f->c1.1 ====>c1.0 申明顺序优先 <!-- test1 - ...

  7. 第二章 kali安装

    @kali安装 本文以虚拟机进行安装(注意:虚拟机安装不等同于物理机安装,在虚拟机安装成功不等于一定能在物理机,U盘安装成功) 下载kali镜像 官方地址:https://www.kali.org/d ...

  8. D3画完整柱状图(带坐标轴、标签)

    昨天晚上本来打算花一点时间把之前学的柱状图改一下,用CSV文件来替换自定义数据.这一替换可不得了,一晚上就搭进去了,还好今早找到了问题的所在,原因在于我的数据引用出了问题. 现在就来讲解一下如何画一个 ...

  9. 【FFMPEG】基于RTP的H264视频数据打包解包类

    最近考虑使用RTP替换原有的高清视频传输协议,遂上网查找有关H264视频RTP打包.解包的文档和代码.功夫不负有心人,找到不少有价值的文档和代码.参考这些资料,写了H264 RTP打包类.解包类,实现 ...

  10. python 配合 es 查询数据

    1.python脚本 [root@do1cloud03 ~]# cat python-es.py #!/usr/bin/env python3 from elasticsearch import El ...