(feature): add custom Guid and List<Guid> serializers, removed useless idGenerator in constructor
This commit is contained in:
parent
f3260ee19b
commit
46c6907925
@ -22,7 +22,7 @@ namespace MaksIT.MongoDB.Tests {
|
|||||||
|
|
||||||
public TestableCollectionDataProvider(ILogger<TestableCollectionDataProvider> logger)
|
public TestableCollectionDataProvider(ILogger<TestableCollectionDataProvider> logger)
|
||||||
#pragma warning disable CS8625 // Cannot convert null literal to non-nullable reference type.
|
#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.
|
#pragma warning restore CS8625 // Cannot convert null literal to non-nullable reference type.
|
||||||
_inMemoryCollection = new List<TestableDocumentDto>(); // Initialize correctly
|
_inMemoryCollection = new List<TestableDocumentDto>(); // Initialize correctly
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -10,19 +10,15 @@ namespace MaksIT.MongoDB.Linq.Abstractions {
|
|||||||
public abstract class BaseCollectionDataProviderBase<T, TDtoDocument, TDtoKey> : DataProviderBase<T>
|
public abstract class BaseCollectionDataProviderBase<T, TDtoDocument, TDtoKey> : DataProviderBase<T>
|
||||||
where TDtoDocument : DtoDocumentBase<TDtoKey> {
|
where TDtoDocument : DtoDocumentBase<TDtoKey> {
|
||||||
|
|
||||||
protected readonly IIdGenerator IdGenerator;
|
|
||||||
protected readonly IMongoCollection<TDtoDocument> Collection;
|
protected readonly IMongoCollection<TDtoDocument> Collection;
|
||||||
protected readonly string _errorMessage = "MaksIT.MongoDB.Linq - Data provider error";
|
protected readonly string _errorMessage = "MaksIT.MongoDB.Linq - Data provider error";
|
||||||
|
|
||||||
protected BaseCollectionDataProviderBase(
|
protected BaseCollectionDataProviderBase(
|
||||||
ILogger<T> logger,
|
ILogger<T> logger,
|
||||||
IMongoClient client,
|
IMongoClient client,
|
||||||
IIdGenerator idGenerator,
|
|
||||||
string databaseName,
|
string databaseName,
|
||||||
string collectionName
|
string collectionName
|
||||||
) : base(logger, client, databaseName) {
|
) : base(logger, client, databaseName) {
|
||||||
IdGenerator = idGenerator;
|
|
||||||
|
|
||||||
if (!Database.ListCollectionNames().ToList().Contains(collectionName))
|
if (!Database.ListCollectionNames().ToList().Contains(collectionName))
|
||||||
Database.CreateCollection(collectionName);
|
Database.CreateCollection(collectionName);
|
||||||
|
|
||||||
|
|||||||
@ -17,10 +17,9 @@ namespace MaksIT.MongoDB.Linq.Abstractions {
|
|||||||
protected CollectionDataProviderBase(
|
protected CollectionDataProviderBase(
|
||||||
ILogger<T> logger,
|
ILogger<T> logger,
|
||||||
IMongoClient client,
|
IMongoClient client,
|
||||||
IIdGenerator idGenerator,
|
|
||||||
string databaseName,
|
string databaseName,
|
||||||
string collectionName
|
string collectionName
|
||||||
) : base(logger, client, idGenerator, databaseName, collectionName) { }
|
) : base(logger, client, databaseName, collectionName) { }
|
||||||
|
|
||||||
#region Insert
|
#region Insert
|
||||||
public Result<TDtoKey?> Insert(TDtoDocument obj, IClientSessionHandle? session) =>
|
public Result<TDtoKey?> Insert(TDtoDocument obj, IClientSessionHandle? session) =>
|
||||||
|
|||||||
@ -8,7 +8,7 @@
|
|||||||
|
|
||||||
<!-- NuGet package metadata -->
|
<!-- NuGet package metadata -->
|
||||||
<PackageId>MaksIT.MongoDB.Linq</PackageId>
|
<PackageId>MaksIT.MongoDB.Linq</PackageId>
|
||||||
<Version>1.0.2</Version>
|
<Version>1.0.3</Version>
|
||||||
<Authors>Maksym Sadovnychyy</Authors>
|
<Authors>Maksym Sadovnychyy</Authors>
|
||||||
<Company>MAKS-IT</Company>
|
<Company>MAKS-IT</Company>
|
||||||
<Product>MaksIT.MongoDB.Linq</Product>
|
<Product>MaksIT.MongoDB.Linq</Product>
|
||||||
|
|||||||
23
src/MaksIT.MongoDB.Linq/Serializers/GuidSerializer.cs
Normal file
23
src/MaksIT.MongoDB.Linq/Serializers/GuidSerializer.cs
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
33
src/MaksIT.MongoDB.Linq/Serializers/ListGuidSerializer.cs
Normal file
33
src/MaksIT.MongoDB.Linq/Serializers/ListGuidSerializer.cs
Normal 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();
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user