PageRenderTime 21ms CodeModel.GetById 15ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://github.com/nwendel/ravendb
C# | 57 lines | 51 code | 6 blank | 0 comment | 6 complexity | be31ee4b871db74a652a70186eaa9857 MD5 | raw file
 1using System;
 2using System.Collections.Specialized;
 3using System.IO;
 4using System.Text;
 5using System.Linq;
 6using Raven.Database.Server.RavenFS.Storage;
 7using Raven.Database.Server.RavenFS.Util;
 8using Raven.Imports.Newtonsoft.Json;
 9using Raven.Abstractions.Extensions;
10using Raven.Json.Linq;
11using System.Collections;
12using System.Collections.Generic;
13
14namespace Raven.Database.Server.RavenFS.Extensions
15{
16	public static class ConfigurationExtension
17	{
18        public static T GetConfigurationValue<T>(this IStorageActionsAccessor accessor, string key)
19        {
20            var value = accessor.GetConfig(key);
21            if (typeof(T).IsValueType || typeof(T) == typeof(string))
22                return value.Value<T>("Value");
23
24            return JsonExtensions.JsonDeserialization<T>(value);
25        }
26
27        public static IEnumerable<T> GetConfigurationValuesStartWithPrefix<T>(this IStorageActionsAccessor accessor, string prefix, int start, int take)
28        {
29            var values = accessor.GetConfigsStartWithPrefix(prefix, start, take);
30            if (typeof(T).IsValueType || typeof(T) == typeof(string))
31            {
32                return values.Select(x => x.Value<T>("Value"));
33            }
34
35            return values.Select(x => JsonExtensions.JsonDeserialization<T>(x));
36        }
37
38        public static bool TryGetConfigurationValue<T>(this IStorageActionsAccessor accessor, string key, out T result)
39        {
40            try
41            {
42                result = GetConfigurationValue<T>(accessor, key);
43                return true;
44            }
45            catch (FileNotFoundException)
46            {
47                result = default(T);
48                return false;
49            }
50        }
51
52        public static void SetConfigurationValue<T>(this IStorageActionsAccessor accessor, string key, T objectToSave)
53        {
54            accessor.SetConfig(key, JsonExtensions.ToJObject(objectToSave));
55        }
56	}
57}