internal InjectionModel InjectSingleton(object formattedObject, Type referencedType) { var type = formattedObject.GetType(); var injectionModel = new InjectionModel(type, formattedObject, referencedType); InjectionModels.Add(injectionModel); return(injectionModel); }
internal InjectionModel InjectType(Type type) { var injectionModel = new InjectionModel( type: type, injectionMode: SpeckType.PerRequest); InjectionModels.Add(injectionModel); return(injectionModel); }
internal InjectionModel InjectSingleton(Type type) { var injectionModel = new InjectionModel( type: type, referencedType: type); InjectionModels.Add(injectionModel); return(injectionModel); }
internal object[] GetParameterInstances(ParameterInfo[] constructorParameters) { var parameterInstances = new List <object>(); foreach (var parameterInfo in constructorParameters) { if (parameterInfo.TryGetAttribute(out SpeckConfigurationAutoAttribute speckConfigurationAutoAttribute)) { var configInjectionModels = InjectionModels.Where(x => x.Type.GetAttribute <SpeckConfigurationAttribute>(att => att.Configuration == GlobalConfiguration.Profile) != null); object configInstance = null; foreach (var configInjectionModel in configInjectionModels) { foreach (var configProperty in configInjectionModel.Type.GetProperties(Constants.BindingFlags)) { if (configProperty.Name == speckConfigurationAutoAttribute.ConfigurationKey) { configInstance = configProperty.GetValue(configInjectionModel.Instance); break; } } if (configInstance != null) { continue; } foreach (var field in configInjectionModel.Type.GetFields(Constants.BindingFlags)) { if (field.Name == speckConfigurationAutoAttribute.ConfigurationKey) { configInstance = field.GetValue(configInjectionModel.Instance); break; } } } parameterInstances.Add(configInstance ?? throw new Exception($"{nameof(GetParameterInstances)} didn't find value for key {speckConfigurationAutoAttribute.ConfigurationKey}. Check the profile and configuration types to make sure the value was initialized.")); } else { var instance = (from injectionModel in InjectionModels where injectionModel.Type == parameterInfo.ParameterType || injectionModel.ReferencedType == parameterInfo.ParameterType select injectionModel.Instance) .FirstOrDefault(); parameterInstances.Add(instance ?? throw new Exception($"{nameof(GetParameterInstances)} didn't find instance for type {parameterInfo.ParameterType.Name}. Make sure a Speck exists of that type.")); } } return(parameterInstances.ToArray()); }
internal InjectionModel InjectSingleton(Type type, Type referencedType, object[] instances) { InjectionModel injectionModel = instances.Any() ? new InjectionModel( type: type, referencedType: referencedType, parameters: instances) : new InjectionModel( type: type, referencedType: referencedType); InjectionModels.Add(injectionModel); return(injectionModel); }
internal object GetInstance(Type type, bool throwable = true) { var injectionModel = InjectionModels.FirstOrDefault(model => model.Type == type || model.ReferencedType == type); switch (injectionModel?.InjectionMode) { case SpeckType.PerRequest: var newSpeck = Activator.CreateInstance(injectionModel.Type); return(newSpeck); case SpeckType.Singleton: return(injectionModel.Instance); default: if (throwable) { throw new Exception($"Type: {type.Name} not injected"); } else { return(null); } } }
/// <summary> /// Looks to see if Specky has the speck type injected. Useful to prevent initializing specks of type PerRequest /// </summary> /// <param name="type">Speck Type to look up</param> /// <returns></returns> public bool HasSpeck(Type type) { return(InjectionModels.FirstOrDefault(model => model.Type == type || model.ReferencedType == type) != null); }