public static void Save(IAutoConfig config) { if (config == null) { throw new ArgumentNullException(nameof(config)); } var genericConfigs = GetConfigDescription(config.GetType()).CreateGenericConfigs(config); GetService().AddOrUpdate(genericConfigs); }
public RamseyAuto(IAutoConfig autoConfig, IWordRemover illegalRemover) { _client = new HemmetsHttpClient(); _illegalRemover = illegalRemover; Config = autoConfig; Logger = new LoggerConfiguration() .MinimumLevel.Verbose() .WriteTo.Trace() .WriteTo.Console(standardErrorFromLevel: LogEventLevel.Error) .CreateLogger(); }
public GenericConfig MakeGenericConfig(IAutoConfig instanse) { if (instanse == null) { throw new ArgumentNullException(nameof(instanse)); } GenericConfig result = new GenericConfig() { AppName = AppName, Key = PropertyAttribute.Key, Value = JsonConvert.SerializeObject(GetValue(instanse)), LastUpdated = DateTime.Now }; return(result); }
public void SetValueFromGenericConfig(IAutoConfig instanse, GenericConfig config) { if (instanse == null) { throw new ArgumentNullException(nameof(instanse)); } if (config == null) { throw new ArgumentNullException(nameof(config)); } if (config.AppName != AppName || config.Key != PropertyAttribute.Key) { throw new ConfigNotMatchException($"config is not for app {AppName} and key {PropertyAttribute.Key}"); } SetValue(instanse, JsonConvert.DeserializeObject(config.Value, Property.PropertyType)); }
public object GetValue(IAutoConfig instanse) { if (instanse == null) { throw new ArgumentNullException(nameof(instanse)); } if (_getValueFunction == null) { var parameterExpression = Expression.Parameter(typeof(IAutoConfig)); var convertTypeExpression = Expression.Convert(parameterExpression, ConfigType); var propertyExpression = Expression.Property(convertTypeExpression, Property); var resultConvertExpression = Expression.Convert(propertyExpression, typeof(object)); var lambdaExpression = Expression.Lambda <Func <IAutoConfig, object> >(resultConvertExpression, parameterExpression); _getValueFunction = lambdaExpression.Compile(); } return(_getValueFunction(instanse)); }
public IAutoConfig CreateAutoConfig(IEnumerable <GenericConfig> genericConfigs) { if (genericConfigs == null) { throw new ArgumentNullException(nameof(genericConfigs)); } IAutoConfig result = GetCreator()(); foreach (var item in Properties) { var findGenericConfig = genericConfigs.FirstOrDefault(e => e.AppName == item.AppName && e.Key == item.PropertyAttribute.Key); if (findGenericConfig != null) { item.SetValueFromGenericConfig(result, findGenericConfig); } } return(result); }
public IEnumerable <GenericConfig> CreateGenericConfigs(IAutoConfig config) { if (config == null) { throw new ArgumentNullException(nameof(config)); } if (config.GetType() != ConfigType) { throw new ArgumentException($"config must with type {ConfigType.Name}"); } IList <GenericConfig> list = new List <GenericConfig>(); foreach (var item in Properties) { list.Add(item.MakeGenericConfig(config)); } return(list); }
public void SetValue(IAutoConfig instanse, object value) { if (instanse == null) { throw new ArgumentNullException(nameof(instanse)); } if (value == null) { throw new ArgumentNullException(nameof(value)); } if (_setValueAction == null) { var instanseParameterExpression = Expression.Parameter(typeof(IAutoConfig)); var valueParameterExpression = Expression.Parameter(typeof(object)); var convertTypeExpression = Expression.Convert(instanseParameterExpression, ConfigType); var convertValueExpression = Expression.Convert(valueParameterExpression, Property.PropertyType); var propertyExpression = Expression.Property(convertTypeExpression, Property); var propertyAssginExpression = Expression.Assign(propertyExpression, convertValueExpression); var lambdaExpression = Expression.Lambda <Action <IAutoConfig, object> >(propertyAssginExpression, instanseParameterExpression, valueParameterExpression); _setValueAction = lambdaExpression.Compile(); } _setValueAction(instanse, value); }