Unity面向切面编程

一直说面向AOP(切面)编程,好久直接专门扒出理论、代码学习过。最近因为某些原因😭还得再学学造火箭的技术。
废话不多说,啥是AOP呢?这里我就不班门弄斧了,网上资料一大堆,解释的肯定比我清楚~
用三层的结构来说。一个请求的处理顺序是:UI=》BLL=>DAL=》BLL=》UI。
那么问题来了,如果某天老板为应付检查,要增加操作日志。普通处理方式是我把A、B、C…的方法全部改造一遍,增加操作日志。系统小还好说,如果是代码山那改动可就大了…

咋整?
这会就用到AOP思想了。(UI=》 ( Log =》) BLL=>DAL=》BLL=》 UI)
UI接收到请求后 在请求BLL的时候统一增加日志记录。

下面用Unity中间件实现的,

  • 引入nuget包

在这里插入图片描述

  • 增加Unity配置
<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Unity.Configuration" />
  </configSections>
  <unity>
    <sectionExtension type="Microsoft.Practices.Unity.InterceptionExtension.Configuration.InterceptionConfigurationExtension, Unity.Interception.Configuration" />
    <containers >
      <container name="aopContainer">
        <extension type="Interception"/>
        <register type="learn04.IUserProcessor,learn04"  mapTo="learn04.UserProcessor,learn04">
          <!--InterfaceInterceptor:继承接口的方法都会被拦截。
              TransparentProxyInterceptor:继承类使用的方法都会被拦截。
              VirtualMethodInterceptor:继承的方法必须是虚方法且必须是公开的方法才会被拦截。-->
          <interceptor type="InterfaceInterceptor"/>
          <!--配置文件的注册顺序是调用顺序,然后才是业务方法,但是扩展逻辑也可以在业务方法之后-->
          <interceptionBehavior type="learn04.LogBeforeBehiavor, learn04"/>
        </register>
      </container>
    </containers>
  </unity>
</configuration>
  • 上代码:
public class User
{
    public string Name { get; set; }
    public string Password { get; set; }
}

public interface IUserProcessor
{
    void RegUser(User user);
}

public class UserProcessor : IUserProcessor
{
    public void RegUser(User user)
    {
        Console.WriteLine($"用户已注册:Name:{user.Name}");
    }
}

public class LogBeforeBehiavor:IInterceptionBehavior
{
    public IEnumerable<Type> GetRequiredInterfaces()
    {
        return Type.EmptyTypes;
    }

    public IMethodReturn Invoke(IMethodInvocation input, GetNextInterceptionBehaviorDelegate getNext)
    {
        Console.WriteLine("LogBeforeBehavior");
        foreach (var item in input.Inputs)
        {
            Console.WriteLine(item.ToString());//反射获取更多信息
        }
        var query = getNext().Invoke(input,getNext);
        return query;
    }

    public bool WillExecute
    {
        get { return true; }
    }
}


public class UnityConfigAop
{
    public static void Show()
    { 
        User user = new User()
        { 
            Name ="马六",
            Password = "123"
        };

        IUnityContainer container = new UnityContainer();
        ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
        fileMap.ExeConfigFilename = Path.Combine(AppDomain.CurrentDomain.BaseDirectory + "Unity.config");
        Configuration configuration = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
        UnityConfigurationSection configSection = (UnityConfigurationSection)configuration.GetSection(UnityConfigurationSection.SectionName);
        //将扩展部分注册到容器
        configSection.Configure(container, "aopContainer");

        IUserProcessor processor = container.Resolve<IUserProcessor>();
        processor.RegUser(user);
    }
}




internal class Program
{
    static void Main(string[] args)
    {
        UnityConfigAop.Show();
        Console.ReadKey();
    }
}

执行结果:
在这里插入图片描述
当然AOP只是一种思想,实现的方式千万条。这里就先用这种写写~

相信只要是.net 的同学都会搞,如果你好久没复习过技术了(毕竟都是流水线搬砖)可以复制下代码来跑跑。注释已经把该说的说了。这里就不多废话了~


有兴趣的同学可以关注我,一起学习哈~

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值