Skip to content

Commit

Permalink
task: updated each options class to include a CreateInstance func pro…
Browse files Browse the repository at this point in the history
…perty
  • Loading branch information
dtanglr committed Dec 2, 2023
1 parent 32d0446 commit 8226e47
Show file tree
Hide file tree
Showing 40 changed files with 578 additions and 483 deletions.
26 changes: 2 additions & 24 deletions src/Primitively.MongoDB.Bson/DependencyInjection.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Primitively.Configuration;
using Primitively.MongoDB.Bson.Serialization;
using Primitively.MongoDB.Bson.Serialization.Options;

namespace Primitively.MongoDB.Bson;
Expand All @@ -16,30 +15,9 @@ public static class DependencyInjection
/// <param name="configurator">Configurator</param>
/// <param name="options">BsonOptions</param>
/// <returns>Configurator</returns>
public static PrimitivelyConfigurator AddBson(this PrimitivelyConfigurator configurator, BsonOptions? options = null)
public static PrimitivelyConfigurator AddBson(this PrimitivelyConfigurator configurator, Action<BsonOptions>? options = null)
{
return configurator.AddBson(configure => { }, options ?? new BsonOptions());
}

/// <summary>
/// Register MongoDB nullable and non-nullable Bson serializers
/// </summary>
/// <param name="configurator">Configurator</param>
/// <param name="options">BsonOptions</param>
/// <param name="configure">BsonSerializerBuilder</param>
/// <returns>Configurator</returns>
public static PrimitivelyConfigurator AddBson(this PrimitivelyConfigurator configurator, Action<BsonSerializerBuilder> configure, BsonOptions? options = null)
{
BsonSerializerBuilder builder = new(options ??= new BsonOptions());
builder.SetDefaultGuidRepresentation();

if (options.RegisterSerializersForEachTypeInRegistry)
{
builder.RegisterSerializers(register =>
register.AddSerializerForEachTypeIn(configurator.Options.Registry));
}

configure.Invoke(builder);
options?.Invoke(new BsonOptions());

return configurator;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<Nullable>enable</Nullable>
<Description>A library which provides MongoDB support for types produced by the Primitively C# source generator</Description>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<NoWarn>$(NoWarn);CA1510</NoWarn>
<NoWarn>$(NoWarn);CA1510;CS0618</NoWarn>
</PropertyGroup>

<Import Project="..\Publish.props" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using MongoDB.Bson.Serialization.Serializers;
using MongoDB.Bson.Serialization;
using MongoDB.Bson;
using Primitively.MongoDB.Bson.Serialization.Options;
using Primitively.MongoDB.Bson.Serialization.Options;

namespace Primitively.MongoDB.Bson.Serialization;

Expand All @@ -10,7 +7,6 @@ namespace Primitively.MongoDB.Bson.Serialization;
/// </summary>
public class BsonSerializerBuilder
{
private readonly BsonSerializerCacheBuilder _cacheBuilder = new();
private readonly BsonSerializerRegisterBuilder _registerBuilder = new();
private readonly BsonOptions _options;

Expand All @@ -22,39 +18,6 @@ internal BsonSerializerBuilder(BsonOptions options)
_options = options;
}

internal BsonSerializerBuilder SetDefaultGuidRepresentation()
{
//var options = _options.GuidOptions;
//var guidSerializer = options.Representation == BsonType.String
// ? new GuidSerializer(BsonType.String)
// : new GuidSerializer(options.GuidRepresentation);

#pragma warning disable CS0618 // Type or member is obsolete
//BsonDefaults.GuidRepresentation = options.GuidRepresentation;
//BsonDefaults.GuidRepresentationMode = options.GuidRepresentationMode;
#pragma warning restore CS0618 // Type or member is obsolete

//BsonSerializer.TryRegisterSerializer(typeof(Guid), guidSerializer);

return this;
}

/// <summary>
/// Override any of the default Primitively serializers with custom ones
/// </summary>
/// <param name="cache">Cache builder. See <see cref="BsonSerializerCacheBuilder"/></param>
/// <returns>The current instance</returns>
/// <remarks>
/// There is a default serializer for each Primitively <see cref="DataType"/>. These are held in cache.
/// This method allows the default serializer to be replaced with a custom implementation.
/// </remarks>
public BsonSerializerBuilder DefaultSerializers(Action<BsonSerializerCacheBuilder> cache)
{
cache.Invoke(_cacheBuilder);

return this;
}

/// <summary>
/// Registers non-nullable and nullable serializer instances for each of the given Primitively types.
/// </summary>
Expand All @@ -64,7 +27,6 @@ public BsonSerializerBuilder DefaultSerializers(Action<BsonSerializerCacheBuilde
/// For a Primitively type to be serialized correctly in MongoDB they must have an associated serializer.
/// This method provides a mechanism which generates and registers an instance of a serializer for the given type.
/// Each Primitively <see cref="DataType"/> has a matching serializer held in cache. The defaults can be changed using
/// the <see cref="DefaultSerializers(Action{BsonSerializerCacheBuilder})"/> method.
/// </remarks>
public BsonSerializerBuilder RegisterSerializers(Action<BsonSerializerRegisterBuilder> register)
{
Expand Down
66 changes: 0 additions & 66 deletions src/Primitively.MongoDB.Bson/Serialization/BsonSerializerCache.cs

This file was deleted.

This file was deleted.

Loading

0 comments on commit 8226e47

Please sign in to comment.