示例#1
0
        public static IConfiguration SetValue <T>([NotNull] this IConfiguration config, [NotNull] Expression <Func <T> > expression, [CanBeNull] T value, [CanBeNull] string instance = null)
        {
            if (config == null)
            {
                throw new ArgumentNullException(nameof(config));
            }
            if (expression == null)
            {
                throw new ArgumentNullException(nameof(expression));
            }

            var settingInfo = SettingMetadata.FromExpression(expression, false);
            var query       = ValueQueryFactory.CreateSetValueQuery(settingInfo, value, instance);

            //var settingValue = config.GetValue(settingInfo.SettingName, typeof(T), settingInfo.ProviderName) ?? settingInfo.DefaultValue;

            //var settingValue = //settingInfo.GetValue();

            settingInfo
            .Validations
            .Validate(query.SettingName, value);

            config.SetValue(query);
            return(config);
        }
示例#2
0
        public static IConfiguration AssignValues <T>([NotNull] this IConfiguration configuration, [NotNull] T obj, [CanBeNull] string instance = null)
        {
            if (configuration == null)
            {
                throw new ArgumentNullException(nameof(configuration));
            }
            if (obj == null)
            {
                throw new ArgumentNullException(nameof(obj));
            }

            var settingProperties =
                typeof(T)
                .GetProperties(BindingFlags.Instance | BindingFlags.Public)
                .Where(p => p.GetCustomAttribute <SettingMemberAttribute>() != null);

            foreach (var property in settingProperties)
            {
                // Create a lambda-expression so that we can reuse the extensions for it we already have.
                var expression = Expression.Lambda(
                    Expression.Property(
                        Expression.Constant(obj),
                        property.Name
                        )
                    );

                var settingInfo = SettingMetadata.FromExpression(expression, false);
                //var value = configuration.GetValue(settingInfo.SettingName, property.PropertyType, settingInfo.ProviderName);
                //settingInfo.SetValue(value);
            }

            return(configuration);
        }
示例#3
0
        public async Task <object> GetItemAsync(LambdaExpression getItem, string handle = null)
        {
            if (getItem == null)
            {
                throw new ArgumentNullException(nameof(getItem));
            }

            var settingInfo     = MemberVisitor.GetMemberInfo(getItem);
            var settingMetadata = new SettingMetadata(settingInfo, GetMemberName);

            var(uri, metadata) = SettingRequestFactory.CreateSettingRequest(settingMetadata, handle);
            return(await _settings.GetItemAsync <object>(uri, metadata.Set(Use <IResourceNamespace> .Namespace, x => x.Type, settingMetadata.MemberType)));
        }
示例#4
0
        public async Task SetItemAsync(LambdaExpression setItem, object newValue, string handle = null)
        {
            if (setItem == null)
            {
                throw new ArgumentNullException(nameof(setItem));
            }

            var settingInfo     = MemberVisitor.GetMemberInfo(setItem);
            var settingMetadata = new SettingMetadata(settingInfo, GetMemberName);

            var(uri, metadata) = SettingRequestFactory.CreateSettingRequest(settingMetadata, handle);
            Validate(newValue, settingMetadata.Validations, uri);
            await _settings.SetItemAsync(uri, newValue, metadata.Set(Use <IResourceNamespace> .Namespace, x => x.Type, settingMetadata.MemberType));
        }
示例#5
0
        public static IConfiguration AssignValue <T>([NotNull] this IConfiguration configuration, [NotNull] Expression <Func <T> > expression, [CanBeNull] string instance = null)
        {
            if (configuration == null)
            {
                throw new ArgumentNullException(nameof(configuration));
            }
            if (expression == null)
            {
                throw new ArgumentNullException(nameof(expression));
            }

            var settingContext = SettingMetadata.FromExpression(expression, false);

            var value = configuration.GetValue(expression, instance);

            settingContext.SetValue(value);

            return(configuration);
        }
示例#6
0
        public static UpdateQuery CreateSetValueQuery <T>(SettingMetadata settingMetadata, T value, string instance)
        {
            var settingName = new SettingName
                              (
                prefix: settingMetadata.Prefix,
                schema: settingMetadata.Namespace,
                type: settingMetadata.TypeName,
                member: settingMetadata.MemberName,
                instance: instance
                              );

            return(new UpdateQuery(settingName, value)
            {
                ProviderName = settingMetadata.ProviderName,
                Strength = settingMetadata.SettingNameStrength,
                PrefixHandling = settingMetadata.PrefixHandling,
                ProviderType = settingMetadata.ProviderType
            });
        }
示例#7
0
        private static T GetValue <T>([NotNull] this IConfiguration config, [NotNull] LambdaExpression expression, [CanBeNull] string instanceName = null)
        {
            if (config == null)
            {
                throw new ArgumentNullException(nameof(config));
            }
            if (expression == null)
            {
                throw new ArgumentNullException(nameof(expression));
            }

            var settingMetadata = SettingMetadata.FromExpression(expression, false);
            var query           = ValueQueryFactory.CreateGetValueQuery(settingMetadata, instanceName);

            var settingValue = config.GetValue(query) ?? settingMetadata.DefaultValue;

            settingMetadata
            .Validations
            .Validate(query.SettingName, settingValue);

            return((T)settingValue);
        }
示例#8
0
 public static (UriString Uri, IImmutableSession Metadata) CreateSettingRequest(SettingMetadata setting, string handle = null)
 {
     var queryParameters = new (SoftString Key, SoftString Value)[]