接口注入与反转

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了接口注入与反转相关的知识,希望对你有一定的参考价值。

抽出要工具类:

/*
* Assembly: Proj.ServiceContainer
* Class      : ContainerImpl CLR.v4.0.30319.42000 
* ─────────────────────────────────────────────────────────────
* Created : yansixing 2017/3/8 10:37:59
* Copyright (c) 2017  All rights reserved.
* ─────────────────────────────────────────────────────────────
*/

using System.Collections.Generic;


namespace Proj.ServiceContainer
{
    /// <summary>
    /// Class ContainerImpl.实现注入
    /// </summary>
    /// <seealso cref="ExFresh.PSS.OrderService.ServiceContainer.IContainer" />
    public class ContainerImpl : IContainer
    {
        public static Dictionary<string, object> _container { get; set; }

        static ContainerImpl()
        {
            _container = new Dictionary<string, object>();
        }
       
        /// <summary>
        /// Registers the specified t class.
        /// </summary>
        /// <typeparam name="TClass">The type of the t class.</typeparam>
        /// <typeparam name="TInterface">The type of the t interface.</typeparam>
        /// <param name="tClass">The t class.</param>
        public void Register<TClass, TInterface>(TClass tClass) where TClass : class, TInterface
        {
            _container.Add(typeof(TInterface).FullName, tClass);
        }

        /// <summary>
        /// Resolves this instance.
        /// </summary>
        /// <typeparam name="TInterface">The type of the t interface.</typeparam>
        /// <returns>TInterface.</returns>
        public TInterface Resolve<TInterface>()
        {
            TInterface tInterface = default(TInterface);
            object obj;
            if (_container.TryGetValue(typeof(TInterface).FullName, out obj))
            {
                tInterface = (TInterface)obj;
            }
            return tInterface;
        }

        /// <summary>
        /// Tries the resolve.
        /// </summary>
        /// <typeparam name="TInterface">The type of the t interface.</typeparam>
        /// <param name="tInterface">The t interface.</param>
        /// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns>
        public bool TryResolve<TInterface>(out TInterface tInterface)
        {
            object obj;
            bool flag = false;
            tInterface = default(TInterface);
            if (_container.TryGetValue(typeof(TInterface).FullName, out obj))
            {
                flag = true;
                tInterface = (TInterface)obj;
            }
            return flag;
        } 
    }
}
namespace Proj.ServiceContainer
{
    public interface IContainer
    {       
        void Register<TClass, TInterface>(TClass tClass) where TClass : class, TInterface;
        TInterface Resolve<TInterface>();
        bool TryResolve<TInterface>(out TInterface tInterface);
    }
}
/*
* Assembly: Proj.ServiceContainer
* Class      : ContainerFactory CLR.v4.0.30319.42000 
* ─────────────────────────────────────────────────────────────
* Created : yansixing 2017/3/8 10:52:04
* Copyright (c) 2017  All rights reserved.
* ─────────────────────────────────────────────────────────────
*/

namespace Proj.ServiceContainer
{
    public class ContainerFactory
    {
        private static IContainer _container;
        public static IContainer GetContainer()
        {
            if (_container == null)
            {
                _container = new ContainerImpl();
            }
            return _container;
        }
    }
}

在表现层加一个范型基类,实现接口反转:

namespace Proj.API
{
    public class CommonApi<T>
    {
        protected TInterface Resolve<TInterface>()
        {
            return ContainerFactory.GetContainer().Resolve<TInterface>();
        }

        protected T ServiceImpl { get; set; }

        public CommonApi()
        {
            ServiceImpl = ContainerFactory.GetContainer().Resolve<T>();
        }
        private IMapper _mapper;
        public IMapper Mapper
        {
            get
            {
                if (null == _mapper)
                {
                    _mapper = AssemblerIoc.GetMapper();
                }
                return _mapper;
            }
        }
    }

在每个API继承该范型基类:

namespace Proj.API
{    
    public class OrderPrintApi:CommonApi<IOrderPrintService>
}

在项目启动地方一次装载:

namespace Proj.ContainerInit
{
    internal class Container
    {
        internal static void Configer(ContainerImpl container)
        {
            #region 订单服务
            //订单打印
            container.Register<OrderPrintService, IOrderPrintService>(new OrderPrintService());

            #endregion

            #region 库存服务
            #endregion
        }
    }
}
//启动容器初始化
            Container.Configer(new ContainerImpl());

 

 

参考:  http://www.cnblogs.com/qqlin/archive/2012/10/09/2707075.html  依赖注入

 

以上是关于接口注入与反转的主要内容,如果未能解决你的问题,请参考以下文章

C#依赖注入控制反转IOC实现详解

控制反转和依赖注入的关系总结

通俗简述 依赖倒置?控制反转?依赖注入?面向接口编程 的思想

spring依赖注入,和控制反转。用自己的话说是怎么说的。最好能够用代码来解释

javaweb-控制反转与依赖注入

轻松了解Spring中的控制反转和依赖注入