Skip to content

Commit 8ea3fa8

Browse files
committedJul 24, 2019
update to netcoreapp3.0
·
3.0.01.0.7
1 parent 24d6669 commit 8ea3fa8

File tree

12 files changed

+115
-58
lines changed

12 files changed

+115
-58
lines changed
 

‎src/DbTool.Core/DbProviderFactory.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ public class DbProviderFactory
88
{
99
private readonly IReadOnlyCollection<IDbProvider> _dbProviders;
1010

11-
public DbProviderFactory(IReadOnlyCollection<IDbProvider> dbProviders) => _dbProviders = dbProviders;
11+
public DbProviderFactory(IEnumerable<IDbProvider> dbProviders) => _dbProviders = dbProviders.ToArray();
1212

1313
public IDbProvider GetDbProvider(string dbType) => _dbProviders.FirstOrDefault(p => p.DbType.EqualsIgnoreCase(dbType));
1414

‎src/DbTool.MySql/DbTool.DbProvider.MySql.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFrameworks>netstandard2.0;net462</TargetFrameworks>
4+
<TargetFramework>netstandard2.0</TargetFramework>
55
</PropertyGroup>
66

77
<ItemGroup>

‎src/DbTool.SqlServer/DbTool.DbProvider.SqlServer.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFrameworks>netstandard2.0;net462</TargetFrameworks>
4+
<TargetFramework>netstandard2.0</TargetFramework>
55
</PropertyGroup>
66

77
<ItemGroup>

‎src/DbTool/ConfigurationHelper.cs

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
using System;
2+
using System.Collections.Specialized;
3+
using System.Configuration;
4+
using WeihanLi.Extensions;
5+
6+
namespace DbTool
7+
{
8+
/// <summary>
9+
/// Helper for ConfigurationManager
10+
/// https://github.com/WeihanLi/WeihanLi.Common/blob/dev/src/WeihanLi.Common/Helpers/ConfigurationHelper.cs
11+
/// </summary>
12+
public static class ConfigurationHelper
13+
{
14+
private static NameValueCollection _appSettings;
15+
16+
static ConfigurationHelper()
17+
{
18+
_appSettings = ConfigurationManager.AppSettings;
19+
}
20+
21+
/// <summary>
22+
/// 获取配置文件中AppSetting节点的值
23+
/// </summary>
24+
/// <param name="key">设置的键值</param>
25+
/// <returns>键值对应的值</returns>
26+
public static string AppSetting(string key) => _appSettings[key];
27+
28+
/// <summary>
29+
/// 获取配置文件中AppSetting节点的值
30+
/// </summary>
31+
/// <param name="key">设置的键值</param>
32+
/// <returns>键值对应的值</returns>
33+
public static T AppSetting<T>(string key) => _appSettings[key].StringToType<T>();
34+
35+
public static T AppSetting<T>(string key, T defaultValue) => _appSettings[key].StringToType(defaultValue);
36+
37+
public static T AppSetting<T>(string key, Func<T> defaultValueFactory) => _appSettings[key].StringToType(defaultValueFactory());
38+
39+
public static bool AddAppSetting<T>(string key, T value) => AddAppSetting(key, value.ToJsonOrString());
40+
41+
public static bool AddAppSetting(string key, string value)
42+
{
43+
var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
44+
config.AppSettings.Settings.Add(key, value);
45+
config.Save(ConfigurationSaveMode.Minimal);
46+
// Force a reload of a changed section.
47+
ConfigurationManager.RefreshSection("appSettings");
48+
_appSettings = ConfigurationManager.AppSettings;
49+
return true;
50+
}
51+
52+
public static bool UpdateAppSetting<T>(string key, T value) => UpdateAppSetting(key, value.ToJsonOrString());
53+
54+
public static bool UpdateAppSetting(string key, string value)
55+
{
56+
var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
57+
config.AppSettings.Settings.Remove(key);
58+
config.AppSettings.Settings.Add(key, value);
59+
config.Save(ConfigurationSaveMode.Minimal);
60+
// Force a reload of a changed section.
61+
ConfigurationManager.RefreshSection("appSettings");
62+
_appSettings = ConfigurationManager.AppSettings;
63+
return true;
64+
}
65+
66+
/// <summary>
67+
/// 获取配置文件中ConnectionStrings节点的值
68+
/// </summary>
69+
/// <param name="key">键值</param>
70+
/// <returns>键值对应的连接字符串值</returns>
71+
public static string ConnectionString(string key) => ConfigurationManager.ConnectionStrings[key].ConnectionString;
72+
}
73+
}

‎src/DbTool/DbTool.csproj

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
22
<PropertyGroup>
3-
<TargetFramework>net462</TargetFramework>
3+
<TargetFramework>netcoreapp3.0</TargetFramework>
44
<OutputType>WinExe</OutputType>
55
<RootNamespace>DbTool</RootNamespace>
66
<AssemblyName>DbTool</AssemblyName>
77
<Version>1.0.6</Version>
8+
<UseWindowsForms>true</UseWindowsForms>
9+
<!-- <UseWPF>true</UseWPF> -->
810
</PropertyGroup>
911
<ItemGroup>
10-
<Reference Include="System.Windows.Forms" />
11-
<PackageReference Include="Autofac" Version="4.8.1" />
12+
<PackageReference Include="Autofac" Version="4.9.3" />
1213
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="3.1.0" />
1314
<PackageReference Include="WeihanLi.Npoi" Version="1.3.5" />
1415
</ItemGroup>

‎src/DbTool/Program.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ public static void Init()
3131
builder.RegisterType<SqlServerDbProvider>().As<IDbProvider>();
3232
builder.RegisterType<DbProviderFactory>().SingleInstance();
3333
builder.RegisterType<DefaultModelCodeGenerator>().As<IModelCodeGenerator>();
34+
//
35+
//IServiceCollection services = new ServiceCollection();
36+
//services.AddSingleton<IModelCodeGenerator, DefaultModelCodeGenerator>();
37+
//services.AddSingleton<IDbProvider, SqlServerDbProvider>();
38+
//services.AddSingleton<DbProviderFactory>();
3439

3540
var pluginDir = ApplicationHelper.MapPath("plugins");
3641
if (Directory.Exists(pluginDir))

‎src/DbTool/Utils.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
using Microsoft.CodeAnalysis;
1212
using Microsoft.CodeAnalysis.CSharp;
1313
using WeihanLi.Common;
14-
using WeihanLi.Common.Helpers;
1514
using WeihanLi.Extensions;
1615

1716
namespace DbTool
@@ -65,7 +64,6 @@ public static List<TableEntity> GeTableEntityFromSourceCode(params string[] sour
6564
.WithOptions(new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary))
6665
.AddReferences(systemReference, annotationReference, weihanliCommonReference)
6766
.AddSyntaxTrees(syntaxTree);
68-
var assemblyPath = ApplicationHelper.MapPath($"{assemblyName}.dll");
6967
using (var ms = new MemoryStream())
7068
{
7169
var compilationResult = compilation.Emit(ms);

‎test/DbTool.Test/App.config

Lines changed: 0 additions & 17 deletions
This file was deleted.

‎test/DbTool.Test/BaseDbTest.cs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
using System.Collections.Generic;
2-
using Autofac;
32
using DbTool.Core;
43
using DbTool.Core.Entity;
54
using DbTool.MySql;
65
using DbTool.SqlServer;
6+
using Microsoft.Extensions.Configuration;
7+
using Microsoft.Extensions.DependencyInjection;
78
using WeihanLi.Common;
8-
using WeihanLi.Common.Helpers;
99
using Xunit;
1010

1111
namespace DbTool.Test
@@ -22,12 +22,17 @@ static BaseDbTest()
2222

2323
private static void Init()
2424
{
25-
var builder = new ContainerBuilder();
26-
builder.RegisterType<SqlServerDbProvider>().As<IDbProvider>();
27-
builder.RegisterType<MySqlDbProvider>().As<IDbProvider>();
28-
builder.RegisterType<DbProviderFactory>().SingleInstance();
29-
var container = builder.Build();
30-
DependencyResolver.SetDependencyResolver(t => container.Resolve(t));
25+
IServiceCollection services = new ServiceCollection();
26+
services.AddSingleton<IDbProvider, SqlServerDbProvider>();
27+
services.AddSingleton<IDbProvider, MySqlDbProvider>();
28+
services.AddSingleton<DbProviderFactory>();
29+
30+
IConfiguration configuration = new ConfigurationBuilder()
31+
.AddJsonFile("appsettings.json")
32+
.Build();
33+
services.AddSingleton(configuration);
34+
35+
DependencyResolver.SetDependencyResolver(services);
3136
}
3237

3338
protected BaseDbTest()
@@ -92,7 +97,8 @@ protected BaseDbTest()
9297

9398
public virtual void QueryTest()
9499
{
95-
var dbHelper = new DbHelper(ConfigurationHelper.ConnectionString(ConnStringKey), _dbType);
100+
var connString = DependencyResolver.Current.GetService<IConfiguration>().GetConnectionString(ConnStringKey);
101+
var dbHelper = new DbHelper(connString, _dbType);
96102
Assert.NotNull(dbHelper.DatabaseName);
97103
var tables = dbHelper.GetTablesInfo();
98104
Assert.NotNull(tables);

‎test/DbTool.Test/DbTool.Test.csproj

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22
<PropertyGroup>
33
<IsPackable>false</IsPackable>
44
<LangVersion>latest</LangVersion>
5-
<TargetFrameworks>net462</TargetFrameworks>
5+
<TargetFramework>netcoreapp2.1</TargetFramework>
66
</PropertyGroup>
77

88
<ItemGroup>
9+
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.1.1" />
910
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.5.0" />
1011
<PackageReference Include="xunit" Version="2.3.1" />
1112
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" />
@@ -14,6 +15,11 @@
1415
<ItemGroup>
1516
<ProjectReference Include="..\..\src\DbTool.MySql\DbTool.DbProvider.MySql.csproj" />
1617
<ProjectReference Include="..\..\src\DbTool.SqlServer\DbTool.DbProvider.SqlServer.csproj" />
17-
<ProjectReference Include="..\..\src\DbTool\DbTool.csproj" />
18+
</ItemGroup>
19+
20+
<ItemGroup>
21+
<None Update="appsettings.json">
22+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
23+
</None>
1824
</ItemGroup>
1925
</Project>

‎test/DbTool.Test/PostgreSqlTest.cs

Lines changed: 0 additions & 21 deletions
This file was deleted.

‎test/DbTool.Test/appsettings.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"ConnectionStrings": {
3+
"MySqlConn": "server=localhost;database=testdb;uid=liweihan;pwd=Admin888",
4+
"SqlServerConn": "server=.;database=test;uid=liweihan;pwd=Admin888"
5+
}
6+
}

0 commit comments

Comments
 (0)
Please sign in to comment.