ASP.NET MVC实现依赖注入

在java的spring中有自动注入功能,使得代码变得更加简洁灵活,所以想把这个功能移植到c#中,接下来逐步分析实现过程

1.使用自动注入场景分析

在asp.net mvc中,无论是什么代码逻辑分层,最终的表现层为Controller层,所以我们注入点就是在Controller中,这里我们需要替换默认的ControllerFactory,扫描代码中标记需要注入的对象,进行实例化注入

 public class FastControllerFactory : DefaultControllerFactory
    {
        public override IController CreateController(RequestContext requestContext, string controllerName)
        {
            Type type = this.GetControllerType(requestContext, controllerName);
            Object obj = GetControllerInstance(requestContext, type);

            //Controller中标记AutoWired属性的自动注入
            List<FieldInfo> AutoWiredFieldList = type.GetRuntimeFields().Where(f => f.GetCustomAttribute(typeof(AutoWired)) != null).ToList();
            foreach (FieldInfo field in AutoWiredFieldList)
            {
                field.SetValue(obj, InjectUtil.Container.Resolve(field.FieldType));
            }
            return obj as IController;
        }
    }

FastControllerFactory就是我们自定义的一个Controller工厂,重写CreateController方法,对标记了AutoWired这个自定义注解的变量,从Bean容器中取出实例进行赋值,同时我们还需要在Global文件中的Start方法中,进行默认工厂进行替换

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

2.IOC容器的实现

c#中的自定义容器有很多开源成熟的框架,例如AutoFac等,这里我们是自己实现一个轻量级的版本

源码地址:https://gitee.com/code2roc/FastIOC

这里就重点说一下如何在asp.net mvc中的使用,首先我们需要对需要注入的Bean对象进行标记,这个标记就叫做Component,

在asp.net mvc Global文件中的Start方法中,我们需要将整个项目中需要自动注入的Bean加入到容器中

    public class InjectUtil
    {
        public static ContainerBuilder Container;
        public static void Init()
        {
            Container = new ContainerBuilder();
             //获取所有程序集
            var assemblies = System.Web.Compilation.BuildManager.GetReferencedAssemblies().Cast<Assembly>().ToArray();
            //注入所有Component组件
            Container.RegisterAssemblyTypes(assemblies, typeof(Component),true);
            Container.Build();
        }
    }

 

到这里Controller层面的事项就已经完成了,接下来就需要在IOC容器中初始化Bean实例方法中进一步处理

       private Object GetInstance(RegisterEntity Entity)
        {
            Object obj = null;
            if (Entity.IsEnableIntercept)
            {
                bool IsExtend = Entity.RealType == Entity.RegistType;
                obj = DynamictProxy.CreateProxyObject(Entity.RealType, Entity.RegistType, Entity.InterceptType, IsExtend, Entity.IsInterceptAllMethod);


            }
            else
            {
                var constructors = Entity.RegistType.GetConstructors();
                obj = constructors[0].Invoke(new Object[] { });
            }
            //这里使用单例模式将实例化Instance存储,提前暴露未进行后续设置的对象实例
            if (!SingleInstanceDic.ContainsKey(Entity.RealType))
            {
                SingleInstanceDic.Add(Entity.RealType, obj);
            }
        
            //如果这个class标记了Component,且有标记了AutoWired的Field,进行自动注入
            if (Entity.RealType.GetCustomAttribute(typeof(Component), true) != null)
            {
                //这里要使用GetRuntimeFields,此方法返回在指定类型上定义的所有字段,包括继承,非公共,实例和静态字段。
                foreach (FieldInfo Field in Entity.RealType.GetRuntimeFields())
                {
                    if (Field.GetCustomAttribute(typeof(AutoWired), true) != null)
                    {
                        Type FieldType = Field.FieldType;
                        if (Contains(FieldType))
                        {
                            //判断单例存储中是否包含,如果有,取出赋值,这里可以防止循环依赖导致的死循环
                            if (SingleInstanceDic.ContainsKey(FieldType))
                            {
                                Field.SetValue(obj, SingleInstanceDic[FieldType]);
                            }
                            else
                            {
                                Field.SetValue(obj, Resolve(FieldType));
                            }
                           
                        }
                    }
                }
            }
            return obj;

        }

 

GetInstance方法就是实例化Bean对象的核心方法,其实很简单,就是通过反射创建对象,其中需要注意的有两点

1)对于一个Bean初始化时需要扫描Bean中的所有变量,如果内部还有依赖注入的嵌套对象,需要使用递归,直到没有需要注入的Field

2)我这里使用的是单例模式,因为在测试过程中可能存在在A类中对B进行依赖注入,在B类中对A进行依赖注入,常规创建过程,如果使用递归进行扫描,就会进入死循环,内存溢出,所以使用对象的单例,一旦创建就放入字典中,如果再次扫描到该对象需要注入,则直接取出使用,就避免了循环引用

3.其他

对其他不在Controller中使用的类需要依赖注入,则需要直接从IOC的Bean容器取出使用

 private AuthUtil @AuthUtil = InjectUtil.Container.Resolve<AuthUtil>();

功能到这里就全部分析完毕了,最后打个广告,自己写的ASP.NET MVC快速开发框架,希望支持一波

地址:https://gitee.com/code2roc/FastExecutor

posted @ 2019-09-10 19:26  code2roc  阅读(3179)  评论(0编辑  收藏  举报