Skip to content

Commit

Permalink
Merge pull request #84 from NerosoftDev/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
Codespilot authored Sep 22, 2024
2 parents 94c6cc7 + e7a3cd1 commit 7971d1d
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 110 deletions.
10 changes: 5 additions & 5 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
</PropertyGroup>
<ItemGroup>
<PackageVersion Include="Apache.NMS.ActiveMQ" Version="2.1.0" />
<PackageVersion Include="Autofac" Version="8.0.0" />
<PackageVersion Include="Autofac.Extensions.DependencyInjection" Version="9.0.0" />
<PackageVersion Include="Autofac" Version="8.1.0" />
<PackageVersion Include="Autofac.Extensions.DependencyInjection" Version="10.0.0" />
<PackageVersion Include="Autofac.Extras.DynamicProxy" Version="7.1.0" />
<PackageVersion Include="AutoMapper" Version="13.0.1" />
<PackageVersion Include="Azure.Storage.Blobs" Version="12.21.2" />
Expand Down Expand Up @@ -41,7 +41,7 @@
<PackageVersion Include="Serilog.Sinks.Elasticsearch" Version="10.0.0" />
<PackageVersion Include="Serilog.Sinks.Email" Version="4.0.0" />
<PackageVersion Include="Serilog.Sinks.File" Version="6.0.0" />
<PackageVersion Include="StackExchange.Redis" Version="2.8.0" />
<PackageVersion Include="StackExchange.Redis" Version="2.8.14" />
<PackageVersion Include="Swashbuckle.AspNetCore" Version="6.7.3" />
<PackageVersion Include="System.Collections.Immutable" Version="8.0.0" />
<PackageVersion Include="System.Configuration.ConfigurationManager" Version="7.0.0" />
Expand Down Expand Up @@ -112,7 +112,7 @@
<!-- Grpc -->
<ItemGroup>
<PackageVersion Include="Google.Api.CommonProtos" Version="2.12.0" />
<PackageVersion Include="Google.Protobuf" Version="3.27.3" />
<PackageVersion Include="Google.Protobuf" Version="3.28.0" />
<PackageVersion Include="Grpc" Version="2.46.6" />
<PackageVersion Include="Grpc.AspNetCore" Version="$(GrpcAspNetCoreVersion)" />
<PackageVersion Include="Grpc.AspNetCore.HealthChecks" Version="$(GrpcAspNetCoreVersion)" />
Expand All @@ -138,6 +138,6 @@
</PackageVersion>
<PackageVersion Include="Xunit.DependencyInjection" Version="9.3.0" />
<PackageVersion Include="Xunit.DependencyInjection.Logging" Version="9.0.0" />
<PackageVersion Include="Moq" Version="4.20.70" />
<PackageVersion Include="Moq" Version="4.20.72" />
</ItemGroup>
</Project>
49 changes: 25 additions & 24 deletions Source/Euonia.Business/Factory/BusinessObjectFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,14 @@ public BusinessObjectFactory(IServiceProvider provider, IObjectActivator activat
/// <inheritdoc/>
public async Task<TTarget> CreateAsync<TTarget>(params object[] criteria)
{
criteria ??= new object[] { null };
criteria ??= [null];
var method = ObjectReflector.FindFactoryMethod<TTarget, FactoryCreateAttribute>(criteria);
var target = GetObjectInstance<TTarget>();
if (target is IEditableObject editable)
{
editable.MarkAsInsert();
}

try
{
_activator?.InitializeInstance(target);
Expand All @@ -56,7 +57,7 @@ public async Task<TTarget> CreateAsync<TTarget>(params object[] criteria)
/// <inheritdoc/>
public async Task<TTarget> FetchAsync<TTarget>(params object[] criteria)
{
criteria ??= new object[] { null };
criteria ??= [null];
var method = ObjectReflector.FindFactoryMethod<TTarget, FactoryFetchAttribute>(criteria);
var target = GetObjectInstance<TTarget>();
try
Expand All @@ -74,7 +75,7 @@ public async Task<TTarget> FetchAsync<TTarget>(params object[] criteria)
/// <inheritdoc/>
public async Task<TTarget> InsertAsync<TTarget>(params object[] criteria)
{
criteria ??= new object[] { null };
criteria ??= [null];
var method = ObjectReflector.FindFactoryMethod<TTarget, FactoryInsertAttribute>(criteria);
var target = GetObjectInstance<TTarget>();
try
Expand All @@ -92,7 +93,7 @@ public async Task<TTarget> InsertAsync<TTarget>(params object[] criteria)
/// <inheritdoc/>
public async Task<TTarget> UpdateAsync<TTarget>(params object[] criteria)
{
criteria ??= new object[] { null };
criteria ??= [null];
var method = ObjectReflector.FindFactoryMethod<TTarget, FactoryUpdateAttribute>(criteria);
var target = GetObjectInstance<TTarget>();
try
Expand All @@ -114,18 +115,18 @@ public async Task<TTarget> SaveAsync<TTarget>(TTarget target, CancellationToken
{
IEditableObject editableObject => editableObject.State switch
{
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.Insert => ObjectReflector.FindFactoryMethod<TTarget, FactoryInsertAttribute>([cancellationToken]),
ObjectEditState.Update => ObjectReflector.FindFactoryMethod<TTarget, FactoryUpdateAttribute>([cancellationToken]),
ObjectEditState.Delete => ObjectReflector.FindFactoryMethod<TTarget, FactoryDeleteAttribute>([cancellationToken]),
ObjectEditState.None => throw new InvalidOperationException(),
_ => throw new ArgumentOutOfRangeException(nameof(target), Resources.IDS_INVALID_STATE)
},
ICommandObject => ObjectReflector.FindFactoryMethod<TTarget, FactoryExecuteAttribute>(new object[] { cancellationToken }),
ICommandObject => ObjectReflector.FindFactoryMethod<TTarget, FactoryExecuteAttribute>([cancellationToken]),
IReadOnlyObject => throw new InvalidOperationException("The operation can not apply for ReadOnlyObject."),
_ => ObjectReflector.FindFactoryMethod<TTarget, FactoryUpdateAttribute>(new object[] { cancellationToken })
_ => ObjectReflector.FindFactoryMethod<TTarget, FactoryUpdateAttribute>([cancellationToken])
};

await InvokeAsync(method, target, new object[] { cancellationToken });
await InvokeAsync(method, target, [cancellationToken]);

return target;
}
Expand All @@ -134,12 +135,12 @@ public async Task<TTarget> SaveAsync<TTarget>(TTarget target, CancellationToken
public async Task<TTarget> ExecuteAsync<TTarget>(TTarget target, CancellationToken cancellationToken = default)
where TTarget : ICommandObject
{
var method = ObjectReflector.FindFactoryMethod<TTarget, FactoryExecuteAttribute>(new object[] { cancellationToken });
var method = ObjectReflector.FindFactoryMethod<TTarget, FactoryExecuteAttribute>([cancellationToken]);

try
{
_activator?.InitializeInstance(target);
await InvokeAsync(method, target, new object[] { cancellationToken });
await InvokeAsync(method, target, [cancellationToken]);
return target;
}
finally
Expand All @@ -152,7 +153,7 @@ public async Task<TTarget> ExecuteAsync<TTarget>(TTarget target, CancellationTok
public async Task<TTarget> ExecuteAsync<TTarget>(params object[] criteria)
where TTarget : ICommandObject
{
criteria ??= new object[] { null };
criteria ??= [null];
var method = ObjectReflector.FindFactoryMethod<TTarget, FactoryExecuteAttribute>(criteria);
var target = GetObjectInstance<TTarget>();

Expand All @@ -171,7 +172,7 @@ public async Task<TTarget> ExecuteAsync<TTarget>(params object[] criteria)
/// <inheritdoc/>
public async Task DeleteAsync<TTarget>(params object[] criteria)
{
criteria ??= new object[] { null };
criteria ??= [null];
var method = ObjectReflector.FindFactoryMethod<TTarget, FactoryDeleteAttribute>(criteria);
var target = GetObjectInstance<TTarget>();

Expand Down Expand Up @@ -208,28 +209,28 @@ private static async Task InvokeAsync<TTarget>(MethodInfo method, TTarget target
private TTarget GetObjectInstance<TTarget>()
{
var @object = ActivatorUtilities.GetServiceOrCreateInstance<TTarget>(_provider);
if (@object is IUseBusinessContext ctx)
{
ctx.BusinessContext = _provider.GetRequiredService<BusinessContext>();
}

if (@object is IHasLazyServiceProvider lazy)
switch (@object)
{
lazy.LazyServiceProvider = _provider.GetRequiredService<ILazyServiceProvider>();
case IUseBusinessContext ctx:
ctx.BusinessContext = _provider.GetRequiredService<BusinessContext>();
break;
case IHasLazyServiceProvider lazy:
lazy.LazyServiceProvider = _provider.GetRequiredService<ILazyServiceProvider>();
break;
}

var properties = ObjectReflector.GetAutoInjectProperties(typeof(TTarget));

foreach (var (property, type, multiple) in properties)
foreach (var (property, type, multiple, serviceKey) in properties)
{
if (multiple)
{
var implement = _provider.GetServices(type);
var implement = serviceKey == null ? _provider.GetServices(type) : _provider.GetKeyedServices(type, serviceKey);
property.SetValue(@object, implement);
}
else
{
var implement = _provider.GetService(type);
var implement = serviceKey == null ? _provider.GetService(type) : _provider.GetKeyedService(type, serviceKey);
property.SetValue(@object, implement);
}
}
Expand Down
Loading

0 comments on commit 7971d1d

Please sign in to comment.