public static void 注入 <T, T1>(bool __单实例 = true, bool __拦截 = true, string __名称 = null, object[] __构造参数 = null) where T1 : T
 {
     if (__单实例)
     {
         if (string.IsNullOrEmpty(__名称))
         {
             if (__构造参数 == null)
             {
                 _IUnityContainer.RegisterType <T, T1>(new ContainerControlledLifetimeManager());
             }
             else
             {
                 _IUnityContainer.RegisterType <T, T1>(new ContainerControlledLifetimeManager(), new InjectionConstructor(__构造参数));
             }
         }
         else
         {
             if (__构造参数 == null)
             {
                 _IUnityContainer.RegisterType <T, T1>(__名称, new ContainerControlledLifetimeManager());
             }
             else
             {
                 _IUnityContainer.RegisterType <T, T1>(__名称, new ContainerControlledLifetimeManager(), new InjectionConstructor(__构造参数));
             }
         }
     }
     else
     {
         if (string.IsNullOrEmpty(__名称))
         {
             if (__构造参数 == null)
             {
                 _IUnityContainer.RegisterType <T, T1>();
             }
             else
             {
                 _IUnityContainer.RegisterType <T, T1>(new InjectionConstructor(__构造参数));
             }
         }
         else
         {
             if (__构造参数 == null)
             {
                 _IUnityContainer.RegisterType <T, T1>(__名称);
             }
             else
             {
                 _IUnityContainer.RegisterType <T, T1>(__名称, new InjectionConstructor(__构造参数));
             }
         }
     }
     if (__拦截 && typeof(T).IsPublic)
     {
         _Interception.SetInterceptorFor <T>(new InterfaceInterceptor());//InterfaceInterceptor,TransparentProxyInterceptor,VirtualMethodInterceptor
         //_PolicyDefinition.AddMatchingRule<TypeMatchingRule>(new InjectionConstructor(typeof(T1).FullName, true));
     }
 }
Ejemplo n.º 2
0
        public void AddInterceptor(string policy, IMatchingRule matchingRule, params Type[] classTypes)
        {
            Interception interception = uContainer.Configure <Interception>();

            foreach (Type classType in classTypes)
            {
                interception = interception.SetInterceptorFor(classType, new VirtualMethodInterceptor());
            }
            interception.AddPolicy(policy)
            .AddMatchingRule(matchingRule);
        }
Ejemplo n.º 3
0
        public static void Initialize()
        {
            #region Neo4j

            _container.RegisterType <NeoServerConfiguration>(new ContainerControlledLifetimeManager(), new InjectionFactory(o => NeoServerConfiguration.GetConfiguration(new Uri(Settings.Default.Neo4jServerUrl), Settings.Default.Neo4jUserName, Settings.Default.Neo4jPassword)));

            _container.RegisterType <IGraphClientFactory, GraphClientFactory>(new ContainerControlledLifetimeManager());

            _container.RegisterType <IUnitOfWorkFactory, UnitOfWorkFactory>(new ContainerControlledLifetimeManager());

            #endregion

            #region NHibernate

            _container.RegisterInstance(NHibernateConfiguration.SessionFactory, new ContainerControlledLifetimeManager());

            #endregion

            #region AOP

            #region Call Handlers

            _container.RegisterType <ICallHandler, LoggingCallHandler>(typeof(LoggingCallHandler).Name, new ContainerControlledLifetimeManager(), new InjectionProperty(nameof(LoggingCallHandler.Order), 1));

            _container.RegisterType <ICallHandler, TransactionCallHandler>(typeof(TransactionCallHandler).Name, new ContainerControlledLifetimeManager(), new InjectionProperty(nameof(TransactionCallHandler.Order), 2));

            #endregion

            #region Controllers Interception

            _container.AddNewExtension <Interception>();

            Interception interception = _container.Configure <Interception>();

            Assembly.GetExecutingAssembly().GetTypes().Where(o => o.IsSubclassOf(typeof(ApiController))).ToList().ForEach(o => interception.SetInterceptorFor(o, new VirtualMethodInterceptor()));

            #endregion

            #endregion

            #region DAOs

            _container.RegisterType <IUserDao>(new ContainerControlledLifetimeManager(), new InjectionFactory(o =>
            {
                var dao = new UserDao();

                dao.SetSessionFactory(NHibernateConfiguration.SessionFactory);

                return(dao);
            }));

            #endregion

            #region Validators

            _container.RegisterType <CreateHotSpotInputDtoValidator>(new ContainerControlledLifetimeManager());

            _container.RegisterType <CreateRefugeeInputDtoValidator>(new ContainerControlledLifetimeManager());

            _container.RegisterType <UpdateRefugeeInputDtoValidator>(new ContainerControlledLifetimeManager());

            _container.RegisterType <UpdateRefugeeInputDtoValidator>(new ContainerControlledLifetimeManager());

            _container.RegisterType <AuthenticationInputDtoValidator>(new ContainerControlledLifetimeManager());

            _container.RegisterType <CreateRefugeesFamilyRelationshipInputDtoValidator>(new ContainerControlledLifetimeManager());

            #endregion

            #region Utilities

            _container.RegisterType <ICurrentHttpRequest, CurrentHttpRequest>(new ContainerControlledLifetimeManager());

            #endregion
        }