首页 > 代码库 > 接口注入与反转

接口注入与反转

抽出要工具类:

/*
* 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  依赖注入

 

接口注入与反转