(feature): add custom Guid and List<Guid> serializers, removed useless idGenerator in constructor

This commit is contained in:
Maksym Sadovnychyy 2024-09-22 13:14:00 +02:00
parent f3260ee19b
commit 46c6907925
7 changed files with 59 additions and 27 deletions

View File

@ -22,7 +22,7 @@ namespace MaksIT.MongoDB.Tests {
public TestableCollectionDataProvider(ILogger<TestableCollectionDataProvider> logger)
#pragma warning disable CS8625 // Cannot convert null literal to non-nullable reference type.
: base(logger, new MongoClientMock(), new MongoIdGeneratorMock(), "TestDatabase", "TestCollection") {
: base(logger, new MongoClientMock(), "TestDatabase", "TestCollection") {
#pragma warning restore CS8625 // Cannot convert null literal to non-nullable reference type.
_inMemoryCollection = new List<TestableDocumentDto>(); // Initialize correctly
}

View File

@ -1,19 +0,0 @@
using MongoDB.Bson.Serialization;
namespace MaksIT.MongoDB.Linq.Tests.Mock {
internal class MongoIdGeneratorMock : IIdGenerator {
public Guid Generate() {
return Guid.NewGuid();
}
#region not implemented
public object GenerateId(object container, object document) {
throw new NotImplementedException();
}
public bool IsEmpty(object id) {
throw new NotImplementedException();
}
#endregion
}
}

View File

@ -10,19 +10,15 @@ namespace MaksIT.MongoDB.Linq.Abstractions {
public abstract class BaseCollectionDataProviderBase<T, TDtoDocument, TDtoKey> : DataProviderBase<T>
where TDtoDocument : DtoDocumentBase<TDtoKey> {
protected readonly IIdGenerator IdGenerator;
protected readonly IMongoCollection<TDtoDocument> Collection;
protected readonly string _errorMessage = "MaksIT.MongoDB.Linq - Data provider error";
protected BaseCollectionDataProviderBase(
ILogger<T> logger,
IMongoClient client,
IIdGenerator idGenerator,
string databaseName,
string collectionName
) : base(logger, client, databaseName) {
IdGenerator = idGenerator;
if (!Database.ListCollectionNames().ToList().Contains(collectionName))
Database.CreateCollection(collectionName);

View File

@ -17,10 +17,9 @@ namespace MaksIT.MongoDB.Linq.Abstractions {
protected CollectionDataProviderBase(
ILogger<T> logger,
IMongoClient client,
IIdGenerator idGenerator,
string databaseName,
string collectionName
) : base(logger, client, idGenerator, databaseName, collectionName) { }
) : base(logger, client, databaseName, collectionName) { }
#region Insert
public Result<TDtoKey?> Insert(TDtoDocument obj, IClientSessionHandle? session) =>

View File

@ -8,7 +8,7 @@
<!-- NuGet package metadata -->
<PackageId>MaksIT.MongoDB.Linq</PackageId>
<Version>1.0.2</Version>
<Version>1.0.3</Version>
<Authors>Maksym Sadovnychyy</Authors>
<Company>MAKS-IT</Company>
<Product>MaksIT.MongoDB.Linq</Product>

View File

@ -0,0 +1,23 @@
using MongoDB.Bson;
using MongoDB.Bson.Serialization;
using MongoDB.Bson.Serialization.Serializers;
namespace MaksIT.MaksIT.MongoDB.Linq.Serializers;
public class GuidSerializer : SerializerBase<Guid> {
public override Guid Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args) {
var bsonType = context.Reader.CurrentBsonType;
if (bsonType == BsonType.Binary) {
var binaryData = context.Reader.ReadBinaryData();
return new Guid(binaryData.Bytes);
}
throw new FormatException($"Cannot deserialize BsonType '{bsonType}' to Guid.");
}
public override void Serialize(BsonSerializationContext context, BsonSerializationArgs args, Guid value) {
var guidBytes = value.ToByteArray();
var binaryData = new BsonBinaryData(guidBytes, BsonBinarySubType.UuidStandard);
context.Writer.WriteBinaryData(binaryData);
}
}

View File

@ -0,0 +1,33 @@
using MongoDB.Bson;
using MongoDB.Bson.Serialization;
using MongoDB.Bson.Serialization.Serializers;
namespace MaksIT.MaksIT.MongoDB.Linq.Serializers;
public class ListGuidSerializer : SerializerBase<List<Guid>> {
private readonly GuidSerializer _guidSerializer = new GuidSerializer();
public override List<Guid> Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args) {
var bsonType = context.Reader.CurrentBsonType;
if (bsonType == BsonType.Array) {
var guidList = new List<Guid>();
context.Reader.ReadStartArray();
while (context.Reader.ReadBsonType() != BsonType.EndOfDocument) {
var guid = _guidSerializer.Deserialize(context, args);
guidList.Add(guid);
}
context.Reader.ReadEndArray();
return guidList;
}
throw new FormatException($"Cannot deserialize BsonType '{bsonType}' to List<Guid>.");
}
public override void Serialize(BsonSerializationContext context, BsonSerializationArgs args, List<Guid> value) {
context.Writer.WriteStartArray();
foreach (var guid in value) {
_guidSerializer.Serialize(context, args, guid);
}
context.Writer.WriteEndArray();
}
}