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; } } } }
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); } }
/// <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"); } }
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); } }
public BindProject GetBindAction(IBindingConfig config, Project project, CancellationToken cancellationToken) { var bindingOperation = createBindingOperationFunc(project, config); bindingOperation.Initialize(); bindingOperation.Prepare(cancellationToken); return(bindingOperation.Commit); }
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); }
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); }
/// <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); }
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); }
public RemoraHostConfig() { ServiceConfig = new ServiceConfig(); BindingConfigs = new IBindingConfig[0]; JobsConfig = new JobsConfig(); }
internal ExpressionCompiler(IBindingConfig bindingConfig, ResolveResolverExpression resolveResolverExpression) { this.bindingConfig = bindingConfig; this.resolveResolverExpression = resolveResolverExpression; }
public DummyProvider(IBindingConfig configToReturn = null, params Language[] supportedLanguages) { SupportedLanguages = new List <Language>(supportedLanguages); this.ConfigToReturn = configToReturn; }
public TinyMapperObjectMapperBindingConfig(IBindingConfig <TFrom, TTo> tinyMapperBindingConfig) { this.tinyMapperBindingConfig = tinyMapperBindingConfig; }