Skip to content

Commit

Permalink
Merge branch 'dev' of https://github.com/WeihanLi/WeihanLi.Common int…
Browse files Browse the repository at this point in the history
…o dev
  • Loading branch information
WeihanLi committed Oct 31, 2023
2 parents 92d52a8 + 0f0c71c commit eb0c000
Show file tree
Hide file tree
Showing 33 changed files with 135 additions and 202 deletions.
2 changes: 1 addition & 1 deletion .husky/pre-commit
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@
# dotnet husky run --name "format-verify"

# run tasks by task group name
dotnet husky run --group pre-commit
# dotnet husky run --group pre-commit
7 changes: 6 additions & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@
<RepositoryType>git</RepositoryType>
<Authors>WeihanLi</Authors>
<Copyright>Copyright 2017-$([System.DateTime]::Now.Year) (c) WeihanLi</Copyright>
<NoWarn>$(NoWarn);NU5048;CS1591</NoWarn>
<NoWarn>$(NoWarn);NU5048;CS1591;NETSDK1057</NoWarn>
<ArtifactsPath>$(MSBuildThisFileDirectory)artifacts</ArtifactsPath>
<ContinuousIntegrationBuild Condition="'$(TF_BUILD)' == 'true' or '$(GITHUB_ACTIONS)' == 'true'">true</ContinuousIntegrationBuild>
</PropertyGroup>

