From a3c1247c723b7b075ca0acaea51e849c71c5034e Mon Sep 17 00:00:00 2001 From: damon Date: Fri, 26 Jan 2024 08:43:36 +0800 Subject: [PATCH] Change entity id overridable. --- Source/Euonia.Domain/Aggregate.cs | 6 +++--- Source/Euonia.Domain/Commands/Command.cs | 20 +++++++++---------- Source/Euonia.Domain/Entity.cs | 2 +- .../ExceptionHandlingMiddleware.cs | 15 +++++++++++++- project.props | 2 +- 5 files changed, 29 insertions(+), 16 deletions(-) diff --git a/Source/Euonia.Domain/Aggregate.cs b/Source/Euonia.Domain/Aggregate.cs index d1c7587..d72e012 100644 --- a/Source/Euonia.Domain/Aggregate.cs +++ b/Source/Euonia.Domain/Aggregate.cs @@ -14,14 +14,14 @@ public abstract class Aggregate : Entity, IAggregateRoot, IHas /// /// The events. /// - public IEnumerable GetEvents() => _events?.AsReadOnly(); + public virtual IEnumerable GetEvents() => _events?.AsReadOnly(); /// /// Register a handler for the specific event type. /// /// /// - protected void Register(Action when) + protected virtual void Register(Action when) { _handlers.Add(typeof(T), @event => when((T)@event)); } @@ -45,7 +45,7 @@ public virtual void RaiseEvent(TEvent @event) /// /// /// - public void Apply(TEvent @event) + public virtual void Apply(TEvent @event) where TEvent : DomainEvent { if (_handlers.TryGetValue(typeof(TEvent), out var handler)) diff --git a/Source/Euonia.Domain/Commands/Command.cs b/Source/Euonia.Domain/Commands/Command.cs index 2638f7f..d1f1b6d 100644 --- a/Source/Euonia.Domain/Commands/Command.cs +++ b/Source/Euonia.Domain/Commands/Command.cs @@ -21,13 +21,13 @@ protected Command() /// /// Gets the extended properties of command. /// - public IDictionary Properties { get; set; } = new Dictionary(); + public virtual IDictionary Properties { get; set; } = new Dictionary(); /// /// Gets or sets the command property with specified name. /// /// - public string this[string name] + public virtual string this[string name] { get => Properties.TryGetValue(name, out var value) ? value : default; set => Properties[name] = value; @@ -47,7 +47,7 @@ public virtual T GetProperty(string name) /// /// Gets or sets the command identifier. /// - public string CommandId + public virtual string CommandId { get => this[PROPERTY_ID]; set => this[PROPERTY_ID] = value; @@ -85,7 +85,7 @@ protected Command(T1 item1) /// /// /// - public object this[int index] + public virtual object this[int index] { get { @@ -152,7 +152,7 @@ protected Command(T1 item1, T2 item2) /// /// /// - public object this[int index] + public virtual object this[int index] { get { @@ -225,7 +225,7 @@ protected Command(T1 item1, T2 item2, T3 item3) /// /// /// - public object this[int index] + public virtual object this[int index] { get { @@ -304,7 +304,7 @@ protected Command(T1 item1, T2 item2, T3 item3, T4 item4) /// /// /// - public object this[int index] + public virtual object this[int index] { get { @@ -389,7 +389,7 @@ protected Command(T1 item1, T2 item2, T3 item3, T4 item4, T5 item5) /// /// /// - public object this[int index] + public virtual object this[int index] { get { @@ -479,7 +479,7 @@ protected Command(T1 item1, T2 item2, T3 item3, T4 item4, T5 item5, T6 item6) /// /// /// - public object this[int index] + public virtual object this[int index] { get { @@ -576,7 +576,7 @@ protected Command(T1 item1, T2 item2, T3 item3, T4 item4, T5 item5, T6 item6, T7 /// /// /// - public object this[int index] + public virtual object this[int index] { get { diff --git a/Source/Euonia.Domain/Entity.cs b/Source/Euonia.Domain/Entity.cs index 21a793e..4bf5868 100644 --- a/Source/Euonia.Domain/Entity.cs +++ b/Source/Euonia.Domain/Entity.cs @@ -12,7 +12,7 @@ public abstract class Entity : Entity, IEntity /// /// Get or set the entity identifier. /// - public TKey Id { get; set; } + public virtual TKey Id { get; set; } /// public override object[] GetKeys() diff --git a/Source/Euonia.Hosting/Middlewares/ExceptionHandlingMiddleware.cs b/Source/Euonia.Hosting/Middlewares/ExceptionHandlingMiddleware.cs index 010cc89..9ab8b54 100644 --- a/Source/Euonia.Hosting/Middlewares/ExceptionHandlingMiddleware.cs +++ b/Source/Euonia.Hosting/Middlewares/ExceptionHandlingMiddleware.cs @@ -45,7 +45,7 @@ private static Task HandleExceptionAsync(HttpContext httpContext, Exception exce var response = new { status = statusCode, - message = exception is AggregateException ex ? ex.InnerException.Message : exception.Message, + message = GetMessage(exception), details = GetErrors(exception) }; @@ -54,6 +54,18 @@ private static Task HandleExceptionAsync(HttpContext httpContext, Exception exce httpContext.Response.StatusCode = statusCode; return httpContext.Response.WriteAsync(JsonSerializer.Serialize(response)); + + + } + + private static string GetMessage(Exception exception) + { + return exception switch + { + AggregateException ex => ex.InnerException == null ? ex.Message : GetMessage(ex.InnerException), + TargetInvocationException ex => ex.InnerException == null ? ex.Message : GetMessage(ex.InnerException), + _ => exception.Message + }; } private static int GetStatusCode(Exception exception) @@ -66,6 +78,7 @@ private static int GetStatusCode(Exception exception) ValidationException => StatusCodes.Status400BadRequest, NotImplementedException => StatusCodes.Status501NotImplemented, AggregateException ex => GetStatusCode(ex.InnerException), + TargetInvocationException ex => ex.InnerException == null ? StatusCodes.Status500InternalServerError : GetStatusCode(ex.InnerException), _ => (int)(exception.GetType().GetCustomAttribute()?.StatusCode ?? HttpStatusCode.InternalServerError) }; } diff --git a/project.props b/project.props index a7f93a1..a3d5392 100644 --- a/project.props +++ b/project.props @@ -1,6 +1,6 @@ - 8.1.20 + 8.1.21 damon Nerosoft Ltd. Euonia