Skip to content

Commit

Permalink
task: replaced DI for Bson setup
Browse files Browse the repository at this point in the history
  • Loading branch information
dtanglr committed Dec 26, 2023
1 parent a23693d commit 05eff53
Show file tree
Hide file tree
Showing 10 changed files with 148 additions and 341 deletions.
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
using System.Collections.Concurrent;
using MongoDB.Bson.Serialization;
using MongoDB.Bson.Serialization.Serializers;
using MongoDB.Bson.Serialization.Serializers;
using Primitively.Configuration;
using Primitively.MongoDB.Bson.Serialization;
using Primitively.MongoDB.Bson.Serialization.Options;

namespace Primitively.MongoDB.Bson.Serialization.Options;
namespace Primitively.MongoDB.Bson;

public class BsonOptions
{
private readonly PrimitiveRegistry _registry;
private readonly ConcurrentDictionary<DataType, IBsonSerializerOptions> _options;
private readonly IBsonSerializerManager _manager;
private readonly Dictionary<DataType, IBsonSerializerOptions> _options = new(GetAll().ToDictionary(o => o.DataType, o => o));
private readonly Dictionary<Type, DataType> _primitiveTypes = new();

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

public bool RegisterSerializersForEachTypeInRegistry { get; set; } = true;
Expand All @@ -35,8 +36,6 @@ private static IEnumerable<IBsonSerializerOptions> GetAll()
yield return new BsonIUShortSerializerOptions();
}

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

public BsonOptions BsonIByteSerializer(Action<BsonIByteSerializerOptions> options)
{
var option = GetSerializerOptions(DataType.Byte);
Expand Down Expand Up @@ -125,29 +124,65 @@ public BsonOptions BsonIUShortSerializer(Action<BsonIUShortSerializerOptions> op
return this;
}

public BsonOptions Register<TPrimitive>() where TPrimitive : struct, IPrimitive
{
var primitive = new TPrimitive();
var _ = TryAddPrimitiveType(typeof(TPrimitive), primitive.DataType);

return this;
}

public BsonOptions Register(IPrimitiveRepository repository)
{
if (repository is null)
{
throw new ArgumentNullException(nameof(repository));
}

foreach (var primitiveInfo in repository.GetTypes())
{
var _ = TryAddPrimitiveType(primitiveInfo.Type, primitiveInfo.DataType);
}

return this;
}

internal void Build()
{
// If configured, add all the primitive types from the registry
if (RegisterSerializersForEachTypeInRegistry && !_registry.IsEmpty)
{
foreach (var primitiveInfo in _registry.ToList())
{
#if NET6_0_OR_GREATER
_primitiveTypes.TryAdd(primitiveInfo.Type, primitiveInfo.DataType);
#else
if (!_primitiveTypes.ContainsKey(primitiveInfo.Type))
{
_primitiveTypes.Add(primitiveInfo.Type, primitiveInfo.DataType);
}
#endif
var _ = TryAddPrimitiveType(primitiveInfo.Type, primitiveInfo.DataType);
}
}

// Now generate and register a Bson serializer for each type in the collection
foreach (var primitiveType in _primitiveTypes)
{
RegisterSerializer(primitiveType.Key, primitiveType.Value);
}
}

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

private bool TryAddPrimitiveType(Type type, DataType dataType)
{
#if NET6_0_OR_GREATER
return _primitiveTypes.TryAdd(type, dataType);
#else
if (_primitiveTypes.ContainsKey(type))
{
return false;
}

_primitiveTypes.Add(type, dataType);

return true;
#endif
}

private void RegisterSerializer(Type primitiveType, DataType dataType)
{
// Retrieve the serializer options for the given dataType
Expand All @@ -163,9 +198,9 @@ private void RegisterSerializer(Type primitiveType, DataType dataType)
var nullableSerializerInstance = NullableSerializer.Create(serializerInstance);

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

// Register a NullableSerializer for a nullable version of the Primitively type
BsonSerializer.TryRegisterSerializer(nullablePrimitiveType, nullableSerializerInstance);
_manager.TryRegisterSerializer(nullablePrimitiveType, nullableSerializerInstance);
}
}
17 changes: 10 additions & 7 deletions src/Primitively.MongoDB.Bson/DependencyInjection.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Primitively.Configuration;
using Primitively.MongoDB.Bson.Serialization.Options;
using Microsoft.Extensions.DependencyInjection;
using Primitively.Configuration;
using Primitively.MongoDB.Bson.Serialization;

namespace Primitively.MongoDB.Bson;

Expand All @@ -13,16 +14,18 @@ public static class DependencyInjection
/// Register MongoDB nullable and non-nullable Bson serializers
/// </summary>
/// <param name="configurator">Configurator</param>
/// <param name="builder">BsonOptions</param>
/// <param name="options">BsonOptions</param>
/// <returns>Configurator</returns>
public static PrimitivelyConfigurator AddBson(this PrimitivelyConfigurator configurator, Action<BsonOptions>? builder = null)
public static PrimitivelyConfigurator AddBson(this PrimitivelyConfigurator configurator, Action<BsonOptions>? options = null)
{
var registry = configurator.Options.Registry;
var options = new BsonOptions(registry);
var services = configurator.Services.BuildServiceProvider();
var manager = services.GetService<IBsonSerializerManager>() ?? new BsonSerializerManager();
var builder = new BsonOptions(registry, manager);

builder?.Invoke(options);
options?.Invoke(builder);

options.Build();
builder.Build();

return configurator;
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using MongoDB.Bson.Serialization;

namespace Primitively.MongoDB.Bson.Serialization;

public class BsonSerializerManager : IBsonSerializerManager
{
public IBsonSerializer LookupSerializer(Type type) =>
BsonSerializer.LookupSerializer(type);

public bool TryRegisterSerializer(Type type, IBsonSerializer serializer) =>
BsonSerializer.TryRegisterSerializer(type, serializer);
}

This file was deleted.

Loading

0 comments on commit 05eff53

Please sign in to comment.