<ItemGroup>
<InternalsVisibleTo Condition="'$(Configuration)'=='Debug'" Include="WeihanLi.Common.Test" />
</ItemGroup>
</Project>
1 change: 1 addition & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<ItemGroup>
<PackageVersion Include="Microsoft.CSharp" Version="4.7.0" />
<PackageVersion Include="System.Reflection.Emit" Version="4.7.0" />
<PackageVersion Include="Microsoft.Bcl.AsyncInterfaces" Version="6.0.0" />
<PackageVersion Include="System.ComponentModel.Annotations" Version="5.0.0" />
<PackageVersion Include="Newtonsoft.Json" Version="13.0.3" />
<PackageVersion Include="AspectCore.Core" Version="2.4.0" />
Expand Down
7 changes: 4 additions & 3 deletions build.cake
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ Task("build")
.IsDependentOn("restore")
.Does(() =>
{
var buildSetting = new DotNetBuildSettings{
var buildSetting = new DotNetBuildSettings
{
NoRestore = true,
Configuration = configuration
};
Expand All @@ -87,7 +88,7 @@ Task("test")
var testSettings = new DotNetTestSettings
{
NoRestore = false,
Configuration = configuration
Configuration = "Debug"
};
foreach(var project in testProjects)
{
Expand All @@ -102,7 +103,7 @@ Task("pack")
{
var settings = new DotNetPackSettings
{
Configuration = configuration,
Configuration = "Release",
OutputDirectory = artifacts,
VersionSuffix = "",
NoRestore = true,
Expand Down
2 changes: 1 addition & 1 deletion build/sign.props
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<Project>
<PropertyGroup Condition="'$(OS)' == 'Windows_NT'">
<PropertyGroup Condition="'$(OS)' == 'Windows_NT' and '$(Configuration)' == 'Release'">
<SignAssembly>True</SignAssembly>
<AssemblyOriginatorKeyFile>$(MSBuildThisFileDirectory)weihanli.snk</AssemblyOriginatorKeyFile>
<DelaySign>False</DelaySign>
Expand Down
4 changes: 2 additions & 2 deletions samples/DotNetCoreSample/AppHostTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public static async Task MainTest()
{
protected override TimeSpan Period => TimeSpan.FromSeconds(1);

protected override Task TimedTask(CancellationToken cancellationToken)
protected override Task ExecuteTaskAsync(CancellationToken cancellationToken)
{
Console.WriteLine(DateTimeOffset.Now);
return Task.CompletedTask;
Expand All @@ -53,7 +53,7 @@ public DiagnosticBackgroundService(IServiceProvider serviceProvider) : base(serv

protected override string CronExpression => CronHelper.Secondly;

protected override Task TimedTask(IServiceProvider serviceProvider, CancellationToken cancellationToken)
protected override Task ExecuteTaskInternalAsync(IServiceProvider serviceProvider, CancellationToken cancellationToken)
{
Console.WriteLine(DateTimeOffset.Now);
return Task.CompletedTask;
Expand Down
9 changes: 7 additions & 2 deletions src/Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
<Project>
<Import Project="$([MSBuild]::GetPathOfFileAbove('Directory.Build.props', '$(MSBuildThisFileDirectory)../'))" />
<Import Project="../build/sign.props" />
<PropertyGroup>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<PropertyGroup>
<!-- code analysis https://github.com/dotnet/docs/blob/main/docs/core/project-sdk/msbuild-props.md#code-analysis-properties -->
<!-- <EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
<AnalysisLevel>latest</AnalysisLevel>
<AnalysisMode>Recommended</AnalysisMode>
<AnalysisModeSecurity>All</AnalysisModeSecurity>
<GenerateDocumentationFile>true</GenerateDocumentationFile> -->
<RepositoryUrl>https://github.com/WeihanLi/WeihanLi.Common</RepositoryUrl>
<!-- Optional: Publish the repository URL in the built .nupkg (in the NuSpec <Repository> element) -->
<PublishRepositoryUrl>true</PublishRepositoryUrl>
Expand Down
15 changes: 5 additions & 10 deletions src/WeihanLi.Common.Aspect.AspectCore/AspectCoreProxyFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ public AspectCoreProxyFactory(IServiceProvider serviceProvider)

public object CreateProxy(Type serviceType, object?[] arguments)
{
if (null == serviceType)
throw new ArgumentNullException(nameof(serviceType));
Guard.NotNull(serviceType);

if (serviceType.IsInterface)
return AspectCoreHelper.ProxyGenerator.CreateInterfaceProxy(serviceType);
Expand All @@ -27,10 +26,8 @@ public object CreateProxy(Type serviceType, object?[] arguments)

public object CreateProxy(Type serviceType, Type implementType, params object?[] arguments)
{
if (null == serviceType)
throw new ArgumentNullException(nameof(serviceType));
if (null == implementType)
throw new ArgumentNullException(nameof(implementType));
Guard.NotNull(serviceType);
Guard.NotNull(implementType);

if (serviceType.IsInterface)
{
Expand All @@ -43,10 +40,8 @@ public object CreateProxy(Type serviceType, Type implementType, params object?[]

public object CreateProxyWithTarget(Type serviceType, object implement, object?[] arguments)
{
if (null == serviceType)
throw new ArgumentNullException(nameof(serviceType));
if (null == implement)
throw new ArgumentNullException(nameof(implement));
Guard.NotNull(serviceType);
Guard.NotNull(implement);

if (serviceType.IsInterface)
return AspectCoreHelper.ProxyGenerator.CreateInterfaceProxy(serviceType, implement);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ internal sealed class AspectCoreProxyTypeFactory : IProxyTypeFactory
{
public Type CreateProxyType(Type serviceType)
{
if (null == serviceType)
{
throw new ArgumentNullException(nameof(serviceType));
}
Guard.NotNull(serviceType);

if (serviceType.IsInterface)
{
Expand All @@ -19,14 +16,9 @@ public Type CreateProxyType(Type serviceType)

public Type CreateProxyType(Type serviceType, Type implementType)
{
if (null == serviceType)
{
throw new ArgumentNullException(nameof(serviceType));
}
if (null == implementType)
{
throw new ArgumentNullException(nameof(implementType));
}
Guard.NotNull(serviceType);
Guard.NotNull(implementType);

if (serviceType.IsInterface)
{
return AspectCoreHelper.ProxyGenerator.TypeGenerator.CreateClassProxyType(serviceType, implementType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@ public static class FluentAspectBuilderExtensions
{
public static IFluentAspectsBuilder UseAspectCoreProxy(this IFluentAspectsBuilder builder)
{
if (builder is null)
{
throw new ArgumentNullException(nameof(builder));
}
Guard.NotNull(builder);

builder.Services.AddTransient<IProxyFactory, AspectCoreProxyFactory>();
builder.Services.AddTransient<IProxyTypeFactory, AspectCoreProxyTypeFactory>();
Expand All @@ -21,10 +18,7 @@ public static IFluentAspectsBuilder UseAspectCoreProxy(this IFluentAspectsBuilde

public static IFluentAspectsServiceContainerBuilder UseAspectCoreProxy(this IFluentAspectsServiceContainerBuilder builder)
{
if (builder is null)
{
throw new ArgumentNullException(nameof(builder));
}
Guard.NotNull(builder);

builder.Services.AddTransient<IProxyFactory, AspectCoreProxyFactory>();
builder.Services.AddTransient<IProxyTypeFactory, AspectCoreProxyTypeFactory>();
Expand Down
17 changes: 5 additions & 12 deletions src/WeihanLi.Common.Aspect.Castle/CastleProxyFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ public CastleProxyFactory(IServiceProvider serviceProvider)

public object CreateProxy(Type serviceType, object?[] arguments)
{
if (null == serviceType)
throw new ArgumentNullException(nameof(serviceType));
Guard.NotNull(serviceType);

if (serviceType.IsInterface)
{
Expand All @@ -32,11 +31,8 @@ public object CreateProxy(Type serviceType, object?[] arguments)

public object CreateProxy(Type serviceType, Type implementType, params object?[] arguments)
{
if (null == serviceType)
throw new ArgumentNullException(nameof(serviceType));

if (null == implementType)
throw new ArgumentNullException(nameof(implementType));
Guard.NotNull(serviceType);
Guard.NotNull(implementType);

if (serviceType.IsInterface)
{
Expand All @@ -49,11 +45,8 @@ public object CreateProxy(Type serviceType, Type implementType, params object?[]

public object CreateProxyWithTarget(Type serviceType, object implement, object?[] arguments)
{
if (null == serviceType)
throw new ArgumentNullException(nameof(serviceType));

if (null == implement)
throw new ArgumentNullException(nameof(implement));
Guard.NotNull(serviceType);
Guard.NotNull(implement);

if (serviceType.IsInterface)
{
Expand Down
15 changes: 3 additions & 12 deletions src/WeihanLi.Common.Aspect.Castle/CastleProxyTypeFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ internal sealed class CastleProxyTypeFactory : IProxyTypeFactory
{
public Type CreateProxyType(Type serviceType)
{
if (null == serviceType)
{
throw new ArgumentNullException(nameof(serviceType));
}
Guard.NotNull(serviceType);

if (serviceType.IsInterface)
{
Expand All @@ -21,14 +18,8 @@ public Type CreateProxyType(Type serviceType)

public Type CreateProxyType(Type serviceType, Type implementType)
{
if (null == serviceType)
{
throw new ArgumentNullException(nameof(serviceType));
}
if (null == implementType)
{
throw new ArgumentNullException(nameof(implementType));
}
Guard.NotNull(serviceType);
Guard.NotNull(implementType);

if (serviceType.IsInterface)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@ public static class FluentAspectBuilderExtensions
{
public static IFluentAspectsBuilder UseCastleProxy(this IFluentAspectsBuilder builder)
{
if (builder is null)
{
throw new ArgumentNullException(nameof(builder));
}
Guard.NotNull(builder);

builder.Services.AddTransient<IProxyFactory, CastleProxyFactory>();
builder.Services.AddTransient<IProxyTypeFactory, CastleProxyTypeFactory>();
Expand All @@ -22,10 +19,7 @@ public static IFluentAspectsBuilder UseCastleProxy(this IFluentAspectsBuilder bu

public static IFluentAspectsServiceContainerBuilder UseCastleProxy(this IFluentAspectsServiceContainerBuilder builder)
{
if (builder is null)
{
throw new ArgumentNullException(nameof(builder));
}
Guard.NotNull(builder);

builder.Services.AddTransient<IProxyFactory, CastleProxyFactory>();
builder.Services.AddTransient<IProxyTypeFactory, CastleProxyTypeFactory>();
Expand Down
2 changes: 1 addition & 1 deletion src/WeihanLi.Common.Logging.Serilog/SerilogLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public SerilogLogger(
ILogger? logger = null,
string? name = null)
{
_provider = provider ?? throw new ArgumentNullException(nameof(provider));
_provider = WeihanLi.Common.Guard.NotNull(provider);

// If a logger was passed, the provider has already added itself as an enricher
_logger = logger ?? Serilog.Log.Logger.ForContext(new[] { provider });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public static ILoggerFactory AddSerilog(
ILogger? logger = null,
bool dispose = false)
{
if (factory == null) throw new ArgumentNullException(nameof(factory));
Guard.NotNull(factory);

factory.AddProvider(new SerilogLoggerProvider(logger, dispose));

Expand All @@ -40,8 +40,7 @@ public static ILoggerFactory AddSerilog(
/// <returns>The logging builder.</returns>
public static ILoggingBuilder AddSerilog(this ILoggingBuilder builder, ILogger? logger = null, bool dispose = false)
{
if (builder == null)
throw new ArgumentNullException(nameof(builder));
Guard.NotNull(builder);

if (dispose)
{
Expand Down
5 changes: 1 addition & 4 deletions src/WeihanLi.Common.Logging.Serilog/SerilogLoggerProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,7 @@ public FrameworkLogger CreateLogger(string categoryName)

public IDisposable BeginScope<T>(T state)
{
if (state == null)
{
throw new ArgumentNullException(nameof(state));
}
WeihanLi.Common.Guard.NotNull(state);

if (CurrentScope != null)
return new SerilogLoggerScope(this, state);
Expand Down
5 changes: 0 additions & 5 deletions src/WeihanLi.Common/Aspect/AspectDelegate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ public static void InvokeInternal(IInvocation invocation, IReadOnlyList<IInterce
invocation.ReturnValue = Task.FromResult(resultType.GetDefaultValue());
}

#if ValueTaskSupport
if (invocation.ProxyMethod.ReturnType == typeof(ValueTask))
{
invocation.ReturnValue = default(ValueTask);
Expand All @@ -80,7 +79,6 @@ public static void InvokeInternal(IInvocation invocation, IReadOnlyList<IInterce
var resultType = invocation.ProxyMethod.ReturnType.GetGenericArguments()[0];
invocation.ReturnValue = new ValueTask(Task.FromResult(resultType.GetDefaultValue()));
}
#endif
}
}

Expand Down Expand Up @@ -116,13 +114,10 @@ private static Func<IInvocation, Task> GetAspectDelegate(IInvocation invocation,
{
return task;
}
#if ValueTaskSupport
if (invocation.ReturnValue is ValueTask valTask)
{
return valTask.AsTask();
}
#endif
return Task.CompletedTask;
};
Expand Down
25 changes: 11 additions & 14 deletions src/WeihanLi.Common/Extensions/DumpExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,15 @@ public static Task DumpAsync<T>(this T t, Func<string, Task> dumpAction, Func<T,
.Invoke(Guard.NotNull(dumpValueFactory, nameof(dumpValueFactory)).Invoke(t));
}

#if ValueTaskSupport

public static ValueTask DumpAsync<T>(this T t, Func<string, ValueTask> dumpAction)
{
return Guard.NotNull(dumpAction, nameof(dumpAction))
.Invoke(t is null ? NullValue : t.ToJsonOrString());
}

public static ValueTask DumpAsync<T>(this T t, Func<string, ValueTask> dumpAction, Func<T, string> dumpValueFactory)
{
return Guard.NotNull(dumpAction, nameof(dumpAction))
.Invoke(Guard.NotNull(dumpValueFactory, nameof(dumpValueFactory)).Invoke(t));
}
#endif
public static ValueTask DumpAsync<T>(this T t, Func<string, ValueTask> dumpAction)
{
return Guard.NotNull(dumpAction, nameof(dumpAction))
.Invoke(t is null ? NullValue : t.ToJsonOrString());
}

public static ValueTask DumpAsync<T>(this T t, Func<string, ValueTask> dumpAction, Func<T, string> dumpValueFactory)
{
return Guard.NotNull(dumpAction, nameof(dumpAction))
.Invoke(Guard.NotNull(dumpValueFactory, nameof(dumpValueFactory)).Invoke(t));
}
}
Loading

0 comments on commit eb0c000

Please sign in to comment.