[banshee] [Configuration] Refactor ConfigurationClients
- From: Gabriel Burt <gburt src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [banshee] [Configuration] Refactor ConfigurationClients
- Date: Thu, 18 Nov 2010 17:16:34 +0000 (UTC)
commit f5ba0a09c77bda80d1913280814fab0916113c84
Author: Gabriel Burt <gabriel burt gmail com>
Date: Thu Nov 18 11:06:56 2010 -0600
[Configuration] Refactor ConfigurationClients
And add bool TryGet<T> function that returns false if no such key/value
pair exists.
.../GConfConfigurationClient.cs | 36 +++-------------
.../Banshee.Configuration/ConfigurationClient.cs | 26 +++++++-----
.../Banshee.Configuration/IConfigurationClient.cs | 42 ++++++++++++++++---
.../MemoryConfigurationClient.cs | 40 +++---------------
.../XmlConfigurationClient.cs | 38 ++++++------------
.../DatabaseConfigurationClient.cs | 39 +++---------------
.../Banshee.Database/SortKeyUpdater.cs | 1 +
7 files changed, 85 insertions(+), 137 deletions(-)
---
diff --git a/src/Backends/Banshee.Gnome/Banshee.GnomeBackend/GConfConfigurationClient.cs b/src/Backends/Banshee.Gnome/Banshee.GnomeBackend/GConfConfigurationClient.cs
index 69635b3..9372b6f 100644
--- a/src/Backends/Banshee.Gnome/Banshee.GnomeBackend/GConfConfigurationClient.cs
+++ b/src/Backends/Banshee.Gnome/Banshee.GnomeBackend/GConfConfigurationClient.cs
@@ -102,25 +102,11 @@ namespace Banshee.GnomeBackend
}
}
- public T Get<T> (SchemaEntry<T> entry)
- {
- return Get<T> (entry.Namespace, entry.Key, entry.DefaultValue);
- }
-
- public T Get<T> (SchemaEntry<T> entry, T fallback)
- {
- return Get<T> (entry.Namespace, entry.Key, fallback);
- }
-
- public T Get<T> (string key, T fallback)
- {
- return Get<T> (null, key, fallback);
- }
-
- public T Get<T> (string @namespace, string key, T fallback)
+ public bool TryGet<T> (string @namespace, string key, out T result)
{
if (DisableGConf || key == null) {
- return fallback;
+ result = default (T);
+ return false;
}
if (client == null) {
@@ -128,23 +114,15 @@ namespace Banshee.GnomeBackend
}
try {
- return (T)client.Get (CreateKey (@namespace, key));
+ result = (T)client.Get (CreateKey (@namespace, key));
+ return true;
} catch (GConf.NoSuchKeyException) {
- return fallback;
} catch (Exception e) {
Log.Exception (String.Format ("Could no read GConf key {0}.{1}", @namespace, key), e);
- return fallback;
}
- }
- public void Set<T> (SchemaEntry<T> entry, T value)
- {
- Set<T> (entry.Namespace, entry.Key, value);
- }
-
- public void Set<T> (string key, T value)
- {
- Set<T> (null, key, value);
+ result = default (T);
+ return false;
}
public void Set<T> (string @namespace, string key, T value)
diff --git a/src/Core/Banshee.Core/Banshee.Configuration/ConfigurationClient.cs b/src/Core/Banshee.Core/Banshee.Configuration/ConfigurationClient.cs
index e8134dc..7534c04 100644
--- a/src/Core/Banshee.Core/Banshee.Configuration/ConfigurationClient.cs
+++ b/src/Core/Banshee.Core/Banshee.Configuration/ConfigurationClient.cs
@@ -78,34 +78,40 @@ namespace Banshee.Configuration
}
}
- public static T Get<T> (SchemaEntry<T> entry)
+ public static T Get<T>(SchemaEntry<T> entry)
{
- return Client.Get<T> (entry);
+ return Get<T>(entry.Namespace, entry.Key, entry.DefaultValue);
}
- public static T Get<T> (SchemaEntry<T> entry, T fallback)
+ public static T Get<T>(SchemaEntry<T> entry, T fallback)
{
- return Client.Get<T> (entry, fallback);
+ return Get<T>(entry.Namespace, entry.Key, fallback);
}
- public static T Get<T> (string key, T fallback)
+ public static T Get<T>(string key, T fallback)
{
- return Client.Get<T> (key, fallback);
+ return Get<T>(null, key, fallback);
}
- public static T Get<T> (string @namespace, string key, T fallback)
+ public static T Get<T>(string namespce, string key, T fallback)
{
- return Client.Get<T> (@namespace, key, fallback);
+ T result;
+ return TryGet<T> (namespce, key, out result) ? result : fallback;
+ }
+
+ public static bool TryGet<T> (string @namespace, string key, out T result)
+ {
+ return Client.TryGet<T> (@namespace, key, out result);
}
public static void Set<T> (SchemaEntry<T> entry, T value)
{
- Client.Set<T> (entry, value);
+ Set (entry.Namespace, entry.Key, value);
}
public static void Set<T> (string key, T value)
{
- Client.Set<T> (key, value);
+ Set (null, key, value);
}
public static void Set<T> (string @namespace, string key, T value)
diff --git a/src/Core/Banshee.Core/Banshee.Configuration/IConfigurationClient.cs b/src/Core/Banshee.Core/Banshee.Configuration/IConfigurationClient.cs
index 10064ba..d7831c3 100644
--- a/src/Core/Banshee.Core/Banshee.Configuration/IConfigurationClient.cs
+++ b/src/Core/Banshee.Core/Banshee.Configuration/IConfigurationClient.cs
@@ -30,13 +30,41 @@ namespace Banshee.Configuration
{
public interface IConfigurationClient
{
- T Get<T> (SchemaEntry<T> entry);
- T Get<T> (SchemaEntry<T> entry, T fallback);
- T Get<T> (string key, T fallback);
- T Get<T> (string @namespace, string key, T fallback);
-
- void Set<T> (SchemaEntry<T> entry, T value);
- void Set<T> (string key, T value);
+ bool TryGet<T> (string @namespace, string key, out T result);
void Set<T> (string @namespace, string key, T value);
}
+
+ public static class Extensions
+ {
+ public static T Get<T> (this IConfigurationClient client, SchemaEntry<T> entry)
+ {
+ return client.Get<T> (entry.Namespace, entry.Key, entry.DefaultValue);
+ }
+
+ public static T Get<T> (this IConfigurationClient client, SchemaEntry<T> entry, T fallback)
+ {
+ return client.Get<T> (entry.Namespace, entry.Key, fallback);
+ }
+
+ public static T Get<T> (this IConfigurationClient client, string key, T fallback)
+ {
+ return client.Get<T> (null, key, fallback);
+ }
+
+ public static T Get<T> (this IConfigurationClient client, string namespce, string key, T fallback)
+ {
+ T result;
+ return client.TryGet<T> (namespce, key, out result) ? result : fallback;
+ }
+
+ public static void Set<T> (this IConfigurationClient client, SchemaEntry<T> entry, T value)
+ {
+ client.Set (entry.Namespace, entry.Key, value);
+ }
+
+ public static void Set<T> (this IConfigurationClient client, string key, T value)
+ {
+ client.Set (null, key, value);
+ }
+ }
}
diff --git a/src/Core/Banshee.Core/Banshee.Configuration/MemoryConfigurationClient.cs b/src/Core/Banshee.Core/Banshee.Configuration/MemoryConfigurationClient.cs
index 8f62aad..a4a7fc9 100644
--- a/src/Core/Banshee.Core/Banshee.Configuration/MemoryConfigurationClient.cs
+++ b/src/Core/Banshee.Core/Banshee.Configuration/MemoryConfigurationClient.cs
@@ -35,38 +35,9 @@ namespace Banshee.Configuration
{
private Dictionary<string, object> config = new Dictionary<string, object> ();
-#region Helper Overrides
-
- public T Get<T> (SchemaEntry<T> entry)
- {
- return Get (entry.Namespace, entry.Key, entry.DefaultValue);
- }
-
- public T Get<T> (SchemaEntry<T> entry, T fallback)
- {
- return Get (entry.Namespace, entry.Key, fallback);
- }
-
- public T Get<T> (string key, T fallback)
- {
- return Get (null, key, fallback);
- }
-
- public void Set<T> (SchemaEntry<T> entry, T value)
- {
- Set (entry.Namespace, entry.Key, value);
- }
-
- public void Set<T> (string key, T value)
- {
- Set (null, key, value);
- }
-
-#endregion
-
#region Implementation
- public T Get<T> (string namespce, string key, T fallback)
+ public bool TryGet<T> (string namespce, string key, out T result)
{
lock (this) {
string fq_key = MakeKey (namespce, key);
@@ -74,13 +45,16 @@ namespace Banshee.Configuration
if (config.TryGetValue (fq_key, out value)) {
if (value == null) {
- return default (T);
+ result = default (T);
+ return true;
} else if (value.GetType () == typeof (T)) {
- return (T)value;
+ result = (T)value;
+ return true;
}
}
- return fallback;
+ result = default (T);
+ return false;
}
}
diff --git a/src/Core/Banshee.Core/Banshee.Configuration/XmlConfigurationClient.cs b/src/Core/Banshee.Core/Banshee.Configuration/XmlConfigurationClient.cs
index 4eb6b04..a19a451 100644
--- a/src/Core/Banshee.Core/Banshee.Configuration/XmlConfigurationClient.cs
+++ b/src/Core/Banshee.Core/Banshee.Configuration/XmlConfigurationClient.cs
@@ -86,41 +86,27 @@ namespace Banshee.Configuration
}
}
- public T Get<T>(SchemaEntry<T> entry)
- {
- return Get<T>(entry.Namespace, entry.Key, entry.DefaultValue);
- }
-
- public T Get<T>(SchemaEntry<T> entry, T fallback)
- {
- return Get<T>(entry.Namespace, entry.Key, fallback);
- }
-
- public T Get<T>(string key, T fallback)
- {
- return Get<T>(null, key, fallback);
- }
-
- public T Get<T>(string namespce, string key, T fallback)
+ public bool TryGet<T>(string namespce, string key, out T result)
{
lock(xml_document) {
XmlNode namespace_node = GetNamespaceNode(namespce == null
? new string [] {null_namespace}
: namespce.Split('.'), false);
- if(namespace_node == null) {
- return fallback;
- }
-
- foreach(XmlNode node in namespace_node.ChildNodes) {
- if(node.Attributes[tag_identifier_attribute_name].Value == key && node.Name == value_tag_name) {
- XmlSerializer serializer = new XmlSerializer(typeof(T));
- using (var reader = new StringReader(node.InnerXml) ) {
- return (T) serializer.Deserialize(reader);
+ if(namespace_node != null) {
+ foreach(XmlNode node in namespace_node.ChildNodes) {
+ if(node.Attributes[tag_identifier_attribute_name].Value == key && node.Name == value_tag_name) {
+ XmlSerializer serializer = new XmlSerializer(typeof(T));
+ using (var reader = new StringReader(node.InnerXml) ) {
+ result = (T) serializer.Deserialize(reader);
+ return true;
+ }
}
}
}
- return fallback;
+
+ result = default (T);
+ return false;
}
}
diff --git a/src/Core/Banshee.Services/Banshee.Configuration/DatabaseConfigurationClient.cs b/src/Core/Banshee.Services/Banshee.Configuration/DatabaseConfigurationClient.cs
index 7a60603..3097c4e 100644
--- a/src/Core/Banshee.Services/Banshee.Configuration/DatabaseConfigurationClient.cs
+++ b/src/Core/Banshee.Services/Banshee.Configuration/DatabaseConfigurationClient.cs
@@ -63,34 +63,19 @@ namespace Banshee.Configuration
"UPDATE {0} SET Value=? WHERE Key=?", TableName));
}
- public T Get <T> (SchemaEntry<T> entry)
- {
- return Get (entry.Namespace, entry.Key, entry.DefaultValue);
- }
-
- public T Get <T> (SchemaEntry<T> entry, T fallback)
- {
- return Get (entry.Namespace, entry.Key, fallback);
- }
-
- public T Get <T> (string key, T fallback)
- {
- return Get (null, key, fallback);
- }
-
- public T Get <T> (string namespce, string key, T fallback)
+ public bool TryGet <T> (string namespce, string key, out T result)
{
try {
using (IDataReader reader = Get (namespce, key)) {
if (reader.Read ()) {
- return (T) Convert.ChangeType (reader[0], typeof (T));
- } else {
- return fallback;
+ result = (T) Convert.ChangeType (reader[0], typeof (T));
+ return true;
}
}
- } catch {
- return fallback;
- }
+ } catch {}
+
+ result = default (T);
+ return false;
}
private IDataReader Get (string namespce, string key)
@@ -99,16 +84,6 @@ namespace Banshee.Configuration
Banshee.Configuration.MemoryConfigurationClient.MakeKey (namespce, key));
}
- public void Set <T> (SchemaEntry<T> entry, T value)
- {
- Set (entry.Namespace, entry.Key, value);
- }
-
- public void Set <T> (string key, T value)
- {
- Set (null, key, value);
- }
-
public void Set <T> (string namespce, string key, T value)
{
string fq_key = Banshee.Configuration.MemoryConfigurationClient.MakeKey (namespce, key);
diff --git a/src/Core/Banshee.Services/Banshee.Database/SortKeyUpdater.cs b/src/Core/Banshee.Services/Banshee.Database/SortKeyUpdater.cs
index 8a6d7e2..5d93cec 100644
--- a/src/Core/Banshee.Services/Banshee.Database/SortKeyUpdater.cs
+++ b/src/Core/Banshee.Services/Banshee.Database/SortKeyUpdater.cs
@@ -25,6 +25,7 @@
//
using System;
+using System.Linq;
using System.Globalization;
using Banshee.Collection;
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]