Skip to content

Commit

Permalink
refactor: ♻️ enhance masstransit messaging registration (#235)
Browse files Browse the repository at this point in the history
  • Loading branch information
mehdihadeli authored Aug 20, 2024
1 parent f5f13fa commit 75c7fa9
Show file tree
Hide file tree
Showing 100 changed files with 1,415 additions and 826 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Riok.Mapperly"/>
<PackageReference Include="AutoMapper"/>
<PackageReference Include="MediatR"/>
<PackageReference Include="MongoDB.Driver"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,5 @@ protected AggregateId(long value)
return new AggregateId(value);
}

public static implicit operator long(AggregateId id)
{
return id.Value;
}
public static implicit operator long(AggregateId? id) => id?.Value ?? default;
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,26 @@
namespace BuildingBlocks.Abstractions.Events;

public interface IEventEnvelopeMetadata
public record EventEnvelopeMetadata(
Guid MessageId,
Guid CorrelationId,
string MessageType,
string Name,
// Causation ID identifies messages that cause other messages to be published. In simple terms, it's used to see what causes what. The first message in a message conversation typically doesn't have a causation ID. Downstream messages get their causation IDs by copying message IDs from messages, causing downstream messages to be published
Guid? CausationId
)
{
Guid MessageId { get; init; }
string MessageType { get; init; }
string Name { get; init; }
Guid? CausationId { get; init; }
Guid CorrelationId { get; init; }
DateTime Created { get; init; }
long? CreatedUnixTime { get; init; }
IDictionary<string, object?> Headers { get; init; }
public IDictionary<string, object?> Headers { get; init; } = new Dictionary<string, object?>();
public DateTime Created { get; init; } = DateTime.Now;
public long? CreatedUnixTime { get; init; } = DateTimeHelper.ToUnixTimeSecond(DateTime.Now);

internal static class DateTimeHelper
{
private static readonly DateTime _epoch = new(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);

public static long ToUnixTimeSecond(DateTime datetime)
{
var unixTime = (datetime.ToUniversalTime() - _epoch).TotalSeconds;
return (long)unixTime;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ namespace BuildingBlocks.Abstractions.Events;
// Ref: https://www.enterpriseintegrationpatterns.com/patterns/messaging/EnvelopeWrapper.html
public interface IEventEnvelope
{
object Data { get; }
IEventEnvelopeMetadata? Metadata { get; }
object Message { get; }
EventEnvelopeMetadata Metadata { get; }
}

public interface IEventEnvelope<out T> : IEventEnvelope
where T : notnull
{
new T Data { get; }
new T Message { get; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using BuildingBlocks.Abstractions.Events;

namespace BuildingBlocks.Abstractions.Messaging;

public interface IBusDirectPublisher
{
Task PublishAsync<TMessage>(IEventEnvelope<TMessage> eventEnvelope, CancellationToken cancellationToken = default)
where TMessage : IMessage;

Task PublishAsync(IEventEnvelope eventEnvelope, CancellationToken cancellationToken = default);

public Task PublishAsync<TMessage>(
IEventEnvelope<TMessage> eventEnvelope,
string? exchangeOrTopic = null,
string? queue = null,
CancellationToken cancellationToken = default
)
where TMessage : IMessage;

public Task PublishAsync(
IEventEnvelope eventEnvelope,
string? exchangeOrTopic = null,
string? queue = null,
CancellationToken cancellationToken = default
);
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using BuildingBlocks.Abstractions.Events;

namespace BuildingBlocks.Abstractions.Messaging;

public interface IBusPublisher
{
public Task PublishAsync<TMessage>(TMessage message, CancellationToken cancellationToken = default)
where TMessage : IMessage;

Task PublishAsync<TMessage>(IEventEnvelope<TMessage> eventEnvelope, CancellationToken cancellationToken = default)
where TMessage : IMessage;

public Task PublishAsync<TMessage>(
TMessage message,
string? exchangeOrTopic = null,
string? queue = null,
CancellationToken cancellationToken = default
)
where TMessage : IMessage;

public Task PublishAsync<TMessage>(
IEventEnvelope<TMessage> eventEnvelope,
string? exchangeOrTopic = null,
string? queue = null,
CancellationToken cancellationToken = default
)
where TMessage : IMessage;
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
namespace BuildingBlocks.Abstractions.Messaging;

public interface IExternalEventBus : IBusProducer, IBusConsumer;
public interface IExternalEventBus : IBusPublisher, IBusConsumer;
Original file line number Diff line number Diff line change
Expand Up @@ -20,30 +20,23 @@ Task<IReadOnlyList<StoreMessage>> GetByFilterAsync(
CancellationToken cancellationToken = default
);

Task AddPublishMessageAsync<TEventEnvelope>(
TEventEnvelope eventEnvelope,
Task AddPublishMessageAsync<TMessage>(
IEventEnvelope<TMessage> eventEnvelope,
CancellationToken cancellationToken = default
)
where TEventEnvelope : IEventEnvelope;

Task AddPublishMessageAsync<TEventEnvelope, TMessage>(
TEventEnvelope eventEnvelope,
CancellationToken cancellationToken = default
)
where TEventEnvelope : IEventEnvelope<TMessage>
where TMessage : IMessage;

Task AddReceivedMessageAsync<TMessageEnvelope>(
TMessageEnvelope messageEnvelope,
Task AddReceivedMessageAsync<TMessage>(
IEventEnvelope<TMessage> eventEnvelope,
CancellationToken cancellationToken = default
)
where TMessageEnvelope : IEventEnvelope;
where TMessage : IMessage;

Task AddInternalMessageAsync<TInternalCommand>(
TInternalCommand internalCommand,
CancellationToken cancellationToken = default
)
where TInternalCommand : class, IInternalCommand;
where TInternalCommand : IInternalCommand;

Task AddNotificationAsync<TDomainNotification>(
TDomainNotification notification,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using BuildingBlocks.Abstractions.Events;
using BuildingBlocks.Abstractions.Messaging;

namespace BuildingBlocks.Abstractions.Serialization;

Expand All @@ -12,12 +13,23 @@ public interface IMessageSerializer
/// <param name="eventEnvelope">a messageEnvelope that implement IMessage interface.</param>
/// <returns>a json string for serialized messageEnvelope.</returns>
string Serialize(IEventEnvelope eventEnvelope);
string Serialize<T>(IEventEnvelope<T> eventEnvelope)
where T : IMessage;

/// <summary>
/// Deserialize the given payload into a <see cref="IEventEnvelope" />.
/// </summary>
/// <param name="eventEnvelope">a json data to deserialize to a messageEnvelope.</param>
/// <param name="messageType">the type of message inside event-envelope.</param>
/// <returns>return a messageEnvelope type.</returns>
IEventEnvelope? Deserialize(string eventEnvelope);
IEventEnvelope? Deserialize(string eventEnvelope, Type messageType);

/// <summary>
/// Deserialize the given payload into a IEventEnvelope" />.
/// </summary>
/// <param name="eventEnvelope"></param>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
IEventEnvelope<T>? Deserialize<T>(string eventEnvelope)
where T : IMessage;
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ public abstract class AuditAggregate<TId> : Aggregate<TId>, IAuditableEntity<TId
}

public abstract class AuditAggregate<TIdentity, TId> : AuditAggregate<TIdentity>
where TIdentity : Identity<TId> { }
where TIdentity : Identity<TId>;

public abstract class AuditAggregate : AuditAggregate<Identity<long>, long> { }
public abstract class AuditAggregate : AuditAggregate<Identity<long>, long>;
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ namespace BuildingBlocks.Core.Domain;

public class AuditableEntity<TId> : Entity<TId>, IAuditableEntity<TId>
{
public DateTime? LastModified { get; protected set; } = default!;
public int? LastModifiedBy { get; protected set; } = default!;
public DateTime? LastModified { get; init; } = default!;
public int? LastModifiedBy { get; init; } = default!;
}

public abstract class AuditableEntity<TIdentity, TId> : AuditableEntity<TIdentity>
where TIdentity : Identity<TId> { }
where TIdentity : Identity<TId>;

public class AuditableEntity : AuditableEntity<Identity<long>, long> { }
public class AuditableEntity : AuditableEntity<Identity<long>, long>;
10 changes: 5 additions & 5 deletions src/BuildingBlocks/BuildingBlocks.Core/Domain/Entity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ namespace BuildingBlocks.Core.Domain;

public abstract class Entity<TId> : IEntity<TId>
{
public TId Id { get; protected set; } = default!;
public DateTime Created { get; private set; } = default!;
public int? CreatedBy { get; private set; } = default!;
public TId Id { get; init; } = default!;
public DateTime Created { get; init; } = default!;
public int? CreatedBy { get; init; } = default!;
}

public abstract class Entity<TIdentity, TId> : Entity<TIdentity>
where TIdentity : Identity<TId> { }
where TIdentity : Identity<TId>;

public abstract class Entity : Entity<EntityId, long>, IEntity { }
public abstract class Entity : Entity<EntityId, long>, IEntity;
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,5 @@ public PostalCode() { }
// validations should be placed here instead of constructor
public static PostalCode Of(string? postalCode) => new() { Value = postalCode.NotBeNullOrWhiteSpace() };

public static implicit operator string(PostalCode postalCode) => postalCode.Value;
public static implicit operator string(PostalCode? postalCode) => postalCode?.Value ?? string.Empty;
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public static Amount Of([NotNull] decimal? value)
return Of(value.Value);
}

public static Amount Of(decimal value)
public static Amount Of([NotNull] decimal value)
{
value.NotBeNegativeOrZero();

Expand All @@ -42,7 +42,7 @@ public static Amount Of(decimal value)
return new Amount(value);
}

public static implicit operator decimal(Amount value) => value.Value;
public static implicit operator decimal(Amount? value) => value?.Value ?? default;

public static bool operator >(Amount a, Amount b) => a.Value > b.Value;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public static BirthDate Of(DateTime value)
return new BirthDate { Value = value };
}

public static implicit operator DateTime(BirthDate value) => value.Value;
public static implicit operator DateTime(BirthDate? value) => value?.Value ?? default;

// https://learn.microsoft.com/en-us/dotnet/csharp/fundamentals/functional/deconstruct#user-defined-types
// https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/record#positional-syntax-for-property-definition
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public static Currency Of([NotNull] string? value)
return new Currency(value);
}

public static implicit operator string(Currency value) => value.Value;
public static implicit operator string(Currency? value) => value?.Value ?? string.Empty;

// https://learn.microsoft.com/en-us/dotnet/csharp/fundamentals/functional/deconstruct#user-defined-types
// https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/record#positional-syntax-for-property-definition
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public static Email Of([NotNull] string? value)
return new Email(value);
}

public static implicit operator string(Email value) => value.Value;
public static implicit operator string(Email? value) => value?.Value ?? string.Empty;

// https://learn.microsoft.com/en-us/dotnet/csharp/fundamentals/functional/deconstruct#user-defined-types
// https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/record#positional-syntax-for-property-definition
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public static MobileNumber Of([NotNull] string? value)
return new MobileNumber(value);
}

public static implicit operator string(MobileNumber phoneNumber) => phoneNumber.Value;
public static implicit operator string(MobileNumber? phoneNumber) => phoneNumber?.Value ?? string.Empty;

// https://learn.microsoft.com/en-us/dotnet/csharp/fundamentals/functional/deconstruct#user-defined-types
// https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/record#positional-syntax-for-property-definition
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public static PhoneNumber Of([NotNull] string? value)
return new PhoneNumber(value);
}

public static implicit operator string(PhoneNumber phoneNumber) => phoneNumber.Value;
public static implicit operator string(PhoneNumber? phoneNumber) => phoneNumber?.Value ?? string.Empty;

// https://learn.microsoft.com/en-us/dotnet/csharp/fundamentals/functional/deconstruct#user-defined-types
// https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/record#positional-syntax-for-property-definition
Expand Down
Loading

0 comments on commit 75c7fa9

Please sign in to comment.