/// <summary> /// Indicates whether or not this setting type matches the specified member /// </summary> /// <param name="member">The property to evaluate</param> /// <returns>True if a match, otehrwise false</returns> internal virtual bool DoWork(SettingTypeModel model) { if (Next != null) { return Next.DoWork(model); } return false; }
internal override bool DoWork(SettingTypeModel model) { if (!IsCollection(model.Member)) { return base.DoWork(model); } var args = model.Member.PropertyType.GetGenericArguments(); if (args.Length > 1) { Trace.TraceWarning($"Mash.AppSettings: Unsupported property type with {args.Length} generic parameters."); } Type itemType = args.First(); // Check to see if an instance already exists dynamic property = model.Member.GetGetMethod().Invoke(model.SettingsClass, null); if (property == null) { // No instance exists so create a List<T> Type listType = typeof(List<>) .GetGenericTypeDefinition() .MakeGenericType(itemType); dynamic instance = Activator.CreateInstance(listType); // Assign the instance to the class property model.Member.SetValue(model.SettingsClass, instance); property = instance; } var loadedValue = LoadValue(model); if (loadedValue == null) { return false; } foreach (var item in loadedValue.Split(_separators, StringSplitOptions.RemoveEmptyEntries)) { // There's a dynamic binding issue with non-public types. One fix is to cast to IList to ensure the call to Add succeeds // but that requires basing this feature off of IList<T> and not ICollection<T>. // This does not work for ICollection because it does not include the Add method (only ICollection<T> does) // http://stackoverflow.com/questions/15920844/system-collections-generic-ilistobject-does-not-contain-a-definition-for-ad property.Add(TypeParser.GetTypedValue(itemType, item.Trim())); } return true; }
internal override bool DoWork(SettingTypeModel model) { if (!IsSupportedConnectionStringsType(model.Member)) { return base.DoWork(model); } Trace.TraceInformation($"Mash.AppSettings: Loading all connection strings into [{model.Member.Name}]."); var connectionStrings = new Dictionary<string, string>(model.SettingLoader.GetConnectionStrings()); if (model.DevLoader != null) { foreach (var kvp in model.DevLoader.GetConnectionStrings()) { Trace.TraceInformation($"Mash.AppSettings: overriding {kvp.Key} connection string from dev settings"); connectionStrings[kvp.Key] = kvp.Value; } } model.Member.SetValue(model.SettingsClass, connectionStrings); return true; }
internal override bool DoWork(SettingTypeModel model) { // Check if the property is meant to load a specific connection string if (!IsConnectionStringSettingType(model.Member)) { return base.DoWork(model); } Trace.TraceInformation($"Mash.AppSettings: Loading connection string into [{model.Member.Name}]."); string loadedConnectionString = null; if (model.DevLoader != null) { loadedConnectionString = model.DevLoader.GetConnectionString(model.SettingName); Trace.TraceInformation($"Mash.AppSettings: attempting override connection string {model.SettingName} from dev settings with [{loadedConnectionString}]"); } if (String.IsNullOrWhiteSpace(loadedConnectionString)) { loadedConnectionString = model.SettingLoader.GetConnectionString(model.SettingName); } if (!CheckIfSettingIsValid(loadedConnectionString, model.SettingName)) { if (IsSettingRequired(model.Member)) { throw new ArgumentException("Mash.AppSettings: The connection string could not be found.", model.SettingName); } return true; } var parsedConnectionString = TypeParser.GetTypedValue(model.Member.PropertyType, loadedConnectionString); model.Member.SetValue(model.SettingsClass, parsedConnectionString); return true; }
protected string LoadValue(SettingTypeModel model) { string loadedValue = model.DevLoader?.GetSetting(model.SettingName); if (loadedValue == null) { loadedValue = model.SettingLoader.GetSetting(model.SettingName); } if (!CheckIfSettingIsValid(loadedValue, model.SettingName)) { if (IsSettingRequired(model.Member)) { throw new ArgumentException("Mash.AppSettings: The setting could not be found.", model.SettingName); } Trace.TraceInformation($"Mash.AppSettings: Skipping optional setting [{model.SettingName}] which had no value."); } return loadedValue; }