20180703Advanced11Course26AOP

一、.net  Unity版本

Unity:5.8.6
Unity.Abstractions:3.3.0
Unity.Interception:5.5.3

.net Framework 4.7.2

二、该版本对应的配置文件 

<?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="UnityProject.UnityWay.IUserProcessor,UnityProject" mapTo="UnityProject.UnityWay.UserProcessor,UnityProject">
                    <interceptor type="InterfaceInterceptor"/>
                    <interceptionBehavior type="UnityProject.UnityWay.ExceptionLoggingBehavior, UnityProject"/>
                    <interceptionBehavior type="UnityProject.UnityWay.CachingBehavior, UnityProject"/>
                    <interceptionBehavior type="UnityProject.UnityWay.LogBeforeBehavior, UnityProject"/>
                    <interceptionBehavior type="UnityProject.UnityWay.ParameterCheckBehavior, UnityProject"/>
                    <interceptionBehavior type="UnityProject.UnityWay.LogAfterBehavior, UnityProject"/>
                </register>
            </container>
        </containers>
    </unity>
</configuration>
View Code

 

 三、.net Core Unity版本

1、System.Configuration.ConfigurationManager 5.0.0

2、Unity 5.11.7

3、Unity.Abstractions 5.11.6

4、Unity.Configuraion 5.11.2

5、Unity.Interception 5.11.1

四、.net 对应的配置文件

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
	<configSections>
		<section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Unity.Configuration"/>
	</configSections>
	<unity>
		<typeAliases>
			<typeAlias alias="IPhone" type="UnityProject.UnityWay.IUserProcessor,UnityProject" />
			<typeAlias alias="NubiaPhone" type="UnityProject.UnityWay.UserProcessor,UnityProject" />
		</typeAliases>
		<containers>
			<container name="IOCcontainer">
				<types>
					<type type="IPhone" mapTo="NubiaPhone"></type >
					
				</types>
				
			</container>
		</containers>
	</unity>
</configuration>

  

五、AOP使用

using System;
using System.Collections.Generic;
using Unity.Interception.InterceptionBehaviors;
using Unity.Interception.PolicyInjection.Pipeline;

namespace UnityProject.UnityWay
{
    /// <summary>
    /// 不需要特性
    /// </summary>
    public class CachingBehavior : IInterceptionBehavior
    {
        public IEnumerable<Type> GetRequiredInterfaces()
        {
            return Type.EmptyTypes;
        }

        private static Dictionary<string, object> CachingBehaviorDictionary = new Dictionary<string, object>();

        public IMethodReturn Invoke(IMethodInvocation input, GetNextInterceptionBehaviorDelegate getNext)
        {
            Console.WriteLine("CachingBehavior");
            string key = $"{input.MethodBase.Name}_{Newtonsoft.Json.JsonConvert.SerializeObject(input.Inputs)}";
            if (CachingBehaviorDictionary.ContainsKey(key))
            {
                return input.CreateMethodReturn(CachingBehaviorDictionary[key]);//直接返还  断路器  不再往下
            }
            else
            {
                IMethodReturn result = getNext().Invoke(input, getNext);
                if (result.ReturnValue != null)
                    CachingBehaviorDictionary.Add(key, result.ReturnValue);
                return result;
            }


        }

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

  

六、接口

namespace UnityProject.UnityWay
{
    public interface IUserProcessor
    {
        void RegUser(User user);
        User GetUser(User user);
    }
}

七、类

using System;

namespace UnityProject.UnityWay
{
    public class UserProcessor : IUserProcessor
    {
        public void RegUser(User user)
        {
            Console.WriteLine("用户已注册。");
            //throw new Exception("11");
        }
        [Obsolete]
        public User GetUser(User user)
        {
            return user;
        }
    }
}

八、动态代理

    /// <summary>
    /// 使用EntLib\PIAB Unity 实现动态代理
    /// 
    /// </summary>
    public class UnityConfigAOP
    {
        public static void Show()
        {
            User user = new User()
            {
                Name = "Eleven",
                Password = "12345678934534643"
            };
            {
                IUnityContainer container = new UnityContainer();
                container.RegisterType<IUserProcessor, UserProcessor>();
                IUserProcessor processor = container.Resolve<IUserProcessor>();
                processor.RegUser(user);
            }
            {
                //配置UnityContainer
                IUnityContainer container = new UnityContainer();
                ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
                fileMap.ExeConfigFilename = Path.Combine(AppDomain.CurrentDomain.BaseDirectory + "CfgFiles\\App.Config");
                Configuration configuration = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
                UnityConfigurationSection configSection = (UnityConfigurationSection)configuration.GetSection(UnityConfigurationSection.SectionName);
                configSection.Configure(container, "IOCcontainer");

                IUserProcessor processor = container.Resolve<IUserProcessor>();
                processor.RegUser(user);
                User userNew1 = processor.GetUser(user);
                User userNew2 = processor.GetUser(user);
            }
        }
    }

九、实体

namespace UnityProject
{
    public class User
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Password { get; set; }
    }
}

  

 

posted @ 2020-11-19 01:52  揽月2020  阅读(63)  评论(0编辑  收藏  举报