Example #1
0
        private static void EnsureConstructoryOrFactory <CType>(IBindingConfig bindingConfig) where CType : class
        {
            var cType = typeof(CType);

            // Do not trigger property change
            if (bindingConfig.FactoryExpression == null && bindingConfig.Constructor == null)
            {
                if (cType.IsInterface || cType.IsAbstract)
                {
                    // if we can not instantiate, set the resolver to throw an exception.
                    Expression <Func <CType> > throwEx = () => ThrowInterfaceException <CType> ();
                    bindingConfig.FactoryExpression = throwEx;
                }
                else
                {
                    // try to find the default constructor and create a default resolver from it
                    var ctor = cType.GetConstructors()
                               .OrderBy(v => Attribute.IsDefined(v, typeof(InjectAttribute)) ? 0 : 1)
                               .ThenBy(v => v.GetParameters().Count())
                               .FirstOrDefault();

                    if (ctor != null)
                    {
                        bindingConfig.Constructor = ctor;
                    }
                    else
                    {
                        Expression <Func <CType> > throwEx = () => ThrowConstructorException <CType> ();
                        bindingConfig.FactoryExpression = throwEx;
                    }
                }
            }
        }
Example #2
0
        private static void MergeBinding(IBindingConfig bindingConfig, IBindingConfig explicitBindingConfig)
        {
            // merge constructor
            if (explicitBindingConfig.Constructor != null)
            {
                bindingConfig.Constructor = explicitBindingConfig.Constructor;
            }

            // merge factory
            if (explicitBindingConfig.FactoryExpression != null)
            {
                bindingConfig.FactoryExpression = explicitBindingConfig.FactoryExpression;
            }

            // merge lifestyle
            if (explicitBindingConfig.Lifestyle != null)
            {
                bindingConfig.Lifestyle = explicitBindingConfig.Lifestyle;
            }

            foreach (var fis in explicitBindingConfig.GetFieldInfoSetters())
            {
                bindingConfig.SetFieldInfoSetter(fis.MemberInfo, fis.MemberSetter);
            }

            foreach (var pis in explicitBindingConfig.GetPropertyInfoSetters())
            {
                bindingConfig.SetPropertyInfoSetter(pis.MemberInfo, pis.MemberSetter);
            }
        }
Example #3
0
        /// <summary>
        /// Adds the property injector to binding config.
        /// </summary>
        /// <param name="bindingConfig">Binding config.</param>
        /// <param name="propertyExpression">Property expression.</param>
        /// <param name="setter">Setter.</param>
        /// <typeparam name="CType">The 1st type parameter.</typeparam>
        /// <typeparam name="TPropertyType">The 2nd type parameter.</typeparam>
        internal static void AddMemberInjectorToBindingConfig <CType, TPropertyType>(
            IBindingConfig bindingConfig,
            Expression <Func <CType, TPropertyType> > propertyExpression,
            Expression <Func <TPropertyType> > setter)
            where CType : class
        {
            var memberExpression = propertyExpression.Body as MemberExpression;

            if (memberExpression == null)
            {
                throw InjectorErrors.ErrorMustContainMemberExpression.FormatEx("memberExpression");
            }

            var member = memberExpression.Member;

            if (member is PropertyInfo)
            {
                bindingConfig.SetPropertyInfoSetter(member as PropertyInfo, setter);
            }
            else if (member is FieldInfo)
            {
                bindingConfig.SetFieldInfoSetter(member as FieldInfo, setter);
            }
            else
            {
                // Should not be reachable.
                throw InjectorErrors.ErrorMustContainMemberExpression.FormatEx("memberExpression");
            }
        }
Example #4
0
        private IResolver BindExplicit <BType, CType>(BindingKey bindingKey, IBindingConfig bindingConfig)
            where BType : class
            where CType : class, BType
        {
            lock (syncLock) {
                IResolver oldResolver;
                if (allResolvers.TryGetValue(bindingKey, out oldResolver))
                {
                    if (bindingKey.IsImplicit)
                    {
                        // Handle edge case where 2 thread auto-register same implicit
                        // binding via 'ResolveBinding'. More complex to add guard within
                        // ResolveBinding() than here.
                        return(oldResolver);
                    }
                    allResolvers.Remove(bindingKey);
                }

                // Add after create resolver
                var resolver = CreateResolverInstance <BType, CType> (bindingKey, bindingConfig);

                // Register Implicits
                implicitBindingResolver.Register(bindingKey);

                return(resolver);
            }
        }
Example #5
0
        public BindProject GetBindAction(IBindingConfig config, Project project, CancellationToken cancellationToken)
        {
            var bindingOperation = createBindingOperationFunc(project, config);

            bindingOperation.Initialize();
            bindingOperation.Prepare(cancellationToken);

            return(bindingOperation.Commit);
        }
Example #6
0
        public Resolver(Injector injector, IBindingConfig bindingConfig, object syncLock)
        {
            this.injector      = injector;
            this.bindingConfig = bindingConfig;
            this.syncLock      = syncLock;

            this.expressionCompiler = new ExpressionCompiler <CType> (
                bindingConfig,
                injector.ResolveResolverExpression);
        }
