Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 3 additions & 7 deletions src/EntityDb.Common/Envelopes/IEnvelopeService.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
namespace EntityDb.Common.Envelopes;

internal interface IEnvelopeService<TEnvelopeValue>
internal interface IEnvelopeService<TSerializedData>
{
Envelope<TEnvelopeValue> Deconstruct<TData>(TData data);
TSerializedData Serialize<TData>(TData data);

byte[] Serialize(Envelope<TEnvelopeValue> envelope);

Envelope<TEnvelopeValue> Deserialize(byte[] rawData);

TData Reconstruct<TData>(Envelope<TEnvelopeValue> envelope);
TData Deserialize<TData>(TSerializedData serializedData);
}
22 changes: 0 additions & 22 deletions src/EntityDb.Common/Extensions/EnvelopeServiceExtensions.cs

This file was deleted.

2 changes: 1 addition & 1 deletion src/EntityDb.MongoDb/Documents/AgentSignatureDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ ITransaction transaction
TransactionId = transaction.Id,
EntityIds = transaction.Steps.Select(transactionStep => transactionStep.EntityId).Distinct()
.ToArray(),
Data = envelopeService.Deconstruct(transaction.AgentSignature)
Data = envelopeService.Serialize(transaction.AgentSignature)
}
};

Expand Down
2 changes: 1 addition & 1 deletion src/EntityDb.MongoDb/Documents/CommandDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ IAppendCommandTransactionStep appendCommandTransactionStep
TransactionId = transaction.Id,
EntityId = appendCommandTransactionStep.EntityId,
EntityVersionNumber = appendCommandTransactionStep.EntityVersionNumber,
Data = envelopeService.Deconstruct(appendCommandTransactionStep.Command)
Data = envelopeService.Serialize(appendCommandTransactionStep.Command)
}
};

Expand Down
3 changes: 1 addition & 2 deletions src/EntityDb.MongoDb/Documents/DocumentBase.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using EntityDb.Abstractions.ValueObjects;
using EntityDb.Common.Envelopes;
using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;

Expand All @@ -11,5 +10,5 @@ internal abstract record DocumentBase : ITransactionDocument

public TimeStamp TransactionTimeStamp { get; init; }
public Id TransactionId { get; init; }
public Envelope<BsonDocument> Data { get; init; }
public BsonDocument Data { get; init; } = default!;
}
3 changes: 1 addition & 2 deletions src/EntityDb.MongoDb/Documents/ITransactionDocument.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using EntityDb.Abstractions.ValueObjects;
using EntityDb.Common.Envelopes;
using MongoDB.Bson;

namespace EntityDb.MongoDb.Documents;
Expand All @@ -15,5 +14,5 @@ internal interface ITransactionDocument

TimeStamp TransactionTimeStamp { get; }

Envelope<BsonDocument> Data { get; }
BsonDocument Data { get; }
}
2 changes: 1 addition & 1 deletion src/EntityDb.MongoDb/Documents/LeaseDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ IAddLeasesTransactionStep addLeasesTransactionStep
Scope = insertLease.Scope,
Label = insertLease.Label,
Value = insertLease.Value,
Data = envelopeService.Deconstruct(insertLease)
Data = envelopeService.Serialize(insertLease)
})
.ToArray();

Expand Down
2 changes: 1 addition & 1 deletion src/EntityDb.MongoDb/Documents/TagDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ IAddTagsTransactionStep addTagsTransactionStep
EntityVersionNumber = addTagsTransactionStep.EntityVersionNumber,
Label = insertTag.Label,
Value = insertTag.Value,
Data = envelopeService.Deconstruct(insertTag)
Data = envelopeService.Serialize(insertTag)
})
.ToArray();

Expand Down
50 changes: 12 additions & 38 deletions src/EntityDb.MongoDb/Envelopes/BsonDocumentEnvelopeService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,22 @@

namespace EntityDb.MongoDb.Envelopes;

internal class BsonDocumentEnvelopeService : IEnvelopeService<BsonDocument>
internal class MongoDbEnvelopeService : IEnvelopeService<BsonDocument>
{
public const string TypeDiscriminatorPropertyName = "_t";

private readonly ILogger<BsonDocumentEnvelopeService> _logger;
private readonly ILogger<MongoDbEnvelopeService> _logger;
private readonly bool _removeTypeDiscriminatorProperty;
private readonly ITypeResolver _typeResolver;

static BsonDocumentEnvelopeService()
static MongoDbEnvelopeService()
{
BsonSerializer.RegisterSerializer(new EnvelopeSerializer());
}

public BsonDocumentEnvelopeService
public MongoDbEnvelopeService
(
ILogger<BsonDocumentEnvelopeService> logger,
ILogger<MongoDbEnvelopeService> logger,
ITypeResolver typeResolver,
bool removeTypeDiscriminatorProperty
)
Expand All @@ -35,7 +35,7 @@ bool removeTypeDiscriminatorProperty
_removeTypeDiscriminatorProperty = removeTypeDiscriminatorProperty;
}

