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
2 changes: 1 addition & 1 deletion src/BuildingBlocks/MASA.BuildingBlocks
23 changes: 17 additions & 6 deletions src/Ddd/Masa.Contrib.Ddd.Domain/Events/DomainCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,27 @@

namespace Masa.Contrib.Ddd.Domain.Events;

public record DomainCommand(Guid Id, DateTime CreationTime) : IDomainCommand
public record DomainCommand : IDomainCommand
{
[JsonIgnore]
public Guid Id { get; } = Id;

[JsonIgnore]
public DateTime CreationTime { get; } = CreationTime;
private Guid _eventId;
private DateTime _creationTime;

[JsonIgnore]
public IUnitOfWork? UnitOfWork { get; set; }

public DomainCommand() : this(Guid.NewGuid(), DateTime.UtcNow) { }

public DomainCommand(Guid eventId, DateTime creationTime)
{
_eventId = eventId;
_creationTime = creationTime;
}

public Guid GetEventId() => _eventId;

public void SetEventId(Guid eventId) => _eventId = eventId;

public DateTime GetCreationTime() => _creationTime;

public void SetCreationTime(DateTime creationTime) => _creationTime = creationTime;
}
22 changes: 16 additions & 6 deletions src/Ddd/Masa.Contrib.Ddd.Domain/Events/DomainEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,27 @@

namespace Masa.Contrib.Ddd.Domain.Events;

public record DomainEvent(Guid Id, DateTime CreationTime) : IDomainEvent
public record DomainEvent : IDomainEvent
{
[JsonIgnore]
public Guid Id { get; } = Id;

[JsonIgnore]
public DateTime CreationTime { get; } = CreationTime;
private Guid _eventId;
private DateTime _creationTime;

[JsonIgnore]
public IUnitOfWork? UnitOfWork { get; set; }

public DomainEvent() : this(Guid.NewGuid(), DateTime.UtcNow) { }

public DomainEvent(Guid eventId, DateTime creationTime)
{
_eventId = eventId;
_creationTime = creationTime;
}

public Guid GetEventId() => _eventId;

public void SetEventId(Guid eventId) => _eventId = eventId;

public DateTime GetCreationTime() => _creationTime;

public void SetCreationTime(DateTime creationTime) => _creationTime = creationTime;
}
23 changes: 17 additions & 6 deletions src/Ddd/Masa.Contrib.Ddd.Domain/Events/DomainQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,11 @@

namespace Masa.Contrib.Ddd.Domain.Events;

public abstract record DomainQuery<TResult>(Guid Id, DateTime CreationTime) : IDomainQuery<TResult>
public abstract record DomainQuery<TResult> : IDomainQuery<TResult>
where TResult : notnull
{
[JsonIgnore]
public Guid Id { get; } = Id;

[JsonIgnore]
public DateTime CreationTime { get; } = CreationTime;
private Guid _eventId;
private DateTime _creationTime;

[JsonIgnore]
public IUnitOfWork? UnitOfWork
Expand All @@ -22,4 +19,18 @@ public IUnitOfWork? UnitOfWork
public abstract TResult Result { get; set; }

public DomainQuery() : this(Guid.NewGuid(), DateTime.UtcNow) { }

public DomainQuery(Guid eventId, DateTime creationTime)
{
_eventId = eventId;
_creationTime = creationTime;
}

public Guid GetEventId() => _eventId;

public void SetEventId(Guid eventId) => _eventId = eventId;

public DateTime GetCreationTime() => _creationTime;

public void SetCreationTime(DateTime creationTime) => _creationTime = creationTime;
}
23 changes: 17 additions & 6 deletions src/Dispatcher/Masa.Contrib.Dispatcher.Events/Event.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,24 @@

namespace Masa.Contrib.Dispatcher.Events;

