/// <summary> /// Does actual binding of target type. /// </summary> /// <param name="kernel">Kernel for the binding</param> /// <param name="targetType">Type that is bound</param> /// <param name="attribute">Attribute from that type that will be used for that binding</param> /// <param name="activeProfiles">List of active profiles or null if no profiles mode</param> protected virtual void DoBinding(IKernel kernel, Type targetType, InjectableAttribute attribute, string[] activeProfiles) { if (activeProfiles != null) { // If none of profiles match current active profiles then we don't bind that type if (attribute.Profiles != null) { bool foundMatch = false; foreach (string profile in attribute.Profiles) { if (activeProfiles.Contains(profile)) { foundMatch = true; break; } } if (!foundMatch) { return; } } // If any of excluded profiles match one of current active profiles then we don't bind that type if (attribute.ExcludeInProfiles != null) { foreach (string profile in attribute.ExcludeInProfiles) { if (activeProfiles.Contains(profile)) { return; } } } } var binding0 = !kernel.GetBindings(targetType).Any() ? kernel.Bind(targetType).To(targetType) : null; if (binding0 != null) { var binding1 = DoScopeConfiguration(attribute, binding0); if (!attribute.IgnoreDisposable && typeof(IDisposable).IsAssignableFrom(targetType)) { binding1.OnDeactivation(x => ((IDisposable)x).Dispose()); } } if (attribute.Interface == null) { return; } kernel.Bind(attribute.Interface).ToMethod(ctx => ctx.Kernel.Get(targetType)); }
protected virtual IBindingNamedWithOrOnSyntax <object> DoScopeConfiguration(InjectableAttribute attribute, IBindingWhenInNamedWithOrOnSyntax <object> binding) { switch (attribute.Scope) { default: case InjectionScope.Transient: return(binding.InTransientScope()); case InjectionScope.Singleton: return(binding.InSingletonScope()); case InjectionScope.Thread: return(binding.InThreadScope()); case InjectionScope.Call: return(binding.InCallScope()); case InjectionScope.Named: return(binding.InNamedScope(attribute.ScopeName)); case InjectionScope.Parent: return(binding.InParentScope()); } }