Microsoft.Practices.Unity AOP unity 3.x

上一文 Microsoft.Practices.Unity mvc controller 注入

本文记录为主,代码取自网络各大神,本地测试通过并记录在案。

内容:AOP 切面编程

核心代码:标的有注释

using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.InterceptionExtension;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NLog;
using System.Diagnostics;

namespace TestClass.common
{
    public class CallHandle : IInterceptionBehavior
    {
        private Logger _log = NLog.LogManager.GetCurrentClassLogger();

        /// <summary>
        /// 获取当前行为需要拦截的对象类型接口。
        /// </summary>
        /// <returns>所有需要拦截的对象类型接口。</returns>
        public IEnumerable<Type> GetRequiredInterfaces()
        {
            return Type.EmptyTypes;
        }

        /// 通过实现此方法来拦截调用并执行所需的拦截行为。
        /// </summary>
        /// <param name="input">调用拦截目标时的输入信息。</param>
        /// <param name="getNext">通过行为链来获取下一个拦截行为的委托。</param>
        /// <returns>从拦截目标获得的返回信息。</returns>
        public IMethodReturn Invoke(IMethodInvocation input, GetNextInterceptionBehaviorDelegate getNext)
        {
            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Reset();
            stopwatch.Start();
            //权限控制

            IMethodReturn retvalue = getNext()(input, getNext);

            #region 参数部分
            for (int i = 0; i < input.Arguments.Count; i++)
            {
                var parameter = input.Arguments[i];
                _log.Info(string.Format("第{0}个参数值为:{1}", i + 1, parameter.ToString()));
            }
            #endregion

            #region 异常处理部分
            if (retvalue.Exception == null)
            {
                //执行时间
                stopwatch.Stop();
                _log.Info(String.Format("Method {0} executed {1}", input.Target.ToString() + " " + input.MethodBase, stopwatch.Elapsed));
            }
            else
            {
                //异常记录
                _log.Info("异常的内容为:" + retvalue.Exception.Message);
                retvalue.Exception = null;
            }
            #endregion
            return retvalue;
        }

        /// <summary>
        /// 获取一个<see cref="Boolean"/>值,该值表示当前拦截行为被调用时,是否真的需要执行
        /// 某些操作。
        /// </summary>
        public bool WillExecute
        {
            get { return true; }
        }

    }
}

测试页面:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcApplication1.common;
using TestClass;
using TestInterface;
using Microsoft.Practices.Unity;  

namespace MvcApplication1.Controllers
{
    public class testController : Controller
    {
        /// <summary>
        /// 这个玩意要加上的
        /// </summary>
        [Dependency]
        public EmptyInterface e { get; set; }

        public ActionResult Index()
        {
            return View();
        }

        public string testUnity()
        {
            //EmptyInterface e = ObjectContainer.CreateObject<EmptyInterface>();
            return e.sayHello();
            //return "testUnity";
        }

        public string method1(int a=0)
        {
            return e.method1(a);
        }
    }
}

配置文件:如有问题,请看上几篇

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection,Microsoft.Practices.Unity.Configuration"/>
  </configSections>
  <unity>
    <sectionExtension type="Microsoft.Practices.Unity.InterceptionExtension.Configuration.InterceptionConfigurationExtension,
      Microsoft.Practices.Unity.Interception.Configuration" />

    <alias alias="MyClass" type="TestClass.MyClass, TestClass"/>
    <alias alias="EmptyInterface" type="TestInterface.EmptyInterface, TestInterface"/>
    <!--指定AOP行为-->
    <alias alias="CallHandleInterceptionBehavior" type="TestClass.common.CallHandle,TestClass"/>

    <container>
      <extension type="Interception"/>
      <register type="EmptyInterface" mapTo="MyClass">
        <interceptor type="InterfaceInterceptor"/>
        <!--指定AOP行为-->
        <interceptionBehavior type="CallHandleInterceptionBehavior" />
      </register>
    </container>

  </unity>
</configuration>

作者:zc

出处:http://www.cnblogs.com/jmzs/

本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

时间: 2024-08-01 10:45:36

Microsoft.Practices.Unity AOP unity 3.x的相关文章

利用Microsoft.Practices.Unity的拦截技术,实现.NET中的AOP