public record Event(Guid Id, DateTime CreationTime) : IEvent
public record Event : IEvent
{
[JsonIgnore]
public Guid Id { get; } = Id;

[JsonIgnore]
public DateTime CreationTime { get; } = CreationTime;
private Guid _eventId;
private DateTime _creationTime;

public Event() : this(Guid.NewGuid(), DateTime.UtcNow) { }

public Event(Guid eventId, DateTime creationTime)
{
_eventId = eventId;
_creationTime = creationTime;
}

public Guid GetEventId() => _eventId;

public void SetEventId(Guid eventId) => _eventId = eventId;

public DateTime GetCreationTime() => _creationTime;

public void SetCreationTime(DateTime creationTime) => _creationTime = creationTime;
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ private async Task ExecuteEventHandlerAsync<TEvent>(IServiceProvider serviceProv

await executionStrategy.ExecuteAsync(strategyOptions, @event, async (@event) =>
{
Logger?.LogDebug("----- Publishing event {@Event}: message id: {messageId} -----", @event, @event.Id);
Logger?.LogDebug("----- Publishing event {@Event}: message id: {messageId} -----", @event, @event.GetEventId());
await dispatchHandler.ExcuteAction(serviceProvider, @event);
}, async (@event, ex, failureLevels) =>
{
Expand All @@ -76,7 +76,7 @@ await executionStrategy.ExecuteAsync(strategyOptions, @event, async (@event) =>
}
else
{
Logger?.LogError("----- Publishing event {@Event} error rollback is ignored: message id: {messageId} -----", @event, @event.Id);
Logger?.LogError("----- Publishing event {@Event} error rollback is ignored: message id: {messageId} -----", @event, @event.GetEventId());
}
});
}
Expand All @@ -95,14 +95,14 @@ private async Task ExecuteEventCanceledHandlerAsync<TEvent>(IServiceProvider ser
strategyOptions.SetStrategy(cancelHandler);
await executionStrategy.ExecuteAsync(strategyOptions, @event, async @event =>
{
logger?.LogDebug("----- Publishing event {@Event} rollback start: message id: {messageId} -----", @event, @event.Id);
logger?.LogDebug("----- Publishing event {@Event} rollback start: message id: {messageId} -----", @event, @event.GetEventId());
await cancelHandler.ExcuteAction(serviceProvider, @event);
}, (@event, ex, failureLevels) =>
{
if (failureLevels != FailureLevels.Ignore)
ex.ThrowException();

logger?.LogError("----- Publishing event {@Event} rollback error ignored: message id: {messageId} -----", @event, @event.Id);
logger?.LogError("----- Publishing event {@Event} rollback error ignored: message id: {messageId} -----", @event, @event.GetEventId());
return Task.CompletedTask;
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public async Task ExecuteAsync<TEvent>(StrategyOptions strategyOptions, TEvent @
{
if (retryTimes > 0)
{
_logger?.LogWarning("----- Error Publishing event {@Event} start: The {retries}th retrying consume a message failed. message id: {messageId} -----", @event, retryTimes, @event.Id);
_logger?.LogWarning("----- Error Publishing event {@Event} start: The {retries}th retrying consume a message failed. message id: {messageId} -----", @event, retryTimes, @event.GetEventId());
}
await func.Invoke(@event);
return;
Expand All @@ -30,11 +30,11 @@ public async Task ExecuteAsync<TEvent>(StrategyOptions strategyOptions, TEvent @
{
if (retryTimes > 0)
{
_logger?.LogError(ex, "----- Error Publishing event {@Event} finish: The {retries}th retrying consume a message failed. message id: {messageId} -----", @event, retryTimes, @event.Id);
_logger?.LogError(ex, "----- Error Publishing event {@Event} finish: The {retries}th retrying consume a message failed. message id: {messageId} -----", @event, retryTimes, @event.GetEventId());
}
else
{
_logger?.LogError(ex, "----- Error Publishing event {@Event}: after {maxRetries}th executions and we will stop retrying. message id: {messageId} -----", @event, strategyOptions.MaxRetryCount, @event.Id);
_logger?.LogError(ex, "----- Error Publishing event {@Event}: after {maxRetries}th executions and we will stop retrying. message id: {messageId} -----", @event, strategyOptions.MaxRetryCount, @event.GetEventId());
}
exception = ex;
retryTimes++;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,10 @@

namespace Masa.Contrib.Dispatcher.IntegrationEvents.Dapr;

public abstract record IntegrationEvent(Guid Id, DateTime CreationTime) : IIntegrationEvent
public abstract record IntegrationEvent : IIntegrationEvent
{
[JsonIgnore]
public Guid Id { get; } = Id;

[JsonIgnore]
public DateTime CreationTime { get; } = CreationTime;
private Guid _eventId;
private DateTime _creationTime;

[JsonIgnore]
public IUnitOfWork? UnitOfWork { get; set; }
Expand All @@ -18,4 +15,18 @@ public abstract record IntegrationEvent(Guid Id, DateTime CreationTime) : IInteg
public abstract string Topic { get; set; }

public IntegrationEvent() : this(Guid.NewGuid(), DateTime.UtcNow) { }

protected IntegrationEvent(Guid eventId, DateTime creationTime)
{
_eventId = eventId;
_creationTime = creationTime;
}

public Guid GetEventId() => _eventId;

public void SetEventId(Guid eventId) => _eventId = eventId;

public DateTime GetCreationTime() => _creationTime;

public void SetCreationTime(DateTime creationTime) => _creationTime = creationTime;
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,32 +65,32 @@ private async Task PublishIntegrationAsync<TEvent>(TEvent @event)
{
try
{
_logger?.LogDebug("----- Saving changes and integrationEvent: {IntegrationEventId}", @event.Id);
_logger?.LogDebug("----- Saving changes and integrationEvent: {IntegrationEventId}", @event.GetEventId());
await _eventLogService.SaveEventAsync(@event, @event.UnitOfWork!.Transaction);

_logger?.LogDebug(
"----- Publishing integration event: {IntegrationEventIdPublished} from {AppId} - ({IntegrationEvent})", @event.Id,
"----- Publishing integration event: {IntegrationEventIdPublished} from {AppId} - ({IntegrationEvent})", @event.GetEventId(),
_appConfig?.CurrentValue.AppId ?? string.Empty, @event);

await _eventLogService.MarkEventAsInProgressAsync(@event.Id);
await _eventLogService.MarkEventAsInProgressAsync(@event.GetEventId());

_logger?.LogDebug("Publishing event {Event} to {PubsubName}.{TopicName}", @event, _daprPubsubName, topicName);
await _dapr.PublishEventAsync(_daprPubsubName, topicName, (dynamic)@event);

await _eventLogService.MarkEventAsPublishedAsync(@event.Id);
await _eventLogService.MarkEventAsPublishedAsync(@event.GetEventId());
}
catch (Exception ex)
{
_logger?.LogError(ex, "Error Publishing integration event: {IntegrationEventId} from {AppId} - ({IntegrationEvent})",
@event.Id, _appConfig?.CurrentValue.AppId ?? string.Empty, @event);
LocalQueueProcessor.Default.AddJobs(new IntegrationEventLogItem(@event.Id, @event.Topic, @event));
await _eventLogService.MarkEventAsFailedAsync(@event.Id);
@event.GetEventId(), _appConfig?.CurrentValue.AppId ?? string.Empty, @event);
LocalQueueProcessor.Default.AddJobs(new IntegrationEventLogItem(@event.GetEventId(), @event.Topic, @event));
await _eventLogService.MarkEventAsFailedAsync(@event.GetEventId());
}
}
else
{
_logger?.LogDebug(
"----- Publishing integration event (don't use local message): {IntegrationEventIdPublished} from {AppId} - ({IntegrationEvent})", @event.Id,
"----- Publishing integration event (don't use local message): {IntegrationEventIdPublished} from {AppId} - ({IntegrationEvent})", @event.GetEventId(),
_appConfig?.CurrentValue.AppId ?? string.Empty, @event);

await _dapr.PublishEventAsync(_daprPubsubName, topicName, (dynamic)@event);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,27 @@

namespace Masa.Contrib.ReadWriteSpliting.Cqrs.Commands;

public record Command(Guid Id, DateTime CreationTime) : ICommand
public record Command : ICommand
{
[JsonIgnore]
public Guid Id { get; } = Id;

[JsonIgnore]
public DateTime CreationTime { get; } = CreationTime;
private Guid _eventId;
private DateTime _creationTime;

[JsonIgnore]
public IUnitOfWork? UnitOfWork { get; set; }

public Command() : this(Guid.NewGuid(), DateTime.UtcNow) { }

public Command(Guid eventId, DateTime creationTime)
{
_eventId = eventId;
_creationTime = creationTime;
}

public Guid GetEventId() => _eventId;

public void SetEventId(Guid eventId) => _eventId = eventId;

public DateTime GetCreationTime() => _creationTime;

public void SetCreationTime(DateTime creationTime) => _creationTime = creationTime;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,27 @@

namespace Masa.Contrib.ReadWriteSpliting.Cqrs.Queries;

public abstract record Query<TResult>(Guid Id, DateTime CreationTime) : IQuery<TResult>
public abstract record Query<TResult> : IQuery<TResult>
where TResult : notnull
{
[JsonIgnore]
public Guid Id { get; } = Id;

[JsonIgnore]
public DateTime CreationTime { get; } = CreationTime;
private Guid _eventId;
private DateTime _creationTime;

public abstract TResult Result { get; set; }

public Query() : this(Guid.NewGuid(), DateTime.UtcNow) { }

public Query(Guid eventId, DateTime creationTime)
{
_eventId = eventId;
_creationTime = creationTime;
}

public Guid GetEventId() => _eventId;

public void SetEventId(Guid eventId) => _eventId = eventId;

public DateTime GetCreationTime() => _creationTime;

public void SetCreationTime(DateTime creationTime) => _creationTime = creationTime;
}
20 changes: 10 additions & 10 deletions test/Masa.Contrib.Ddd.Domain.Tests/DomainEventBusTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -264,27 +264,27 @@ public void TestParameterInitialization()
var createTime = DateTime.UtcNow;

var domainCommand = new DomainCommand();
Assert.IsTrue(domainCommand.Id != default);
Assert.IsTrue(domainCommand.CreationTime != default && domainCommand.CreationTime >= createTime);
Assert.IsTrue(domainCommand.GetEventId() != default);
Assert.IsTrue(domainCommand.GetCreationTime() != default && domainCommand.GetCreationTime() >= createTime);

domainCommand = new DomainCommand(id, createTime);
Assert.IsTrue(domainCommand.Id == id);
Assert.IsTrue(domainCommand.CreationTime == createTime);
Assert.IsTrue(domainCommand.GetEventId() == id);
Assert.IsTrue(domainCommand.GetCreationTime() == createTime);

var domainEvent = new DomainEvent();
Assert.IsTrue(domainEvent.Id != default);
Assert.IsTrue(domainEvent.CreationTime != default && domainEvent.CreationTime >= createTime);
Assert.IsTrue(domainEvent.GetEventId() != default);
Assert.IsTrue(domainEvent.GetCreationTime() != default && domainEvent.GetCreationTime() >= createTime);

domainEvent = new DomainEvent(id, createTime);
Assert.IsTrue(domainEvent.Id == id);
Assert.IsTrue(domainEvent.CreationTime == createTime);
Assert.IsTrue(domainEvent.GetEventId() == id);
Assert.IsTrue(domainEvent.GetCreationTime() == createTime);

var domainQuery = new ProductItemDomainQuery()
{
ProductId = Guid.NewGuid().ToString()
};
Assert.IsTrue(domainQuery.Id != default);
Assert.IsTrue(domainQuery.CreationTime != default && domainQuery.CreationTime >= createTime);
Assert.IsTrue(domainQuery.GetEventId() != default);
Assert.IsTrue(domainQuery.GetCreationTime() != default && domainQuery.GetCreationTime() >= createTime);
}

[TestMethod]
Expand Down
12 changes: 10 additions & 2 deletions test/Masa.Contrib.Ddd.Domain.Tests/Events/ForgetPasswordEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,17 @@ namespace Masa.Contrib.Ddd.Domain.Tests.Events;

public class ForgetPasswordEvent : IEvent
{
public Guid Id { get; init; } = Guid.NewGuid();
public Guid Id { get; set; } = Guid.NewGuid();

public DateTime CreationTime { get; init; } = DateTime.UtcNow;
public DateTime CreationTime { get; set; } = DateTime.UtcNow;

public string Account { get; set; }

public Guid GetEventId() => Id;

public void SetEventId(Guid eventId) => Id = eventId;

public DateTime GetCreationTime() => CreationTime;

public void SetCreationTime(DateTime creationTime) => CreationTime = creationTime;
}
Loading