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

More serialization configuration #41

Merged
merged 1 commit into from
Apr 14, 2021
Merged
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
2 changes: 2 additions & 0 deletions Spinit.CosmosDb.UnitTests/Internals/JsonSerializingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Microsoft.Azure.Cosmos;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Xunit;
Expand Down Expand Up @@ -57,6 +58,7 @@ private class CustomOptions : IDatabaseOptions
public string DatabaseId { get; set; }
public string PreferredLocation { get; set; }
public Action<JsonSerializerSettings> ConfigureJsonSerializerSettings { get; set; }
public Action<CosmosClientOptions> ConfigureCosmosClientOptions { get; set; }
}

public class VersionJsonConverter : JsonConverter
Expand Down
9 changes: 8 additions & 1 deletion Spinit.CosmosDb/CosmosDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ protected CosmosDatabase(IDatabaseOptions options, bool initialize = true)
new CosmosClient(
new Uri(options.Endpoint).AbsoluteUri,
options.Key,
new CosmosClientOptions()
CreateCosmosClientOptions(options)
), options, initialize)
{ }

Expand All @@ -78,6 +78,13 @@ internal static Documents.Client.ConnectionPolicy CreateConnectionPolicy(IDataba
return connectionPolicy;
}

internal static CosmosClientOptions CreateCosmosClientOptions(IDatabaseOptions options = null)
{
var clientOptions = new CosmosClientOptions();
options?.ConfigureCosmosClientOptions?.Invoke(clientOptions);
return clientOptions;
}

internal static JsonSerializerSettings CreateJsonSerializerSettings(IDatabaseOptions options = null)
{
var settings = new JsonSerializerSettings
Expand Down
10 changes: 6 additions & 4 deletions Spinit.CosmosDb/DatabaseOptions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Newtonsoft.Json;
using Microsoft.Azure.Cosmos;
using Newtonsoft.Json;
using System;

namespace Spinit.CosmosDb
Expand All @@ -20,9 +21,10 @@ public class DatabaseOptions<TDatabase> : IDatabaseOptions
/// </summary>
public string PreferredLocation { get; set; }

/// <summary>
/// Optional Json serialization settings configuration
/// </summary>
/// <inheritdoc/>
public Action<JsonSerializerSettings> ConfigureJsonSerializerSettings { get; set; }

/// <inheritdoc/>
public Action<CosmosClientOptions> ConfigureCosmosClientOptions { get; set; }
}
}
11 changes: 10 additions & 1 deletion Spinit.CosmosDb/IDatabaseOptions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Newtonsoft.Json;
using Microsoft.Azure.Cosmos;
using Newtonsoft.Json;
using System;

namespace Spinit.CosmosDb
Expand All @@ -19,6 +20,14 @@ public interface IDatabaseOptions
/// </summary>
string PreferredLocation { get; set; }

/// <summary>
/// Configure document client json serializer settings
/// </summary>
Action<JsonSerializerSettings> ConfigureJsonSerializerSettings { get; set; }

/// <summary>
/// Configure CosmosClient options
/// </summary>
Action<CosmosClientOptions> ConfigureCosmosClientOptions { get; set; }
}
}