Permalink
Browse files

Add ToInQueueName(this IMessage message) Ext method

  • Loading branch information...
mythz committed Oct 11, 2012
1 parent 558ef07 commit 0a57df34257f78571165d18b861d7507f435d12c
Showing with 96 additions and 87 deletions.
  1. +96 −87 src/ServiceStack.Common/Messaging/MessageExtensions.cs
@@ -1,88 +1,97 @@
-using System;
-using System.Collections.Generic;
-using System.Reflection;
-using System.Threading;
-using ServiceStack.Text;
-
-namespace ServiceStack.Messaging
-{
- public static class MessageExtensions
- {
- public static string ToString(byte[] bytes)
- {
-#if !SILVERLIGHT
- return System.Text.Encoding.UTF8.GetString(bytes);
-#else
- return System.Text.Encoding.UTF8.GetString(bytes, 0, bytes.Length);
-#endif
- }
-
- private static Dictionary<Type, ToMessageDelegate> ToMessageFnCache = new Dictionary<Type, ToMessageDelegate>();
- internal static ToMessageDelegate GetToMessageFn(Type type)
- {
- ToMessageDelegate toMessageFn;
- ToMessageFnCache.TryGetValue(type, out toMessageFn);
-
- if (toMessageFn != null) return toMessageFn;
-
- var genericType = typeof(MessageExtensions<>).MakeGenericType(type);
- var mi = genericType.GetMethod("ConvertToMessage", BindingFlags.Public | BindingFlags.Static);
- toMessageFn = (ToMessageDelegate)Delegate.CreateDelegate(typeof(ToMessageDelegate), mi);
-
- Dictionary<Type, ToMessageDelegate> snapshot, newCache;
- do
- {
- snapshot = ToMessageFnCache;
- newCache = new Dictionary<Type, ToMessageDelegate>(ToMessageFnCache);
- newCache[type] = toMessageFn;
-
- } while (!ReferenceEquals(
- Interlocked.CompareExchange(ref ToMessageFnCache, newCache, snapshot), snapshot));
-
- return toMessageFn;
- }
-
- public static IMessage ToMessage(this byte[] bytes, Type ofType)
- {
- var msgFn = GetToMessageFn(ofType);
- var msg = msgFn(bytes);
- return msg;
- }
-
- public static Message<T> ToMessage<T>(this byte[] bytes)
- {
- var messageText = ToString(bytes);
- return JsonSerializer.DeserializeFromString<Message<T>>(messageText);
- }
-
- public static byte[] ToBytes(this IMessage message)
- {
- var serializedMessage = JsonSerializer.SerializeToString((object)message);
- return System.Text.Encoding.UTF8.GetBytes(serializedMessage);
- }
-
- public static byte[] ToBytes<T>(this IMessage<T> message)
- {
- var serializedMessage = JsonSerializer.SerializeToString(message);
- return System.Text.Encoding.UTF8.GetBytes(serializedMessage);
- }
-
- public static string ToInQueueName<T>(this IMessage<T> message)
- {
- return message.Priority > 0
- ? QueueNames<T>.Priority
- : QueueNames<T>.In;
- }
- }
-
- internal delegate IMessage ToMessageDelegate(object param);
-
- internal static class MessageExtensions<T>
- {
- public static IMessage ConvertToMessage(object oBytes)
- {
- var bytes = (byte[]) oBytes;
- return bytes.ToMessage<T>();
- }
- }
+using System;
+using System.Collections.Generic;
+using System.Reflection;
+using System.Threading;
+using ServiceStack.Text;
+
+namespace ServiceStack.Messaging
+{
+ public static class MessageExtensions
+ {
+ public static string ToString(byte[] bytes)
+ {
+#if !SILVERLIGHT
+ return System.Text.Encoding.UTF8.GetString(bytes);
+#else
+ return System.Text.Encoding.UTF8.GetString(bytes, 0, bytes.Length);
+#endif
+ }
+
+ private static Dictionary<Type, ToMessageDelegate> ToMessageFnCache = new Dictionary<Type, ToMessageDelegate>();
+ internal static ToMessageDelegate GetToMessageFn(Type type)
+ {
+ ToMessageDelegate toMessageFn;
+ ToMessageFnCache.TryGetValue(type, out toMessageFn);
+
+ if (toMessageFn != null) return toMessageFn;
+
+ var genericType = typeof(MessageExtensions<>).MakeGenericType(type);
+ var mi = genericType.GetMethod("ConvertToMessage", BindingFlags.Public | BindingFlags.Static);
+ toMessageFn = (ToMessageDelegate)Delegate.CreateDelegate(typeof(ToMessageDelegate), mi);
+
+ Dictionary<Type, ToMessageDelegate> snapshot, newCache;
+ do
+ {
+ snapshot = ToMessageFnCache;
+ newCache = new Dictionary<Type, ToMessageDelegate>(ToMessageFnCache);
+ newCache[type] = toMessageFn;
+
+ } while (!ReferenceEquals(
+ Interlocked.CompareExchange(ref ToMessageFnCache, newCache, snapshot), snapshot));
+
+ return toMessageFn;
+ }
+
+ public static IMessage ToMessage(this byte[] bytes, Type ofType)
+ {
+ var msgFn = GetToMessageFn(ofType);
+ var msg = msgFn(bytes);
+ return msg;
+ }
+
+ public static Message<T> ToMessage<T>(this byte[] bytes)
+ {
+ var messageText = ToString(bytes);
+ return JsonSerializer.DeserializeFromString<Message<T>>(messageText);
+ }
+
+ public static byte[] ToBytes(this IMessage message)
+ {
+ var serializedMessage = JsonSerializer.SerializeToString((object)message);
+ return System.Text.Encoding.UTF8.GetBytes(serializedMessage);
+ }
+
+ public static byte[] ToBytes<T>(this IMessage<T> message)
+ {
+ var serializedMessage = JsonSerializer.SerializeToString(message);
+ return System.Text.Encoding.UTF8.GetBytes(serializedMessage);
+ }
+
+ public static string ToInQueueName(this IMessage message)
+ {
+ var queueName = message.Priority > 0
+ ? new QueueNames(message.Body.GetType()).Priority
+ : new QueueNames(message.Body.GetType()).In;
+
+ return queueName;
+ }
+
+ public static string ToInQueueName<T>(this IMessage<T> message)
+ {
+ return message.Priority > 0
+ ? QueueNames<T>.Priority
+ : QueueNames<T>.In;
+ }
+ }
+
+ internal delegate IMessage ToMessageDelegate(object param);
+
+ internal static class MessageExtensions<T>
+ {
+ public static IMessage ConvertToMessage(object oBytes)
+ {
+ var bytes = (byte[]) oBytes;
+ return bytes.ToMessage<T>();
+ }
+ }
}

0 comments on commit 0a57df3

Please sign in to comment.