/// <summary> /// Creates a Stump contract from a Stump data entity. /// </summary> /// <param name="serverId">The unique identifier for the server.</param> /// <param name="entity">The <see cref="StumpEntity"/> used to create the contract.</param> /// <param name="dataAccess">The data access provider used by the instance.</param> /// <returns> /// A <see cref="StumpContract"/> created from the specified <paramref name="entity"/>. /// </returns> public static StumpContract CreateContractFromEntity(string serverId, StumpEntity entity, IDataAccess dataAccess) { var contract = new StumpContract { OriginalRequest = new RecordedRequest(new HttpRequestEntityReader(serverId, entity.OriginalRequest, dataAccess), ContentDecoderHandling.DecodeNotRequired), OriginalResponse = new RecordedResponse(new HttpResponseEntityReader(serverId, entity.OriginalResponse, dataAccess), ContentDecoderHandling.DecodeNotRequired), Response = new RecordedResponse(new HttpResponseEntityReader(serverId, entity.Response, dataAccess), ContentDecoderHandling.DecodeNotRequired), Rules = new RuleContractCollection(), StumpCategory = entity.StumpName, StumpId = entity.StumpId, StumpName = entity.StumpName, }; foreach (var ruleEntity in entity.Rules) { var rule = new RuleContract { RuleName = ruleEntity.RuleName }; foreach (var value in ruleEntity.Settings) { var setting = new RuleSetting { Name = value.Name, Value = value.Value }; rule.AppendRuleSetting(setting); } contract.Rules.Add(rule); } return(contract); }
/// <summary> /// Creates a <see cref="IStumpRule"/> from a <see cref="RuleContract"/>. /// </summary> /// <param name="contract">The <see cref="RuleContract"/> used to create the <see cref="IStumpRule"/>.</param> /// <returns>A <see cref="IStumpRule"/> object created from the specified <paramref name="contract"/>.</returns> /// <exception cref="ArgumentNullException"><paramref name="contract"/> is <c>null</c>.</exception> public static IStumpRule CreateRuleFromContract(RuleContract contract) { contract = contract ?? throw new ArgumentNullException(nameof(contract)); var type = KnownRules[contract.RuleName]; var rule = Activator.CreateInstance(type) as IStumpRule; rule.InitializeFromSettings(contract.GetRuleSettings()); return(rule); }
/// <summary> /// Creates an object based on an <see cref="IStumpRule"/> from a <see cref="RuleContract"/>. /// </summary> /// <typeparam name="T">The concrete implementation of the <see cref="IStumpRule"/> rule to create.</typeparam> /// <param name="contract">The <see cref="RuleContract"/> used to create the <see cref="IStumpRule"/>.</param> /// <returns>A <see cref="IStumpRule"/> object created from the specified <paramref name="contract"/>.</returns> /// <exception cref="ArgumentNullException"><paramref name="contract"/> is <c>null</c>.</exception> public static T CreateRuleFromContract <T>(RuleContract contract) where T : IStumpRule, new() { contract = contract ?? throw new ArgumentNullException(nameof(contract)); var type = KnownRules[contract.RuleName]; var rule = Activator.CreateInstance(type) as IStumpRule; if (rule is T) { rule.InitializeFromSettings(contract.GetRuleSettings()); } else { return((T)Activator.CreateInstance(typeof(T))); } return((T)rule); }
/// <summary> /// Creates a Stump contract from a Stump data entity. /// </summary> /// <param name="serverId">The unique identifier for the server.</param> /// <param name="entity">The <see cref="T:Stumps.Server.Data.StumpEntity"/> used to create the contract.</param> /// <param name="dataAccess">The data access provider used by the instance.</param> /// <returns> /// A <see cref="T:Stumps.Server.StumpContract"/> created from the specified <paramref name="entity"/>. /// </returns> public static StumpContract CreateContractFromEntity(string serverId, StumpEntity entity, IDataAccess dataAccess) { var contract = new StumpContract { OriginalRequest = new RecordedRequest(new HttpRequestEntityReader(serverId, entity.OriginalRequest, dataAccess), ContentDecoderHandling.DecodeNotRequired), OriginalResponse = new RecordedResponse(new HttpResponseEntityReader(serverId, entity.OriginalResponse, dataAccess), ContentDecoderHandling.DecodeNotRequired), Response = new RecordedResponse(new HttpResponseEntityReader(serverId, entity.Response, dataAccess), ContentDecoderHandling.DecodeNotRequired), ResponseDelay = entity.ResponseDelay, Rules = new RuleContractCollection(), StumpCategory = entity.StumpName, StumpId = entity.StumpId, StumpName = entity.StumpName, TerminateConnection = entity.TerminateConnection }; foreach (var ruleEntity in entity.Rules) { var rule = new RuleContract { RuleName = ruleEntity.RuleName }; foreach (var value in ruleEntity.Settings) { var setting = new RuleSetting { Name = value.Name, Value = value.Value }; rule.AppendRuleSetting(setting); } contract.Rules.Add(rule); } return contract; }