using Autofac;
using Autofac.Integration.Mvc;
using CommonClass.Factory;
using Autofac.Integration;

namespace Cmdjy
{
    /// <summary>
    /// IOC框架
    /// </summary>
    public class IOC
    {
        private static IOCFactory _iocFactory = null;
        private static object _objLock = new object();
        /// <summary>
        /// 控制反转容器工厂
        /// </summary>
        public static ILifetimeScope Factory {
            get {
                if(_iocFactory == null) {
                    lock(_objLock) {
                        if(_iocFactory == null) {
                            _iocFactory = new IOCFactory();
                            _iocFactory.BeforeBuild += _iocFactory_BeforeBuild;
                            _iocFactory.Init();
                        }
                    }
                }
                return _iocFactory.Container.BeginLifetimeScope();
            }
        }

        private static void _iocFactory_BeforeBuild(IOCFactory arg1,ContainerBuilder arg2) {
            //注册控制器
            arg2.RegisterControllers(typeof(IOC).Assembly).PropertiesAutowired();
            //注入特性
            arg2.RegisterFilterProvider();
            //注册模型绑定
            arg2.RegisterModelBinderProvider();
        }

    }
}