Skip to content

Commit

Permalink
Merge pull request #71 from NerosoftDev/develop
Browse files Browse the repository at this point in the history
Change entity id overridable.
  • Loading branch information
Codespilot committed Jan 26, 2024
2 parents b0ae1a4 + a3c1247 commit 53e86ab
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 16 deletions.
6 changes: 3 additions & 3 deletions Source/Euonia.Domain/Aggregate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ public abstract class Aggregate<TKey> : Entity<TKey>, IAggregateRoot<TKey>, IHas
/// <summary>
/// The events.
/// </summary>
public IEnumerable<DomainEvent> GetEvents() => _events?.AsReadOnly();
public virtual IEnumerable<DomainEvent> GetEvents() => _events?.AsReadOnly();

/// <summary>
/// Register a handler for the specific event type.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="when"></param>
protected void Register<T>(Action<T> when)
protected virtual void Register<T>(Action<T> when)
{
_handlers.Add(typeof(T), @event => when((T)@event));
}
Expand All @@ -45,7 +45,7 @@ public virtual void RaiseEvent<TEvent>(TEvent @event)
/// </summary>
/// <typeparam name="TEvent"></typeparam>
/// <param name="event"></param>
public void Apply<TEvent>(TEvent @event)
public virtual void Apply<TEvent>(TEvent @event)
where TEvent : DomainEvent
{
if (_handlers.TryGetValue(typeof(TEvent), out var handler))
Expand Down
20 changes: 10 additions & 10 deletions Source/Euonia.Domain/Commands/Command.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ protected Command()
/// <summary>
/// Gets the extended properties of command.
/// </summary>
public IDictionary<string, string> Properties { get; set; } = new Dictionary<string, string>();
public virtual IDictionary<string, string> Properties { get; set; } = new Dictionary<string, string>();

/// <summary>
/// Gets or sets the command property with specified name.
/// </summary>
/// <param name="name"></param>
public string this[string name]
public virtual string this[string name]
{
get => Properties.TryGetValue(name, out var value) ? value : default;
set => Properties[name] = value;
Expand All @@ -47,7 +47,7 @@ public virtual T GetProperty<T>(string name)
/// <summary>
/// Gets or sets the command identifier.
/// </summary>
public string CommandId
public virtual string CommandId
{
get => this[PROPERTY_ID];
set => this[PROPERTY_ID] = value;
Expand Down Expand Up @@ -85,7 +85,7 @@ protected Command(T1 item1)
/// <param name="index"></param>
/// <returns></returns>
/// <exception cref="IndexOutOfRangeException"></exception>
public object this[int index]
public virtual object this[int index]
{
get
{
Expand Down Expand Up @@ -152,7 +152,7 @@ protected Command(T1 item1, T2 item2)
/// <param name="index"></param>
/// <returns></returns>
/// <exception cref="IndexOutOfRangeException"></exception>
public object this[int index]
public virtual object this[int index]
{
get
{
Expand Down Expand Up @@ -225,7 +225,7 @@ protected Command(T1 item1, T2 item2, T3 item3)
/// <param name="index"></param>
/// <returns></returns>
/// <exception cref="IndexOutOfRangeException"></exception>
public object this[int index]
public virtual object this[int index]
{
get
{
Expand Down Expand Up @@ -304,7 +304,7 @@ protected Command(T1 item1, T2 item2, T3 item3, T4 item4)
/// <param name="index"></param>
/// <returns></returns>
/// <exception cref="IndexOutOfRangeException"></exception>
public object this[int index]
public virtual object this[int index]
{
get
{
Expand Down Expand Up @@ -389,7 +389,7 @@ protected Command(T1 item1, T2 item2, T3 item3, T4 item4, T5 item5)
/// <param name="index"></param>
/// <returns></returns>
/// <exception cref="IndexOutOfRangeException"></exception>
public object this[int index]
public virtual object this[int index]
{
get
{
Expand Down Expand Up @@ -479,7 +479,7 @@ protected Command(T1 item1, T2 item2, T3 item3, T4 item4, T5 item5, T6 item6)
/// </summary>
/// <param name="index"></param>
/// <exception cref="IndexOutOfRangeException"></exception>
public object this[int index]
public virtual object this[int index]
{
get
{
Expand Down Expand Up @@ -576,7 +576,7 @@ protected Command(T1 item1, T2 item2, T3 item3, T4 item4, T5 item5, T6 item6, T7
/// <param name="index"></param>
/// <returns></returns>
/// <exception cref="IndexOutOfRangeException"></exception>
public object this[int index]
public virtual object this[int index]
{
get
{
Expand Down
2 changes: 1 addition & 1 deletion Source/Euonia.Domain/Entity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public abstract class Entity<TKey> : Entity, IEntity<TKey>
/// <summary>
/// Get or set the entity identifier.
/// </summary>
public TKey Id { get; set; }
public virtual TKey Id { get; set; }

/// <inheritdoc/>
public override object[] GetKeys()
Expand Down
15 changes: 14 additions & 1 deletion Source/Euonia.Hosting/Middlewares/ExceptionHandlingMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
};

Expand All @@ -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)
Expand All @@ -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<HttpStatusCodeAttribute>()?.StatusCode ?? HttpStatusCode.InternalServerError)
};
}
Expand Down
2 changes: 1 addition & 1 deletion project.props
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project>
<PropertyGroup>
<Version>8.1.20</Version>
<Version>8.1.21</Version>
<Authors>damon</Authors>
<Company>Nerosoft Ltd.</Company>
<Product>Euonia</Product>
Expand Down

0 comments on commit 53e86ab

Please sign in to comment.