Skip to content

Commit

Permalink
task: removed statics BsonOptions
Browse files Browse the repository at this point in the history
  • Loading branch information
dtanglr committed Dec 12, 2023
1 parent b7db7d7 commit a23693d
Show file tree
Hide file tree
Showing 18 changed files with 102 additions and 39 deletions.
2 changes: 2 additions & 0 deletions src/Primitively.MongoDB.Bson/DependencyInjection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ public static PrimitivelyConfigurator AddBson(this PrimitivelyConfigurator confi
{
var registry = configurator.Options.Registry;
var options = new BsonOptions(registry);

builder?.Invoke(options);

options.Build();

return configurator;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ internal BsonSerializerRegisterBuilder() { }
/// </remarks>
public BsonSerializerRegisterBuilder ForEachTypeIn<TPrimitiveRepository>() where TPrimitiveRepository : class, IPrimitiveRepository, new()
{
// Get a list of the source generated Primitively types
// GetSerializerOptions a list of the source generated Primitively types
var repository = new TPrimitiveRepository();

return ForEachTypeIn(repository);
}

public BsonSerializerRegisterBuilder ForEachTypeIn(IPrimitiveRepository repository)
{
// Get a list of the source generated Primitively types
// GetSerializerOptions a list of the source generated Primitively types
var primitives = repository.GetTypes();

if (primitives.Count == 0)
Expand All @@ -57,7 +57,7 @@ public BsonSerializerRegisterBuilder ForEachTypeIn(IPrimitiveRepository reposito
/// <returns>BsonSerializerRegisterBuilder</returns>
public BsonSerializerRegisterBuilder ForType<TPrimitive>() where TPrimitive : struct, IPrimitive
{
// Get a default instance of the provided Primitively type to obtain the DataType enum property value
// GetSerializerOptions a default instance of the provided Primitively type to obtain the DataType enum property value
var primitive = new TPrimitive();
var dataType = primitive.DataType;

Expand Down Expand Up @@ -149,7 +149,7 @@ private static void RegisterBsonSerializer(Type primitiveType, DataType dataType
_primitiveTypes.Add(primitiveType);

// Create a Primitively serializer instance
var serializerType = BsonSerializerOptionsCache.Get(dataType); // TODO: Update Get method/call to ensure failure handled
var serializerType = BsonSerializerOptionsCache.Get(dataType); // TODO: Update GetSerializerOptions method/call to ensure failure handled
var serializerInstance = serializerType.CreateInstance(primitiveType);

// Register a Serializer for the Primitively type
Expand All @@ -166,7 +166,7 @@ private static void RegisterBsonSerializer(Type primitiveType, DataType dataType
_primitiveTypes.Add(nullablePrimitiveType);

// Create a nullable Primitively serializer instance
var serializerType = BsonSerializerOptionsCache.Get(dataType); // TODO: Update Get method/call to ensure failure handled
var serializerType = BsonSerializerOptionsCache.Get(dataType); // TODO: Update GetSerializerOptions method/call to ensure failure handled
var serializerInstance = NullableSerializer.Create(serializerType.CreateInstance(primitiveType));

// Register a NullableSerializer for a nullable version of the Primitively type
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace Primitively.MongoDB.Bson.Serialization;

public interface IBsonSerializerRepository
{

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public record BsonIByteSerializerOptions : IBsonSerializerOptions<BsonIByteSeria
public Func<BsonIByteSerializerOptions, Type, IBsonSerializer> CreateInstance { get; set; } = (options, primitiveType) =>
{
// Construct a Bson serializer for the given Primitively type using the options
var serializerType = BsonOptions.GetSerializerType(primitiveType, options.SerializerType);
var serializerType = options.GetSerializerType(primitiveType, options.SerializerType);

// Create an instance of the serializer
var serializerInstance = (IBsonSerializer)Activator.CreateInstance(serializerType, options.Representation)!;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public record BsonIDateOnlySerializerOptions : IBsonSerializerOptions<BsonIDateO
public Func<BsonIDateOnlySerializerOptions, Type, IBsonSerializer> CreateInstance { get; set; } = (options, primitiveType) =>
{
// Construct a Bson serializer for the given Primitively type using the options
var serializerType = BsonOptions.GetSerializerType(primitiveType, options.SerializerType);
var serializerType = options.GetSerializerType(primitiveType, options.SerializerType);

// Create an instance of the serializer
var serializerInstance = (IBsonSerializer)Activator.CreateInstance(serializerType, options.Representation)!;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public record BsonIGuidSerializerOptions : IBsonIGuidSerializerOptions<BsonIGuid
}

// Construct a Bson serializer for the given Primitively type using the options
var serializerType = BsonOptions.GetSerializerType(primitiveType, options.SerializerType);
var serializerType = options.GetSerializerType(primitiveType, options.SerializerType);

// Create an instance of the serializer
object argument = options.Representation == BsonType.String ? BsonType.String : options.GuidRepresentation;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public record BsonIIntSerializerOptions : IBsonConvertibleSerializerOptions<Bson
public Func<BsonIIntSerializerOptions, Type, IBsonSerializer> CreateInstance { get; set; } = (options, primitiveType) =>
{
// Construct a Bson serializer for the given Primitively type using the options
var serializerType = BsonOptions.GetSerializerType(primitiveType, options.SerializerType);
var serializerType = options.GetSerializerType(primitiveType, options.SerializerType);

// Create an instance of the serializer
var serializerInstance = (IBsonSerializer)Activator.CreateInstance(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public record BsonILongSerializerOptions : IBsonConvertibleSerializerOptions<Bso
public Func<BsonILongSerializerOptions, Type, IBsonSerializer> CreateInstance { get; set; } = (options, primitiveType) =>
{
// Construct a Bson serializer for the given Primitively type using the options
var serializerType = BsonOptions.GetSerializerType(primitiveType, options.SerializerType);
var serializerType = options.GetSerializerType(primitiveType, options.SerializerType);

// Create an instance of the serializer
var serializerInstance = (IBsonSerializer)Activator.CreateInstance(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public record BsonISByteSerializerOptions : IBsonSerializerOptions<BsonISByteSer
public Func<BsonISByteSerializerOptions, Type, IBsonSerializer> CreateInstance { get; set; } = (options, primitiveType) =>
{
// Construct a Bson serializer for the given Primitively type using the options
var serializerType = BsonOptions.GetSerializerType(primitiveType, options.SerializerType);
var serializerType = options.GetSerializerType(primitiveType, options.SerializerType);

// Create an instance of the serializer
var serializerInstance = (IBsonSerializer)Activator.CreateInstance(serializerType, options.Representation)!;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public record BsonIShortSerializerOptions : IBsonConvertibleSerializerOptions<Bs
public Func<BsonIShortSerializerOptions, Type, IBsonSerializer> CreateInstance { get; set; } = (options, primitiveType) =>
{
// Construct a Bson serializer for the given Primitively type using the options
var serializerType = BsonOptions.GetSerializerType(primitiveType, options.SerializerType);
var serializerType = options.GetSerializerType(primitiveType, options.SerializerType);

// Create an instance of the serializer
var serializerInstance = (IBsonSerializer)Activator.CreateInstance(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public record BsonIStringSerializerOptions : IBsonSerializerOptions<BsonIStringS
public Func<BsonIStringSerializerOptions, Type, IBsonSerializer> CreateInstance { get; set; } = (options, primitiveType) =>
{
// Construct a Bson serializer for the given Primitively type using the options
var serializerType = BsonOptions.GetSerializerType(primitiveType, options.SerializerType);
var serializerType = options.GetSerializerType(primitiveType, options.SerializerType);

// Create an instance of the serializer
var serializerInstance = (IBsonSerializer)Activator.CreateInstance(serializerType, options.Representation)!;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public record BsonIUIntSerializerOptions : IBsonConvertibleSerializerOptions<Bso
public Func<BsonIUIntSerializerOptions, Type, IBsonSerializer> CreateInstance { get; set; } = (options, primitiveType) =>
{
// Construct a Bson serializer for the given Primitively type using the options
var serializerType = BsonOptions.GetSerializerType(primitiveType, options.SerializerType);
var serializerType = options.GetSerializerType(primitiveType, options.SerializerType);

// Create an instance of the serializer
var serializerInstance = (IBsonSerializer)Activator.CreateInstance(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public record BsonIULongSerializerOptions : IBsonConvertibleSerializerOptions<Bs
public Func<BsonIULongSerializerOptions, Type, IBsonSerializer> CreateInstance { get; set; } = (options, primitiveType) =>
{
// Construct a Bson serializer for the given Primitively type using the options
var serializerType = BsonOptions.GetSerializerType(primitiveType, options.SerializerType);
var serializerType = options.GetSerializerType(primitiveType, options.SerializerType);

// Create an instance of the serializer
var serializerInstance = (IBsonSerializer)Activator.CreateInstance(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public record BsonIUShortSerializerOptions : IBsonConvertibleSerializerOptions<B
public Func<BsonIUShortSerializerOptions, Type, IBsonSerializer> CreateInstance { get; set; } = (options, primitiveType) =>
{
// Construct a Bson serializer for the given Primitively type using the options
var serializerType = BsonOptions.GetSerializerType(primitiveType, options.SerializerType);
var serializerType = options.GetSerializerType(primitiveType, options.SerializerType);

// Create an instance of the serializer
var serializerInstance = (IBsonSerializer)Activator.CreateInstance(
Expand Down
61 changes: 39 additions & 22 deletions src/Primitively.MongoDB.Bson/Serialization/Options/BsonOptions.cs
Original file line number Diff line number Diff line change
@@ -1,110 +1,125 @@
using MongoDB.Bson.Serialization;
using System.Collections.Concurrent;
using MongoDB.Bson.Serialization;
using MongoDB.Bson.Serialization.Serializers;
using Primitively.Configuration;

namespace Primitively.MongoDB.Bson.Serialization.Options;

public class BsonOptions
{
private readonly Dictionary<Type, DataType> _primitiveTypes = new();
private readonly PrimitiveRegistry _registry;
private readonly ConcurrentDictionary<DataType, IBsonSerializerOptions> _options;
private readonly Dictionary<Type, DataType> _primitiveTypes = new();

internal BsonOptions(PrimitiveRegistry registry)
{
_registry = registry;
_options = new(GetAll().ToDictionary(o => o.DataType, o => o));
}

public bool RegisterSerializersForEachTypeInRegistry { get; set; } = true;

public static Type GetSerializerType(Type primitiveType, Type serializerType)
private static IEnumerable<IBsonSerializerOptions> GetAll()
{
// Construct a Bson serializer for the given Primitively type using the options
return serializerType.IsGenericTypeDefinition ? serializerType.MakeGenericType(primitiveType) : serializerType;
// Initialises the default instancse for each option
yield return new BsonIByteSerializerOptions();
yield return new BsonIDateOnlySerializerOptions();
yield return new BsonIGuidSerializerOptions();
yield return new BsonIIntSerializerOptions();
yield return new BsonILongSerializerOptions();
yield return new BsonISByteSerializerOptions();
yield return new BsonIShortSerializerOptions();
yield return new BsonIStringSerializerOptions();
yield return new BsonIUIntSerializerOptions();
yield return new BsonIULongSerializerOptions();
yield return new BsonIUShortSerializerOptions();
}

public IBsonSerializerOptions GetSerializerOptions(DataType dataType) => _options[dataType];

public BsonOptions BsonIByteSerializer(Action<BsonIByteSerializerOptions> options)
{
var option = BsonSerializerOptionsCache.Get(DataType.Byte);
var option = GetSerializerOptions(DataType.Byte);
options.Invoke((BsonIByteSerializerOptions)option);

return this;
}

public BsonOptions BsonIDateOnlySerializer(Action<BsonIDateOnlySerializerOptions> options)
{
var option = BsonSerializerOptionsCache.Get(DataType.DateOnly);
var option = GetSerializerOptions(DataType.DateOnly);
options.Invoke((BsonIDateOnlySerializerOptions)option);

return this;
}

public BsonOptions BsonIGuidSerializer(Action<BsonIGuidSerializerOptions> options)
{
var option = BsonSerializerOptionsCache.Get(DataType.Guid);
var option = GetSerializerOptions(DataType.Guid);
options.Invoke((BsonIGuidSerializerOptions)option);

return this;
}

public BsonOptions BsonIIntSerializer(Action<BsonIIntSerializerOptions> options)
{
var option = BsonSerializerOptionsCache.Get(DataType.Int);
var option = GetSerializerOptions(DataType.Int);
options.Invoke((BsonIIntSerializerOptions)option);

return this;
}

public BsonOptions BsonILongSerializer(Action<BsonILongSerializerOptions> options)
{
var option = BsonSerializerOptionsCache.Get(DataType.Long);
var option = GetSerializerOptions(DataType.Long);
options.Invoke((BsonILongSerializerOptions)option);

return this;
}

public BsonOptions BsonISByteSerializer(Action<BsonISByteSerializerOptions> options)
{
var option = BsonSerializerOptionsCache.Get(DataType.SByte);
var option = GetSerializerOptions(DataType.SByte);
options.Invoke((BsonISByteSerializerOptions)option);

return this;
}

public BsonOptions BsonIShortSerializer(Action<BsonIShortSerializerOptions> options)
{
var option = BsonSerializerOptionsCache.Get(DataType.Short);
var option = GetSerializerOptions(DataType.Short);
options.Invoke((BsonIShortSerializerOptions)option);

return this;
}

public BsonOptions BsonIStringSerializer(Action<BsonIStringSerializerOptions> options)
{
var option = BsonSerializerOptionsCache.Get(DataType.String);
var option = GetSerializerOptions(DataType.String);
options.Invoke((BsonIStringSerializerOptions)option);

return this;
}

public BsonOptions BsonIUIntSerializer(Action<BsonIUIntSerializerOptions> options)
{
var option = BsonSerializerOptionsCache.Get(DataType.UInt);
var option = GetSerializerOptions(DataType.UInt);
options.Invoke((BsonIUIntSerializerOptions)option);

return this;
}

public BsonOptions BsonIULongSerializer(Action<BsonIULongSerializerOptions> options)
{
var option = BsonSerializerOptionsCache.Get(DataType.ULong);
var option = GetSerializerOptions(DataType.ULong);
options.Invoke((BsonIULongSerializerOptions)option);

return this;
}

public BsonOptions BsonIUShortSerializer(Action<BsonIUShortSerializerOptions> options)
{
var option = BsonSerializerOptionsCache.Get(DataType.UShort);
var option = GetSerializerOptions(DataType.UShort);
options.Invoke((BsonIUShortSerializerOptions)option);

return this;
Expand Down Expand Up @@ -133,21 +148,23 @@ internal void Build()
}
}

private static void RegisterSerializer(Type primitiveType, DataType dataType)
private void RegisterSerializer(Type primitiveType, DataType dataType)
{
// Create a Primitively serializer instance
var serializerType = BsonSerializerOptionsCache.Get(dataType); // TODO: Update Get method/call to ensure failure handled
var serializerInstance = serializerType.CreateInstance(primitiveType);
// Retrieve the serializer options for the given dataType
var serializerOptions = GetSerializerOptions(dataType);

// Register a Serializer for the Primitively type
BsonSerializer.TryRegisterSerializer(primitiveType, serializerInstance);
// Create a Primitively serializer instance
var serializerInstance = serializerOptions.CreateInstance(primitiveType);

// Construct a nullable version of the Primitively type
var nullablePrimitiveType = typeof(Nullable<>).MakeGenericType(primitiveType);

// Create a nullable Primitively serializer instance
var nullableSerializerInstance = NullableSerializer.Create(serializerInstance);

// Register a Serializer for the Primitively type
BsonSerializer.TryRegisterSerializer(primitiveType, serializerInstance);

// Register a NullableSerializer for a nullable version of the Primitively type
BsonSerializer.TryRegisterSerializer(nullablePrimitiveType, nullableSerializerInstance);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,10 @@ public static IBsonSerializerOptions<TOptions> SerializeWith<TOptions>(this IBso
options.SerializerType = serializerType;
return options;
}

public static Type GetSerializerType(this IBsonSerializerOptions _, Type primitiveType, Type serializerType)
{
// Construct a Bson serializer for the given Primitively type using the options
return serializerType.IsGenericTypeDefinition ? serializerType.MakeGenericType(primitiveType) : serializerType;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using MongoDB.Driver;
using Testcontainers.MongoDb;
using Xunit;

namespace Primitively.IntegrationTests.MongoDbTests;

public sealed class MongoDbContainerTest : IAsyncLifetime
{
private readonly MongoDbContainer _mongoDbContainer = new MongoDbBuilder().Build();

[Fact]
public async Task ReadFromMongoDbDatabase()
{
var client = new MongoClient(_mongoDbContainer.GetConnectionString());

using var databases = await client.ListDatabasesAsync();

Assert.True(await databases.AnyAsync());
}

public Task InitializeAsync()
=> _mongoDbContainer.StartAsync();

public Task DisposeAsync()
=> _mongoDbContainer.DisposeAsync().AsTask();
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
<PackageReference Include="FluentAssertions" Version="6.12.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="MongoDB.Bson" Version="2.22.0" />
<PackageReference Include="Moq" Version="4.20.69" />
<PackageReference Include="MongoDB.Driver" Version="2.22.0" />
<PackageReference Include="Moq" Version="4.20.70" />
<PackageReference Include="Testcontainers.MongoDb" Version="3.6.0" />
<PackageReference Include="xunit" Version="2.6.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.4">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down

0 comments on commit a23693d

Please sign in to comment.