Example #7
0
        public async Task <IBindingConfig> GetConfigurationAsync(SonarQubeQualityProfile qualityProfile, Language language, BindingConfiguration bindingConfiguration, CancellationToken cancellationToken)
        {
            var provider = Providers.FirstOrDefault(p => p.IsLanguageSupported(language));

            if (provider == null)
            {
                throw new ArgumentOutOfRangeException(nameof(language));
            }
            IBindingConfig config = null;

            if (provider != null)
            {
                config = await provider?.GetConfigurationAsync(qualityProfile, language, bindingConfiguration, cancellationToken);
            }

            return(config);
        }
Example #8
0
        /// <summary>
        /// Clones the supplied explicit bindings and merges the settings together with implicit bindings.
        /// </summary>
        /// <returns>The with implicit settings.</returns>
        /// <param name="explicitBindingConfig">Explicit binding config.</param>
        /// <typeparam name="CType">The 1st type parameter.</typeparam>
        internal static IBindingConfig MergeImplicitWithExplicitSettings <CType>(IBindingConfig explicitBindingConfig) where CType : class
        {
            // setup implicits
            var  bindingConfig = new BindingConfig(typeof(CType));
            var  bindingFlags  = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance;
            Type cType         = typeof(CType);

            do
            {
                foreach (var prop in FilterMemberInfo <PropertyInfo>(cType, cType.GetProperties(bindingFlags)))
                {
                    bindingConfig.SetPropertyInfoSetter(prop, null);
                }

                foreach (var field in FilterMemberInfo <FieldInfo>(cType, cType.GetFields(bindingFlags)))
                {
                    bindingConfig.SetFieldInfoSetter(field, null);
                }
            } while ((cType = cType.BaseType) != null && cType != ObjectType);

            if (typeof(CType).GetCustomAttributes(typeof(SingletonAttribute), false).Any())
            {
                bindingConfig.Lifestyle = Lifestyle.Singleton;
            }

            MergeBinding(bindingConfig, explicitBindingConfig);
            EnsureConstructoryOrFactory <CType> (bindingConfig);

            // ensure lifestyle
            if (bindingConfig.Lifestyle == null)
            {
                bindingConfig.Lifestyle = Lifestyle.Transient;
            }

            return(bindingConfig);
        }
Example #9
0
        private Resolver <CType> CreateResolverInstance <BType, CType>(BindingKey bindingKey, IBindingConfig bindingConfig)
            where BType : class
            where CType : class, BType
        {
            if (bindingConfig == null)
            {
                bindingConfig = BindingConfigUtils.CreateImplicitBindingSettings <CType> ();
            }
            else
            {
                bindingConfig = BindingConfigUtils.MergeImplicitWithExplicitSettings <CType> (bindingConfig);
            }

            var resolver = new Resolver <CType> (this, bindingConfig, syncLock);

            allResolvers.Add(bindingKey, resolver);

            return(resolver);
        }
        public BindProject GetBindAction(IBindingConfig config, Project project, CancellationToken cancellationToken)
        {
            logger.WriteLine(Strings.Bind_Project_NotRequired, project.FullName);

            return(() => { });
        }
		/// <summary>
		/// Gets the generic implementation.
		/// </summary>
		/// <returns>The generic implementation.</returns>
		/// <param name="genericBindingKey">Generic binding key.</param>
		/// <param name="genericBindingType">Generic binding type.</param>
		/// <param name="genericBindingConfig">Generic binding config.</param>
		private Type GetGenericImplementation (BindingKey genericBindingKey, Type genericBindingType, out IBindingConfig genericBindingConfig) {
			genericBindingConfig = null;

			// Try registrations
			if (allGenericResolvers.TryGetValue (genericBindingKey, out genericBindingConfig)) {
				return genericBindingConfig.ConcreteType;
			}

			// Try implicit
			return BindingAttributeUtils.GetImplementedBy (genericBindingType);
		}
Example #12
0
 public RemoraHostConfig()
 {
     ServiceConfig = new ServiceConfig();
     BindingConfigs = new IBindingConfig[0];
     JobsConfig = new JobsConfig();
 }
Example #13
0
 internal ExpressionCompiler(IBindingConfig bindingConfig, ResolveResolverExpression resolveResolverExpression)
 {
     this.bindingConfig             = bindingConfig;
     this.resolveResolverExpression = resolveResolverExpression;
 }
Example #14
0
 public DummyProvider(IBindingConfig configToReturn = null, params Language[] supportedLanguages)
 {
     SupportedLanguages  = new List <Language>(supportedLanguages);
     this.ConfigToReturn = configToReturn;
 }
Example #15
0
 public TinyMapperObjectMapperBindingConfig(IBindingConfig <TFrom, TTo> tinyMapperBindingConfig)
 {
     this.tinyMapperBindingConfig = tinyMapperBindingConfig;
 }