Demo中的IOC自定义实现

在做练习的时候,小小项目,使用IOC控件觉得麻烦,使用工厂觉得不高大上啊,自己写个简陋的依赖注入IOC吧;

控制反转(IOC)是管理映射依赖的的,是依赖倒置(DIP)的实现方式;

依赖倒置(DIP)是设计原则,控制反转(IOC)是具体实现,依赖注入(DI)是控制反转的具体实现;

解决方案的目录:

image

IOC 有3个类,一个是用来保存依赖关系的实体类(EntityIOC),一个是保存依赖关系的类(InterviewsDependencyResolver),一个是外部调用类(InterviewsIOC);

我理解的核心就是依赖关系.使用反射实现依赖倒置.

EntityIOC 代码:

/// <summary>
    /// 依赖注入实体
    /// </summary>
    internal class EntityIoc
    {
        private EntityIoc(){        }
        public EntityIoc(string _namespace, string _fullName)
        {
            Namespace = _namespace;
            FullName = _fullName;
        }
        /// <summary>
        /// 程序集名称
        /// </summary>
        public string Namespace { get; set; }
        /// <summary>
        /// 实例名(不包括命名空间)
        /// </summary>
        public string FullName { get; set; }

    }

InterviewsDependencyResolver 代码:

 /// <summary>
    ///容器
    /// </summary>
    internal   class InterviewsDependencyResolver<IEntity> where IEntity : class
    {
        private InterviewsDependencyResolver() { }

        private static Dictionary<EntityIoc, EntityIoc> _dictIoc = new Dictionary<EntityIoc, EntityIoc>();

        //private static Dictionary<Type, Type> accessDict = new Dictionary<Type, Type>();
        public static EntityIoc Get()
        {
            Type ientity = typeof(IEntity);
            //需要返回的值
            EntityIoc iioc = new EntityIoc(ientity.Module.Name.Replace(".dll", ""), ientity.FullName);
            //集合中的数据
            iioc = _dictIoc.Keys.Where(a => a.FullName == iioc.FullName && a.Namespace == iioc.Namespace).FirstOrDefault();
            if (iioc != null)
            {
                //根据Key返回集合中保存的实例信息
                return _dictIoc[iioc];
            }
            return null;
        }

        /// <summary>
        /// 绑定
        /// </summary>
        /// <typeparam name="T"></typeparam>
        public static void To<T>() where T : class
        {
            Type ientity = typeof(IEntity);
            Type entity = typeof(T);
            EntityIoc iioc = new EntityIoc(ientity.Module.Name.Replace(".dll", ""), ientity.FullName);
            EntityIoc ioc = new EntityIoc(entity.Module.Name.Replace(".dll", ""), entity.FullName);
            if (!_dictIoc.Keys.Contains(iioc))
            {
                _dictIoc.Add(iioc, ioc);
            }
        }
        /// <summary>
        /// 取消绑定
        /// </summary>
        /// <typeparam name="T"></typeparam>
        public static void UninstallTo<T>() where T : class
        {
            Type ientity = typeof(IEntity);
            EntityIoc iioc = new EntityIoc(ientity.Module.Name.Replace(".dll", ""), ientity.FullName);
            //集合中的数据
            iioc = _dictIoc.Keys.Where(a => a.FullName == iioc.FullName && a.Namespace == iioc.Namespace).FirstOrDefault();
            if (iioc != null)
            {
                _dictIoc.Remove(iioc);
            }
        }


    }

InterviewsIOC 代码:

/// <summary>
    /// 简单IOC 
    /// </summary>
    public class InterviewsIOC
    {
        private InterviewsIOC() { }
        /// <summary>
        /// 绑定
        /// </summary>
        public static void Bing()
        {
            //配置文件读取或者直接绑定,如果是配置文件读取,需要先读取后反射为类(用来判断依赖是否正确)
            InterviewsDependencyResolver<IAccountService>.To<Impl.AccountService>();
        }
        /// <summary>
        /// 获取依赖中具体实现
        /// </summary>
        /// <typeparam name="IEntity"></typeparam>
        /// <returns></returns>
        public static IEntity LoadInstance<IEntity>() where IEntity : class
        {
            IEntity resultEntity = null;
            EntityIoc ioc = InterviewsDependencyResolver<IEntity>.Get();
            if (ioc != null)
            {
                try
                {
                    //反射实现接口实例
                    resultEntity = (IEntity)Assembly.Load(ioc.Namespace).CreateInstance(ioc.FullName);
                }
                catch (Exception ex)
                {

                    throw;
                }


            }
            return resultEntity;
        }

    }
 
程序使用:
image
 
image
原文地址:https://www.cnblogs.com/Fyhong/p/3761286.html