Skip to content

Commit

Permalink
Merge pull request #66 from NerosoftDev/develop
Browse files Browse the repository at this point in the history
Fixes an object factory bug.
  • Loading branch information
Codespilot committed Jan 7, 2024
2 parents d3fb355 + b11214e commit 5aaa71d
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 20 deletions.
4 changes: 4 additions & 0 deletions Source/Euonia.Application/UseCase/IUseCase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public interface IUseCase<in TInput, TOutput> : IUseCase
/// <returns></returns>
Task<TOutput> ExecuteAsync(TInput input, CancellationToken cancellationToken = default);

/// <inheritdoc />
Task<object> IUseCase.ExecuteAsync(object input, CancellationToken cancellationToken)
=> ExecuteAsync((TInput)input, cancellationToken).ContinueWith(t => (object)t.Result, cancellationToken);
}
Expand All @@ -47,6 +48,7 @@ public interface INonOutputUseCase<in TInput> : IUseCase<TInput, EmptyUseCaseOut
/// <returns></returns>
new Task ExecuteAsync(TInput input, CancellationToken cancellationToken = default);

/// <inheritdoc />
Task<EmptyUseCaseOutput> IUseCase<TInput, EmptyUseCaseOutput>.ExecuteAsync(TInput input, CancellationToken cancellationToken)
=> ExecuteAsync(input, cancellationToken).ContinueWith(_ => EmptyUseCaseOutput.Instance, cancellationToken);
}
Expand All @@ -64,6 +66,7 @@ public interface INonInputUseCase<TOutput> : IUseCase<EmptyUseCaseInput, TOutput
/// <returns></returns>
Task<TOutput> ExecuteAsync(CancellationToken cancellationToken = default);

/// <inheritdoc />
Task<TOutput> IUseCase<EmptyUseCaseInput, TOutput>.ExecuteAsync(EmptyUseCaseInput _, CancellationToken cancellationToken)
=> ExecuteAsync(cancellationToken);
}
Expand All @@ -80,6 +83,7 @@ public interface IParameterlessUseCase : IUseCase<EmptyUseCaseInput, EmptyUseCas
/// <returns></returns>
Task ExecuteAsync(CancellationToken cancellationToken = default);

/// <inheritdoc />
Task<EmptyUseCaseOutput> IUseCase<EmptyUseCaseInput, EmptyUseCaseOutput>.ExecuteAsync(EmptyUseCaseInput _, CancellationToken cancellationToken)
=> ExecuteAsync(cancellationToken).ContinueWith(_ => EmptyUseCaseOutput.Instance, cancellationToken);
}
16 changes: 8 additions & 8 deletions Source/Euonia.Business/Factory/BusinessObjectFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,15 +114,15 @@ public async Task<TTarget> SaveAsync<TTarget>(TTarget target, CancellationToken
{
IEditableObject editableObject => editableObject.State switch
{
ObjectEditState.Insert => ObjectReflector.FindFactoryMethod<TTarget, FactoryInsertAttribute>(Array.Empty<object>()),
ObjectEditState.Update => ObjectReflector.FindFactoryMethod<TTarget, FactoryUpdateAttribute>(Array.Empty<object>()),
ObjectEditState.Delete => ObjectReflector.FindFactoryMethod<TTarget, FactoryDeleteAttribute>(Array.Empty<object>()),
ObjectEditState.Insert => ObjectReflector.FindFactoryMethod<TTarget, FactoryInsertAttribute>(new object[] { cancellationToken }),
ObjectEditState.Update => ObjectReflector.FindFactoryMethod<TTarget, FactoryUpdateAttribute>(new object[] { cancellationToken }),
ObjectEditState.Delete => ObjectReflector.FindFactoryMethod<TTarget, FactoryDeleteAttribute>(new object[] { cancellationToken }),
ObjectEditState.None => throw new InvalidOperationException(),
_ => throw new ArgumentOutOfRangeException(nameof(target), Resources.IDS_INVALID_STATE)
},
ICommandObject => ObjectReflector.FindFactoryMethod<TTarget, FactoryExecuteAttribute>(Array.Empty<object>()),
ICommandObject => ObjectReflector.FindFactoryMethod<TTarget, FactoryExecuteAttribute>(new object[] { cancellationToken }),
IReadOnlyObject => throw new InvalidOperationException("The operation can not apply for ReadOnlyObject."),
_ => ObjectReflector.FindFactoryMethod<TTarget, FactoryUpdateAttribute>(Array.Empty<object>())
_ => ObjectReflector.FindFactoryMethod<TTarget, FactoryUpdateAttribute>(new object[] { cancellationToken })
};

await InvokeAsync(method, target, new object[] { cancellationToken });
Expand All @@ -131,16 +131,16 @@ public async Task<TTarget> SaveAsync<TTarget>(TTarget target, CancellationToken
}

/// <inheritdoc/>
public async Task<TTarget> ExecuteAsync<TTarget>(TTarget command)
public async Task<TTarget> ExecuteAsync<TTarget>(TTarget command, CancellationToken cancellationToken = default)
where TTarget : ICommandObject
{
var method = ObjectReflector.FindFactoryMethod<TTarget, FactoryExecuteAttribute>(Array.Empty<object>());
var method = ObjectReflector.FindFactoryMethod<TTarget, FactoryExecuteAttribute>(new object[] { cancellationToken });
var target = GetObjectInstance<TTarget>();

try
{
_activator?.InitializeInstance(target);
await InvokeAsync(method, target, null);
await InvokeAsync(method, target, new object[] { cancellationToken });
return target;
}
finally
Expand Down
21 changes: 11 additions & 10 deletions Source/Euonia.Business/Factory/IObjectFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,17 @@ public interface IObjectFactory
/// </remarks>
Task<TTarget> UpdateAsync<TTarget>(params object[] criteria);

/// <summary>
/// Invoke the execute method of an exists command object of <typeparamref name="TTarget"/>.
/// </summary>
/// <typeparam name="TTarget">Type of the target object.</typeparam>
/// <param name="command"></param>
/// <returns></returns>
/// <remarks>
/// The method should named as Execute, ExecuteAsync, FactoryExecute, FactoryExecuteAsync, or attributed use <see cref="FactoryExecuteAttribute"/>.
/// </remarks>
Task<TTarget> ExecuteAsync<TTarget>(TTarget command)
/// <summary>
/// Invoke the execute method of an exists command object of <typeparamref name="TTarget"/>.
/// </summary>
/// <typeparam name="TTarget">Type of the target object.</typeparam>
/// <param name="command"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
/// <remarks>
/// The method should named as Execute, ExecuteAsync, FactoryExecute, FactoryExecuteAsync, or attributed use <see cref="FactoryExecuteAttribute"/>.
/// </remarks>
Task<TTarget> ExecuteAsync<TTarget>(TTarget command, CancellationToken cancellationToken = default)
where TTarget : ICommandObject;

/// <summary>
Expand Down
4 changes: 2 additions & 2 deletions project.props
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<Project>
<PropertyGroup>
<Version>8.1.15</Version>
<Version>8.1.16</Version>
<Authors>damon</Authors>
<Company>Nerosoft Ltd.</Company>
<Product>Euonia</Product>
<Copyright>© 2018-2023 Nerosoft. All Rights Reserved.</Copyright>
<Copyright>© 2018-2024 Nerosoft. All Rights Reserved.</Copyright>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>disable</Nullable>
<LangVersion>latest</LangVersion>
Expand Down

0 comments on commit 5aaa71d

Please sign in to comment.