public Envelope<BsonDocument> Deconstruct<TData>(TData data)
public BsonDocument Serialize<TData>(TData data)
{
try
{
Expand All @@ -48,21 +48,9 @@ public Envelope<BsonDocument> Deconstruct<TData>(TData data)

var headers = EnvelopeHelper.GetEnvelopeHeaders(data!.GetType());

return new Envelope<BsonDocument>(headers, bsonDocument);
}
catch (Exception exception)
{
_logger.LogError(exception, "Unable to deconstruct");

throw new SerializeException();
}
}
var envelope = new Envelope<BsonDocument>(headers, bsonDocument);

public byte[] Serialize(Envelope<BsonDocument> envelope)
{
try
{
return envelope.ToBsonDocument().ToBson();
return envelope.ToBsonDocument();
}
catch (Exception exception)
{
Expand All @@ -72,31 +60,17 @@ public byte[] Serialize(Envelope<BsonDocument> envelope)
}
}

public Envelope<BsonDocument> Deserialize(byte[] rawData)
public TData Deserialize<TData>(BsonDocument serializedData)
{
try
{
var bsonDocument = new RawBsonDocument(rawData);
var envelope = (Envelope<BsonDocument>)BsonSerializer.Deserialize(serializedData, typeof(Envelope<BsonDocument>));

return (Envelope<BsonDocument>)BsonSerializer.Deserialize(bsonDocument, typeof(Envelope<BsonDocument>));
}
catch (Exception exception)
{
_logger.LogError(exception, "Unable to deserialize");

throw new DeserializeException();
}
}

public TData Reconstruct<TData>(Envelope<BsonDocument> envelope)
{
try
{
return (TData)BsonSerializer.Deserialize(envelope.Value, _typeResolver.ResolveType(envelope.Headers));
}
catch (Exception exception)
{
_logger.LogError(exception, "Unable to reconstruct");
_logger.LogError(exception, "Unable to deserialize");

throw new DeserializeException();
}
Expand All @@ -105,7 +79,7 @@ public TData Reconstruct<TData>(Envelope<BsonDocument> envelope)
public static IEnvelopeService<BsonDocument> Create(IServiceProvider serviceProvider,
bool removeTypeDiscriminatorProperty)
{
return ActivatorUtilities.CreateInstance<BsonDocumentEnvelopeService>(serviceProvider,
return ActivatorUtilities.CreateInstance<MongoDbEnvelopeService>(serviceProvider,
removeTypeDiscriminatorProperty);
}
}
6 changes: 3 additions & 3 deletions src/EntityDb.MongoDb/Extensions/DocumentQueryExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ [EnumeratorCancellation] CancellationToken cancellationToken

await foreach (var document in documents)
{
yield return envelopeService.Reconstruct<TData>(document.Data);
yield return envelopeService.Deserialize<TData>(document.Data);
}
}

Expand All @@ -167,7 +167,7 @@ [EnumeratorCancellation] CancellationToken cancellationToken
document.TransactionId,
document.TransactionTimeStamp,
document.EntityIds,
envelopeService.Reconstruct<TData>(document.Data)
envelopeService.Deserialize<TData>(document.Data)
);
}
}
Expand All @@ -192,7 +192,7 @@ [EnumeratorCancellation] CancellationToken cancellationToken
document.TransactionTimeStamp,
document.EntityId,
document.EntityVersionNumber,
envelopeService.Reconstruct<TData>(document.Data)
envelopeService.Deserialize<TData>(document.Data)
);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ internal static void AddBsonDocumentEnvelopeService(this IServiceCollection serv
bool removeTypeDiscriminatorProperty)
{
serviceCollection.AddSingleton(serviceProvider =>
BsonDocumentEnvelopeService.Create(serviceProvider, removeTypeDiscriminatorProperty));
MongoDbEnvelopeService.Create(serviceProvider, removeTypeDiscriminatorProperty));
}

/// <summary>
Expand Down
5 changes: 3 additions & 2 deletions src/EntityDb.MongoDb/Queries/BuilderBase.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
using EntityDb.Common.Envelopes;
using EntityDb.MongoDb.Documents;
using MongoDB.Bson;

namespace EntityDb.MongoDb.Queries;

internal abstract class BuilderBase
{
protected const string DataTypeNameFieldName =
$"{nameof(DocumentBase.Data)}.{nameof(DocumentBase.Data.Headers)}.{EnvelopeHelper.Type}";
$"{nameof(DocumentBase.Data)}.{nameof(Envelope<BsonDocument>.Headers)}.{EnvelopeHelper.Type}";

protected const string DataValueFieldName =
$"{nameof(DocumentBase.Data)}.{nameof(DocumentBase.Data.Value)}";
$"{nameof(DocumentBase.Data)}.{nameof(Envelope<BsonDocument>.Value)}";
}
Loading