Unity容器的简单AOP与DI的应用Demo,基于asp.net mvc框架

转发请注明出处:https://home.cnblogs.com/u/zhiyong-ITNote/

整个Demo是基于Controller-Service-Repository架构设计的,每一层之间是通过接口来实现解耦与调用的,参照了《ASP.NETMVC5框架揭秘》一书最后的网站示例架构,使用Unity容器作为DI容器以及实现AOP。

首先Repository文件夹里面的代码文件:

见百度网盘链接

整个Repository相当于三层架构里面的DAL数据访问层,它的作用就是调用数据库,封装了最基本的增删改查,当然你可以选择ADO.NET或是EntityFramework来做数据库驱动。

其次就是Services文件夹里面的代码文件:

见百度网盘链接

整个Services文件主要的功能就是调用下一层的Repository文件夹的相关类。我们在这里就是使用DI中的构造函数注入了,使用接口来实现解耦,这就需要用到unity容器了。这个层次是为上一层的控制器层服务的。

接下来就是Controller层了,这一层调用下一层Services也是基于接口,使用DI构造函数注入实现了解耦。

见百度网盘链接

准备做好了,接下来就是使用Unity容器来替换MVC框架默认的控制器工厂以及基于Unity的AOP设计。

首先基于DefaultControllerFactory创建一个UnityControllerFactory,引入unity容器:

public class UnityControllerFactory : DefaultControllerFactory
    {
        public IUnityContainer UnityContainer { get; private set; }

        public UnityControllerFactory()
        {
            /// unity container 的AOP可以完成IOC的功能,在我们使用AOP的时候
            /// 也就完成了依赖项的实例化。
            UnityContainer = new UnityContainer();
            UnityContainer.AddNewExtension<Interception>()
                .RegisterType<IFooRepository, FooRepository>() ///IOC注入实现
                .RegisterType<IBarRepository, BarRepository>() ///IOC注入实现
                .RegisterType<IFooService, FooService>() /// FooService的AOP
                .Configure<Interception>()
                .SetInterceptorFor<IFooService>(new InterfaceInterceptor());

            /// BarSerice的AOP
            UnityContainer.AddNewExtension<Interception>()
                .RegisterType<IBarService, BarSerice>()
                .Configure<Interception>()
                .SetInterceptorFor<IBarService>(new InterfaceInterceptor());
        }

        protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
        {
            if (null == controllerType)
            {
                return null;
            }
            return (IController)UnityContainer.Resolve(controllerType);
        }
    }

在构造函数里面使用Unity容器引入IOC和AOP,这是特别重要的:

/// unity container 的AOP可以完成IOC的功能,在我们使用AOP的时候
/// 也就完成了依赖项的实例化。
UnityContainer = new UnityContainer();
UnityContainer.AddNewExtension<Interception>()
.RegisterType<IFooRepository, FooRepository>()
.RegisterType<IBarRepository, BarRepository>()
.RegisterType<IFooService, FooService>() /// FooService的AOP
.Configure<Interception>()
.SetInterceptorFor<IFooService>(new InterfaceInterceptor());


/// BarSerice的AOP
UnityContainer.AddNewExtension<Interception>()
.RegisterType<IBarService, BarSerice>()
.Configure<Interception>()
.SetInterceptorFor<IBarService>(new InterfaceInterceptor());

查看FooSercice类和BarService类,我们在两个方法里面使用了AOP注入,这点是要在Unity构造函数中,用unity容器的创建AOP,AOP的实现是基于IFooService接口与FooService类,IBarService接口和BarService类的。

接下来我们需要替换调用MVC框架中的默认控制器工厂,在Global.asax文件中的Application_Start()方法中:

ControllerBuilder.Current.SetControllerFactory(new UnityControllerFactory());

这样就完成了替换。

最后就是我们的AOP实现了,对于AOP的实现,其实没有什么好说的,我在之前的博客里面写过,随后我会给出链接。

这篇博客的重点是在如果完成一系列的IOC和AOP的注入操作。重点就是UnityControllerFactory类的构造函数里面的注入代码。

程序项目:

链接:https://pan.baidu.com/s/1hGaMlU30RP90qnCrZTTNQA 密码:dmg8

转发请注明出处:https://home.cnblogs.com/u/zhiyong-ITNote/