1.记住这个单词的意思:Interception(拦截) 2.首先说一下原理和背景 原理:所谓的AOP就是面向切面编程,这里不多说,百度搜索. 目的:个人认为是为了解耦,部分代码跟业务代码分离,业务代码里面不掺杂其它功能,比如:记录异常.记录操作日志. 背景:项目基本功能已完成,产品要求记录用户的操作日志,新增的时候记录某人在某时做了某事(包括详细的信息,比如新增了哪些字段或者修改了哪些字段).于是着手在业务代码里写了大量的关于记录操作日志的代码,怎么看怎么别扭,像是被XX了的感觉. 3.考虑解

第九回 Microsoft.Practices.Unity.Interception实现基于数据集的缓存(针对六,七,八讲的具体概念和配置的解说)

返回目录 概念 Microsoft.Practices.Unity.Interception是一个拦截器,它隶属于Microsoft.Practices.Unity组成之中,主要完成AOP的功能,而实现AOP方法拦截(页向切面)的基础就是IoC,我们需要配置相关接口或者类型的IoC方式,然后在生产对象时,使用Unity的方法进行动态生产对象,这时,你的Interception拦截器也会起作用! 相关技术 IoC: 控制反转(Inversion of Control,英文缩写为IoC)是一个重要的

Microsoft.Practices.Unity 通用类

类库的用法网上有很多,就不多说了,下面稍微封装了一下,记个笔记. 结合泛型接口类型和配置文件,得到IUnityContainer实例存于键值对中. 1 namespace Containers 2 { 3 public sealed class ObjectContainer 4 { 5 //fields 6 private static readonly object containerLock = new object(); 7 private static Dictionary<strin

Microsoft.Practices.Unity入门

Unity是微软Patterns & Practices团队所开发的一个轻量级的,并且可扩展的依赖注入(Dependency Injection)容器,它支持常用的三种依赖注入方式:构造器注入(Constructor Injection).属性注入(Property Injection),以及方法调用注入(Method Call Injection).现在Unity最新的版本的1.2版,可以在微软的开源站点http://unity.codeplex.com下载最新的发布版本和文档.通过使用Uni

VS - Microsoft.Practices.Unity

PM>  Install-Package Unity Web.config <configSections> <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection,Microsoft.Practices.Unity.Configuration" /> </configSections> <

使用Microsoft.Practices.Unity 依赖注入

Unity是微软Patterns & Practices团队所开发的一个轻量级的,并且可扩展的依赖注入(Dependency Injection)容器,它支持常用的三种依赖注入方式:构造器注入(Constructor Injection).属性注入(Property Injection),以及方法调用注入(Method Call Injection). 假设我们有下面的场景代码,在代码里面有一个很简单的customer对象,customer 对象有个save 方法, 这个方法通过调用ICusto

DI 依赖注入之unity的MVC版本使用Microsoft.Practices.Unity1.2与2.0版本对比

DI 依赖注入之unity的MVC版本使用Microsoft.Practices.Unity1.2与2.0版本对比 参考:https://www.cnblogs.com/xishuai/p/3670292.html 参考:https://docs.microsoft.com/en-us/previous-versions/msp-n-p/ff660914(v=pandp.20)?redirectedfrom=MSDN#config_registerelement 必读: 在unity1.2中我们

Microsoft.Practices.EnterpriseLibrary

项目中使用了Microsoft.Practices.EnterpriseLibrary这个东西,根据名字猜测和微软有关系(可以翻译为:微软实践企业库). 看到了引入了两个命名空间: using Microsoft.Practices.EnterpriseLibrary.Data; using Microsoft.Practices.EnterpriseLibrary.Data.Sql; 在项目引用中发现了三个相关的dll文件: Microsoft.Practices.EnterpriseLibr

在WebService中使用Microsoft.Practices.EnterpriseLibrary.Data配置数据库

1. 新建WebApplication1项目 1.1 新建—Web—ASP.NET Empty Web Application--WebApplication1 1.2 添加一个WebForm1 2. 新建webService项目 2.1 新建—Web—ASP.NET Empty Web Application—WebApplicationService 2.2 选择WebApplicationService—右键—Add—WebService (ASMX)-- WebServiceGSS.as