From 14982f9cfb0f6f3896851a02d043036568cf7f3d Mon Sep 17 00:00:00 2001 From: Matt Sullivan Date: Wed, 19 Oct 2011 16:10:23 +1000 Subject: [PATCH 1/3] NcqrsEnvironment support for default factories (as well as instances.) --- Framework/src/Ncqrs/NcqrsEnvironment.cs | 30 +++++++++++++++++++------ 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/Framework/src/Ncqrs/NcqrsEnvironment.cs b/Framework/src/Ncqrs/NcqrsEnvironment.cs index c468b415..05eb966b 100644 --- a/Framework/src/Ncqrs/NcqrsEnvironment.cs +++ b/Framework/src/Ncqrs/NcqrsEnvironment.cs @@ -52,7 +52,7 @@ private static void InitDefaults() /// /// Use the method to set a default. /// - private static readonly Dictionary _defaults = new Dictionary(0); + private static readonly Dictionary> _defaults = new Dictionary>(0); /// /// Hold the environment configuration. This is initialized by the method. @@ -81,12 +81,11 @@ public static T Get() where T : class if (_instance == null || !_instance.TryGet(out result)) { - object defaultResult; + Func defaultFactory; - if (_defaults.TryGetValue(typeof(T), out defaultResult)) + if (_defaults.TryGetValue(typeof(T), out defaultFactory)) { - result = (T)defaultResult; - + result = (T)defaultFactory(); } } @@ -105,12 +104,29 @@ public static T Get() where T : class /// /// The type of the instance to set a default. /// - /// The instance to set as default. + /// The instance to return by default. public static void SetDefault(T instance) where T : class { Contract.Requires(instance != null, "The instance cannot be null."); - _defaults[typeof(T)] = instance; + _defaults[typeof(T)] = () => instance; + } + + /// + /// Sets the default for an type. This default instance is returned when + /// the configured did not + /// returned an instance for this type. + /// + /// When the type already contains a default, it is overridden. + /// + /// The type of the instance to set a default. + /// + /// The factory function to get/create a default instance. + public static void SetDefault(Func instanceFactory) where T : class + { + Contract.Requires(instanceFactory != null, "The instance factory cannot be null."); + + _defaults[typeof(T)] = instanceFactory; } /// From 2bf2fd7d6c89ff9054f30c3238b520493d3ef61e Mon Sep 17 00:00:00 2001 From: Matt Sullivan Date: Wed, 19 Oct 2011 16:14:49 +1000 Subject: [PATCH 2/3] Support custom Json settings for JsonEventFormatter JsonEventFormatter now uses NcqrsEnvironment to instances of JsonSerializer. Thus IEnvironmentConfiguration can return instances with custom settings. --- .../Storage/Serialization/JsonEventFormatter.cs | 17 ++++++++++++++++- Framework/src/Ncqrs/NcqrsEnvironment.cs | 2 ++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/Framework/src/Ncqrs/Eventing/Storage/Serialization/JsonEventFormatter.cs b/Framework/src/Ncqrs/Eventing/Storage/Serialization/JsonEventFormatter.cs index 861494d6..910ae512 100644 --- a/Framework/src/Ncqrs/Eventing/Storage/Serialization/JsonEventFormatter.cs +++ b/Framework/src/Ncqrs/Eventing/Storage/Serialization/JsonEventFormatter.cs @@ -22,11 +22,26 @@ public class JsonEventFormatter : IEventFormatter /// when resolving event types/names. /// is null. public JsonEventFormatter(IEventTypeResolver typeResolver) + : this(typeResolver, NcqrsEnvironment.Get()) { Contract.Requires(typeResolver != null, "typeResolver"); + } + + /// + /// Initializes a new instance of the class + /// with a given type resolver. + /// + /// The to use + /// when resolving event types/names. + /// The serializer. + /// is null. + public JsonEventFormatter(IEventTypeResolver typeResolver, JsonSerializer serializer) + { + Contract.Requires(typeResolver != null, "typeResolver"); + Contract.Requires(serializer != null, "serializer"); _typeResolver = typeResolver; - _serializer = new JsonSerializer(); + _serializer = serializer; } public object Deserialize(JObject obj, string eventName) diff --git a/Framework/src/Ncqrs/NcqrsEnvironment.cs b/Framework/src/Ncqrs/NcqrsEnvironment.cs index 05eb966b..04197bc2 100644 --- a/Framework/src/Ncqrs/NcqrsEnvironment.cs +++ b/Framework/src/Ncqrs/NcqrsEnvironment.cs @@ -10,6 +10,7 @@ using Ncqrs.Eventing.Sourcing.Snapshotting; using Ncqrs.Eventing.Storage; using Ncqrs.Domain.Storage; +using Newtonsoft.Json; namespace Ncqrs { @@ -44,6 +45,7 @@ private static void InitDefaults() SetDefault(new SimpleAggregateRootCreationStrategy()); SetDefault(new AggregateSupportsSnapshotValidator()); SetDefault(new DefaultAggregateSnapshotter(Get(), Get())); + SetDefault(() => new JsonSerializer()); } /// From a1f8dd8fc893747fca7f752d3f208e34ec1bee77 Mon Sep 17 00:00:00 2001 From: Matt Sullivan Date: Wed, 19 Oct 2011 16:30:42 +1000 Subject: [PATCH 3/3] Use NcqrsEnvironment to get a JsonSerializer everywhere it's used. --- .../src/Ncqrs.Eventing.Storage.WindowsAzure/Utility.cs | 4 ++-- .../src/Ncqrs/Eventing/Storage/NoDB/NoDBSnapshotStore.cs | 2 +- .../Eventing/Storage/Serialization/JsonEventFormatter.cs | 5 +++-- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/Extensions/src/Ncqrs.Eventing.Storage.WindowsAzure/Utility.cs b/Extensions/src/Ncqrs.Eventing.Storage.WindowsAzure/Utility.cs index 1e73d0c6..a84820df 100644 --- a/Extensions/src/Ncqrs.Eventing.Storage.WindowsAzure/Utility.cs +++ b/Extensions/src/Ncqrs.Eventing.Storage.WindowsAzure/Utility.cs @@ -12,7 +12,7 @@ public static class Utility public static string Jsonize(object data, Type type) { StringBuilder result = new StringBuilder(); - new JsonSerializer().Serialize(new StringWriter(result), data); + NcqrsEnvironment.Get().Serialize(new StringWriter(result), data); return result.ToString(); } @@ -24,7 +24,7 @@ public static string Jsonize(object data, string assemblyQualifiedTypeName) public static object DeJsonize(string data, Type type) { - return new JsonSerializer().Deserialize(new StringReader(data), type); + return NcqrsEnvironment.Get().Deserialize(new StringReader(data), type); } public static object DeJsonize(string data, string assemblyQualifiedTypeName) diff --git a/Framework/src/Ncqrs/Eventing/Storage/NoDB/NoDBSnapshotStore.cs b/Framework/src/Ncqrs/Eventing/Storage/NoDB/NoDBSnapshotStore.cs index b8214c92..9be8c622 100644 --- a/Framework/src/Ncqrs/Eventing/Storage/NoDB/NoDBSnapshotStore.cs +++ b/Framework/src/Ncqrs/Eventing/Storage/NoDB/NoDBSnapshotStore.cs @@ -35,7 +35,7 @@ public Snapshot GetSnapshot(Guid eventSourceId, long maxVersion) var type = Type.GetType(typeline.Trim()); try { - var result = (Snapshot) new JsonSerializer().Deserialize(reader, type); + var result = (Snapshot)NcqrsEnvironment.Get().Deserialize(reader, type); return result.Version > maxVersion ? null : result; diff --git a/Framework/src/Ncqrs/Eventing/Storage/Serialization/JsonEventFormatter.cs b/Framework/src/Ncqrs/Eventing/Storage/Serialization/JsonEventFormatter.cs index 910ae512..8d28ac8b 100644 --- a/Framework/src/Ncqrs/Eventing/Storage/Serialization/JsonEventFormatter.cs +++ b/Framework/src/Ncqrs/Eventing/Storage/Serialization/JsonEventFormatter.cs @@ -29,12 +29,13 @@ public JsonEventFormatter(IEventTypeResolver typeResolver) /// /// Initializes a new instance of the class - /// with a given type resolver. + /// with a given type resolver and JsonSerializer. /// /// The to use /// when resolving event types/names. - /// The serializer. + /// The json serializer. /// is null. + /// is null. public JsonEventFormatter(IEventTypeResolver typeResolver, JsonSerializer serializer) { Contract.Requires(typeResolver != null, "typeResolver");