Skip to content

Commit fee39ff

Browse files
committed
style: 优化代码
1 parent a36651e commit fee39ff

3 files changed

Lines changed: 21 additions & 20 deletions

File tree

src/OSharp.Wpf/Stylet/IoC.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@ public static class IoC
2020
/// <summary>
2121
/// 获取指定服务类型的单个实例
2222
/// </summary>
23-
public static Func<Type, string, object> GetInstance = (service, key) => throw new InvalidOperationException("IoC is not initialized");
23+
public static Func<Type, string, object> GetInstance = (_, _) => throw new InvalidOperationException("IoC is not initialized");
2424

2525
/// <summary>
2626
/// 获取指定服务类型的多个实例
2727
/// </summary>
28-
public static Func<Type, IEnumerable<object>> GetAllInstances = service => throw new InvalidOperationException("IoC is not initialized");
28+
public static Func<Type, IEnumerable<object>> GetAllInstances = _ => throw new InvalidOperationException("IoC is not initialized");
2929

30-
public static Action<object> BuildUp = instance => throw new InvalidOperationException("IoC is not initialized");
30+
public static Action<object> BuildUp = _ => throw new InvalidOperationException("IoC is not initialized");
3131

3232
/// <summary>
3333
/// 获取指定服务类型的单个实例
@@ -62,7 +62,7 @@ public static void Initialize(IContainer container)
6262
/// </summary>
6363
public static void Initialize(IServiceProvider provider)
6464
{
65-
GetInstance = (type, key) => provider.GetService(type);
65+
GetInstance = (type, _) => provider.GetService(type);
6666
GetAllInstances = provider.GetServices;
6767
}
6868
}

src/OSharp.Wpf/Stylet/ServiceProviderBootstrapper.cs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ namespace OSharp.Wpf.Stylet;
1414

1515
public abstract class ServiceProviderBootstrapper<TRootViewModel> : BootstrapperBase where TRootViewModel : class
1616
{
17-
private IHostBuilder _hostBuilder;
17+
private HostApplicationBuilder _hostBuilder;
1818
private IHost _host;
1919
private readonly CancellationTokenSource _cancellationTokenSource = new();
2020
private TRootViewModel _rootViewModel;
21-
protected virtual TRootViewModel RootViewModel => this._rootViewModel ??= (TRootViewModel)this.GetInstance(typeof(TRootViewModel));
21+
protected virtual TRootViewModel RootViewModel => _rootViewModel ??= (TRootViewModel)GetInstance(typeof(TRootViewModel));
2222

2323
protected IServiceProvider ServiceProvider { get; private set; }
2424

@@ -27,20 +27,20 @@ public abstract class ServiceProviderBootstrapper<TRootViewModel> : Bootstrapper
2727
/// </summary>
2828
protected override void OnStart()
2929
{
30-
_hostBuilder = Host.CreateDefaultBuilder();
30+
_hostBuilder = Host.CreateApplicationBuilder();
31+
_hostBuilder.Environment.EnvironmentName = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT")
32+
?? Environment.GetEnvironmentVariable("DOTNET_ENVIRONMENT") ?? "Production";
3133
}
3234

3335
/// <summary>
3436
/// Overridden from BootstrapperBase, this sets up the IoC container
3537
/// </summary>
3638
protected override void ConfigureBootstrapper()
3739
{
38-
_hostBuilder.ConfigureServices((context, services) =>
39-
{
40-
services.AddSingleton(context);
41-
DefaultConfigureIoC(services);
42-
ConfigureIoC(services);
43-
});
40+
_hostBuilder.Services.AddSingleton<IHostApplicationBuilder>(_hostBuilder);
41+
DefaultConfigureIoC(_hostBuilder.Services);
42+
ConfigureIoC(_hostBuilder.Services);
43+
4444
_host = _hostBuilder.Build();
4545
ServiceProvider = _host.Services;
4646
_host.StartAsync(_cancellationTokenSource.Token).GetAwaiter().GetResult();
@@ -53,8 +53,8 @@ protected virtual void DefaultConfigureIoC(IServiceCollection services)
5353
{
5454
var viewManagerConfig = new ViewManagerConfig()
5555
{
56-
ViewFactory = this.GetInstance,
57-
ViewAssemblies = new List<Assembly>() { this.GetType().Assembly }
56+
ViewFactory = GetInstance,
57+
ViewAssemblies = [GetType().Assembly]
5858
};
5959

6060
services.AddSingleton<IViewManager>(new ViewManager(viewManagerConfig));
@@ -100,7 +100,7 @@ protected override void OnExit(ExitEventArgs e)
100100
catch (Exception ex)
101101
{
102102
// 记录日志但不阻止退出
103-
System.Diagnostics.Debug.WriteLine($"Error stopping host during exit: {ex.Message}");
103+
Debug.WriteLine($"Error stopping host during exit: {ex.Message}");
104104
}
105105
}
106106

src/OSharp/EventBuses/EventHandlerBase.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public virtual void Handle(IEventData eventData)
4343
/// <param name="eventData">事件源数据</param>
4444
/// <param name="cancelToken">异步取消标识</param>
4545
/// <returns></returns>
46-
public virtual Task HandleAsync(IEventData eventData, CancellationToken cancelToken = default(CancellationToken))
46+
public virtual Task HandleAsync(IEventData eventData, CancellationToken cancelToken = default)
4747
{
4848
if (!CanHandle(eventData))
4949
{
@@ -56,16 +56,17 @@ public virtual void Handle(IEventData eventData)
5656
/// 事件处理
5757
/// </summary>
5858
/// <param name="eventData">事件源数据</param>
59-
public abstract void Handle(TEventData eventData);
59+
public virtual void Handle(TEventData eventData)
60+
{ }
6061

6162
/// <summary>
6263
/// 异步事件处理
6364
/// </summary>
6465
/// <param name="eventData">事件源数据</param>
6566
/// <param name="cancelToken">异步取消标识</param>
6667
/// <returns>是否成功</returns>
67-
public virtual Task HandleAsync(TEventData eventData, CancellationToken cancelToken = default(CancellationToken))
68+
public virtual Task HandleAsync(TEventData eventData, CancellationToken cancelToken = default)
6869
{
6970
return Task.Run(() => Handle(eventData), cancelToken);
7071
}
71-
}
72+
}

0 commit comments

Comments
 (0)