private BackgroundTaskHandler GetTaskHandler(Type concreteType) { var backgroundTaskHandler = Activator.CreateInstance(concreteType) as BackgroundTaskHandler; //Get the right configuration subType and object TConfig config = vaultApplication.GetConfig(); Type subConfigType = concreteType.BaseType.GenericTypeArguments[0]; object subConfig = Dispatcher_Helpers.GetConfigSubProperty(config, subConfigType); //Set the configuration var configProperty = backgroundTaskHandler.GetType().GetProperty(nameof(IBackgroundTaskHandler <object, EmptyTaskQueueDirective> .Configuration)); configProperty.SetValue(backgroundTaskHandler, subConfig); //Set the configuration independent variables backgroundTaskHandler.PermanentVault = vaultApplication.PermanentVault; backgroundTaskHandler.OnDemandBackgroundOperations = vaultApplication.OnDemandBackgroundOperations; backgroundTaskHandler.RecurringBackgroundOperations = vaultApplication.RecurringBackgroundOperations; if (vaultApplication.ValidationResults.TryGetValue(subConfigType, out ValidationResults results)) { backgroundTaskHandler.ValidationResults = results; } else { backgroundTaskHandler.ValidationResults = null; } return(backgroundTaskHandler); }
public void Test_AreSubConfigProperties_Test2() { bool[] expected = new bool[] { true, false }; var results = Dispatcher_Helpers.AreConfigSubProperties(typeof(Configuration), typeof(Child_Configuration), typeof(VaultApplication <object>)); for (int i = 0; i < results.Length; i++) { Assert.AreEqual(expected[i], results[i]); } }
protected internal override IEnumerable <ValidationFinding> HandleConcreteTypes(IEnumerable <Type> concreteValidators, params ICtrlVAFCommand[] commands) { if (!concreteValidators.Any()) { return(new ValidationFinding[0]); } if (!commands.Any()) { return(new ValidationFinding[0]); } List <ValidationFinding> findings = new List <ValidationFinding>(); foreach (Type concreteValidatorType in concreteValidators) { var subConfigType = concreteValidatorType.BaseType.GenericTypeArguments[0]; var concreteHandler = Activator.CreateInstance(concreteValidatorType) as CustomValidator; //Set the configuration var configProperty = concreteValidatorType .GetProperty(nameof(ICustomValidator <object, ValidationCommand> .Configuration)); var subConfig = Dispatcher_Helpers.GetConfigSubProperty(vaultApplication.GetConfig(), subConfigType); configProperty.SetValue(concreteHandler, subConfig); //Set the configuration independent variables concreteHandler.PermanentVault = vaultApplication.PermanentVault; concreteHandler.OnDemandBackgroundOperations = vaultApplication.OnDemandBackgroundOperations; concreteHandler.RecurringBackgroundOperations = vaultApplication.RecurringBackgroundOperations; var validateMethod = concreteValidatorType.GetMethod(nameof(ICustomValidator <object, ValidationCommand> .Validate)); ValidationFinding[] results = new ValidationFinding[0]; try { var validatorCommand = commands.FirstOrDefault(cmd => cmd.GetType() == concreteValidatorType.BaseType.GenericTypeArguments[1]); results = (validateMethod.Invoke(concreteHandler, new object[] { validatorCommand }) as IEnumerable <ValidationFinding>).ToArray(); findings.AddRange(results); } catch (TargetInvocationException te) { ExceptionDispatchInfo.Capture(te.InnerException).Throw(); } catch (Exception e) { throw e; } if (!vaultApplication.ValidationResults.TryAdd(subConfigType, new ValidationResults(results))) { vaultApplication.ValidationResults[subConfigType].AddResults(results); } } return(findings); }
protected internal override void HandleConcreteTypes(IEnumerable <Type> types, params ICtrlVAFCommand[] commands) { // If none, return if (!types.Any()) { return; } List <Type> handledTypes = new List <Type>(); foreach (ICtrlVAFCommand command in commands) { var commandType = command.GetType(); if (TypeCache.TryGetValue(commandType, out IEnumerable <Type> concreteHandlerTypes)) { foreach (Type concreteHandlerType in concreteHandlerTypes) { if (handledTypes.Contains(concreteHandlerType) || !types.Contains(concreteHandlerType)) { continue; } var concreteHandler = Activator.CreateInstance(concreteHandlerType) as Handlers.EventHandler; var subConfigType = concreteHandlerType.BaseType.GenericTypeArguments[0]; //Set the configuration var configProperty = concreteHandlerType.GetProperty(nameof(ICommandHandler <object> .Configuration)); var subConfig = Dispatcher_Helpers.GetConfigSubProperty(vaultApplication.GetConfig(), subConfigType); configProperty.SetValue(concreteHandler, subConfig); //Set the configuration independent variables concreteHandler.PermanentVault = vaultApplication.PermanentVault; concreteHandler.OnDemandBackgroundOperations = vaultApplication.OnDemandBackgroundOperations; concreteHandler.RecurringBackgroundOperations = vaultApplication.RecurringBackgroundOperations; var keys = vaultApplication.ValidationResults.Keys.ToArray(); var found = Dispatcher_Helpers.AreConfigSubProperties(subConfigType, keys); for (int i = 0; i < keys.Length; i++) { if (found[i]) { concreteHandler.ValidationResults.AddResults(vaultApplication.ValidationResults[keys[i]]); } } var handleMethod = concreteHandlerType .GetMethod(nameof(EventHandler <object, EventCommand> .Handle), new Type[] { commandType }); try { handleMethod.Invoke(concreteHandler, new object[] { command }); } catch (TargetInvocationException te) { ExceptionDispatchInfo.Capture(te.InnerException).Throw(); } catch (Exception e) { throw e; } handledTypes.Add(concreteHandlerType); } } } return; }
public void Test_AreSubConfigProperties_Test1() { var results = Dispatcher_Helpers.AreConfigSubProperties(typeof(Configuration), typeof(Configuration), typeof(Child_Configuration), typeof(GrandChild_Configuration)); Assert.IsTrue(!results.Contains(false)); }