ravendb /Raven.Database/Server/RavenFS/Extensions/ConfigurationExtension.cs

Language C# Lines 57
MD5 Hash be31ee4b871db74a652a70186eaa9857
Repository https://github.com/nwendel/ravendb.git View Raw File
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
using System;
using System.Collections.Specialized;
using System.IO;
using System.Text;
using System.Linq;
using Raven.Database.Server.RavenFS.Storage;
using Raven.Database.Server.RavenFS.Util;
using Raven.Imports.Newtonsoft.Json;
using Raven.Abstractions.Extensions;
using Raven.Json.Linq;
using System.Collections;
using System.Collections.Generic;

namespace Raven.Database.Server.RavenFS.Extensions
{
	public static class ConfigurationExtension
	{
        public static T GetConfigurationValue<T>(this IStorageActionsAccessor accessor, string key)
        {
            var value = accessor.GetConfig(key);
            if (typeof(T).IsValueType || typeof(T) == typeof(string))
                return value.Value<T>("Value");

            return JsonExtensions.JsonDeserialization<T>(value);
        }

        public static IEnumerable<T> GetConfigurationValuesStartWithPrefix<T>(this IStorageActionsAccessor accessor, string prefix, int start, int take)
        {
            var values = accessor.GetConfigsStartWithPrefix(prefix, start, take);
            if (typeof(T).IsValueType || typeof(T) == typeof(string))
            {
                return values.Select(x => x.Value<T>("Value"));
            }

            return values.Select(x => JsonExtensions.JsonDeserialization<T>(x));
        }

        public static bool TryGetConfigurationValue<T>(this IStorageActionsAccessor accessor, string key, out T result)
        {
            try
            {
                result = GetConfigurationValue<T>(accessor, key);
                return true;
            }
            catch (FileNotFoundException)
            {
                result = default(T);
                return false;
            }
        }

        public static void SetConfigurationValue<T>(this IStorageActionsAccessor accessor, string key, T objectToSave)
        {
            accessor.SetConfig(key, JsonExtensions.ToJObject(objectToSave));
        }
	}
}
Back to Top