/// <inheritdoc /> public IBindingData BindSingleton(string serviceName, Type implType) { GuardNotDisposingOrDisposed(); Guard.RequireNotNullOrEmpty <ArgumentException>(serviceName, $"Invalid '{nameof(serviceName)}'."); GuardUnbound(Dealias(serviceName)); GuardImplType(implType); var bindingData = new BindingData(this) { ServiceName = serviceName, ImplType = implType, LifeCycleManaged = true, }; m_ServiceNameToBindingDataMap[serviceName] = bindingData; return(bindingData); }
internal object MakeInternal(BindingData bindingData) { object serviceInstance; try { serviceInstance = ResolveInternal(bindingData); InitServices(); } finally { m_BindingDatasToBuild.Clear(); m_ServicesToInit.Clear(); } return(serviceInstance); }
/// <inheritdoc /> public IBindingData BindSingleton(string serviceName, Type implType, params PropertyInjection[] propertyInjections) { GuardNotDisposingOrDisposed(); Guard.RequireNotNullOrEmpty <ArgumentException>(serviceName, $"Invalid '{nameof(serviceName)}'."); GuardUnbound(Dealias(serviceName)); GuardImplType(implType); var bindingData = new BindingData(this) { ServiceName = serviceName, ImplType = implType, LifeCycleManaged = true, }; int propertyInjectionIndex = 0; foreach (var propertyInjection in propertyInjections) { if (string.IsNullOrEmpty(propertyInjection.PropertyName)) { throw new ArgumentException($"Property injection {propertyInjectionIndex} has a invalid property name."); } if (propertyInjection.Value == null) { throw new ArgumentException($"Property injection {propertyInjectionIndex} has a null value."); } var propertyInfo = implType.GetProperty(propertyInjection.PropertyName, BindingFlags.Public | BindingFlags.Instance | BindingFlags.SetProperty); if (propertyInfo == null) { throw new ArgumentException($"Cannot find property named '{propertyInjection.PropertyName}' at index {propertyInjectionIndex}."); } if (!propertyInfo.PropertyType.IsInstanceOfType(propertyInjection.Value)) { throw new ArgumentException($"Property injection {propertyInjectionIndex} has a value that doesn't have a feasible type."); } bindingData.AddPropertyInjection(propertyInjection); propertyInjectionIndex++; } m_ServiceNameToBindingDataMap[serviceName] = bindingData; return(bindingData); }
/// <inheritdoc /> public IBindingData BindInstance(string serviceName, object instance) { GuardNotDisposingOrDisposed(); Guard.RequireNotNullOrEmpty <ArgumentException>(serviceName, $"Invalid '{nameof(serviceName)}'."); GuardUnbound(Dealias(serviceName)); var implType = instance.GetType(); Guard.RequireNotNull <ArgumentNullException>(instance, $"Invalid '{nameof(instance)}'."); var bindingData = new BindingData(this) { ServiceName = serviceName, ImplType = implType, LifeCycleManaged = false, }; m_ServiceNameToBindingDataMap[serviceName] = bindingData; m_ServiceNameToSingletonMap[serviceName] = instance; return(bindingData); }
private object ResolveInternal(BindingData bindingData) { if (m_BindingDatasToBuild.Contains(bindingData)) { throw new InvalidOperationException("Cyclic dependencies are not supported."); } m_BindingDatasToBuild.Push(bindingData); var instanceType = bindingData.ImplType; if (!m_ServiceNameToSingletonMap.TryGetValue(bindingData.ServiceName, out object ret)) { ret = Activator.CreateInstance(instanceType); foreach (var property in instanceType.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.SetProperty)) { if (bindingData.PropertyInjections != null && bindingData.PropertyInjections.TryGetValue(property.Name, out var value)) { property.SetValue(ret, value); continue; } if (property.GetCustomAttribute <InjectAttribute>() == null) { continue; } var dependency = ResolveInternal((BindingData)GetBindingData(property.PropertyType)); property.SetValue(ret, dependency); } m_ServiceNameToSingletonMap[bindingData.ServiceName] = ret; m_ServicesToInit.Enqueue(bindingData.ServiceName); OnInstanceCreated?.Invoke(bindingData, ret); } m_BindingDatasToBuild.Pop(); return(ret); }