Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support custom Json settings for JsonEventFormatter #82

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Extensions/src/Ncqrs.Eventing.Storage.WindowsAzure/Utility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<JsonSerializer>().Serialize(new StringWriter(result), data);
return result.ToString();
}

Expand All @@ -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<JsonSerializer>().Deserialize(new StringReader(data), type);
}

public static object DeJsonize(string data, string assemblyQualifiedTypeName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<JsonSerializer>().Deserialize(reader, type);
return result.Version > maxVersion
? null
: result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,27 @@ public class JsonEventFormatter : IEventFormatter<JObject>
/// when resolving event types/names.</param>
/// <exception cref="ArgumentNullException"><paramref name="typeResolver"/> is <value>null</value>.</exception>
public JsonEventFormatter(IEventTypeResolver typeResolver)
: this(typeResolver, NcqrsEnvironment.Get<JsonSerializer>())
{
Contract.Requires<ArgumentNullException>(typeResolver != null, "typeResolver");
}

/// <summary>
/// Initializes a new instance of the <see cref="JsonEventFormatter"/> class
/// with a given type resolver and JsonSerializer.
/// </summary>
/// <param name="typeResolver">The <see cref="IEventTypeResolver"/> to use
/// when resolving event types/names.</param>
/// <param name="serializer">The json serializer.</param>
/// <exception cref="ArgumentNullException"><paramref name="typeResolver"/> is <value>null</value>.</exception>
/// <exception cref="ArgumentNullException"><paramref name="serializer"/> is <value>null</value>.</exception>
public JsonEventFormatter(IEventTypeResolver typeResolver, JsonSerializer serializer)
{
Contract.Requires<ArgumentNullException>(typeResolver != null, "typeResolver");
Contract.Requires<ArgumentNullException>(serializer != null, "serializer");

_typeResolver = typeResolver;
_serializer = new JsonSerializer();
_serializer = serializer;
}

public object Deserialize(JObject obj, string eventName)
Expand Down
32 changes: 25 additions & 7 deletions Framework/src/Ncqrs/NcqrsEnvironment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using Ncqrs.Eventing.Sourcing.Snapshotting;
using Ncqrs.Eventing.Storage;
using Ncqrs.Domain.Storage;
using Newtonsoft.Json;

namespace Ncqrs
{
Expand Down Expand Up @@ -44,6 +45,7 @@ private static void InitDefaults()
SetDefault<IAggregateRootCreationStrategy>(new SimpleAggregateRootCreationStrategy());
SetDefault<IAggregateSupportsSnapshotValidator>(new AggregateSupportsSnapshotValidator());
SetDefault<IAggregateSnapshotter>(new DefaultAggregateSnapshotter(Get<IAggregateRootCreationStrategy>(), Get<IAggregateSupportsSnapshotValidator>()));
SetDefault<JsonSerializer>(() => new JsonSerializer());
}

/// <summary>
Expand All @@ -52,7 +54,7 @@ private static void InitDefaults()
/// <remarks>
/// Use the <see cref="SetDefault{T}"/> method to set a default.
/// </remarks>
private static readonly Dictionary<Type, Object> _defaults = new Dictionary<Type, object>(0);
private static readonly Dictionary<Type, Func<Object>> _defaults = new Dictionary<Type, Func<object>>(0);

/// <summary>
/// Hold the environment configuration. This is initialized by the <see cref="Configure"/> method.
Expand Down Expand Up @@ -81,12 +83,11 @@ public static T Get<T>() where T : class

if (_instance == null || !_instance.TryGet(out result))
{
object defaultResult;
Func<object> defaultFactory;

if (_defaults.TryGetValue(typeof(T), out defaultResult))
if (_defaults.TryGetValue(typeof(T), out defaultFactory))
{
result = (T)defaultResult;

result = (T)defaultFactory();
}
}

Expand All @@ -105,12 +106,29 @@ public static T Get<T>() where T : class
/// </remarks>
/// <typeparam name="T">The type of the instance to set a default.
/// </typeparam>
/// <param name="instance">The instance to set as default.</param>
/// <param name="instance">The instance to return by default.</param>
public static void SetDefault<T>(T instance) where T : class
{
Contract.Requires<ArgumentNullException>(instance != null, "The instance cannot be null.");

_defaults[typeof(T)] = instance;
_defaults[typeof(T)] = () => instance;
}

/// <summary>
/// Sets the default for an type. This default instance is returned when
/// the configured <see cref="IEnvironmentConfiguration"/> did not
/// returned an instance for this type.
/// </summary>
/// <remarks>When the type already contains a default, it is overridden.
/// </remarks>
/// <typeparam name="T">The type of the instance to set a default.
/// </typeparam>
/// <param name="instanceFactory">The factory function to get/create a default instance.</param>
public static void SetDefault<T>(Func<T> instanceFactory) where T : class
{
Contract.Requires<ArgumentNullException>(instanceFactory != null, "The instance factory cannot be null.");

_defaults[typeof(T)] = instanceFactory;
}

/// <summary>
Expand Down