Skip to content

Commit

Permalink
Using file-scoped namespace and add license header
Browse files Browse the repository at this point in the history
  • Loading branch information
WeihanLi committed Apr 22, 2022
1 parent 4293998 commit d6cdb7e
Show file tree
Hide file tree
Showing 15 changed files with 933 additions and 901 deletions.
170 changes: 86 additions & 84 deletions src/DbTool/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,118 +1,120 @@
using System;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Runtime.Loader;
using System.Threading;
using System.Windows;
// Copyright (c) Weihan Li. All rights reserved.
// Licensed under the MIT license.

using DbTool.Core;
using DbTool.DbProvider.MySql;
using DbTool.DbProvider.PostgreSql;
using DbTool.DbProvider.SqlServer;
using DbTool.ViewModels;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using System;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Runtime.Loader;
using System.Threading;
using System.Windows;
using WeihanLi.Common;
using WeihanLi.Common.Helpers;
using WeihanLi.Extensions.Localization.Json;
using WeihanLi.Npoi;

namespace DbTool
namespace DbTool;

/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App
protected override void OnStartup(StartupEventArgs e)
{
protected override void OnStartup(StartupEventArgs e)
{
Init();
base.OnStartup(e);
}
Init();
base.OnStartup(e);
}

private static void ConfigureServices(IServiceCollection services)
{
services.TryAddTransient<MainWindow>();
services.AddJsonLocalization(options => options.ResourcesPathType = ResourcesPathType.CultureBased);

services.TryAddSingleton<IModelNameConverter, ModelNameConverter>();
services.TryAddSingleton<IModelCodeGenerator, DefaultCSharpModelCodeGenerator>();
services.TryAddSingleton<IModelCodeExtractor, DefaultCSharpModelCodeExtractor>();
services.TryAddSingleton<IDbHelperFactory, DbHelperFactory>();
services.TryAddSingleton<DbProviderFactory>();

services
.AddDbProvider<SqlServerDbProvider>()
.AddDbProvider<MySqlDbProvider>()
.AddDbProvider<PostgreSqlDbProvider>()
;

services.AddDbDocExporter<ExcelDbDocExporter>()
.AddDbDocExporter<CsvDbDocExporter>()
;
services.AddDbDocImporter<ExcelDbDocImporter>()
.AddDbDocImporter<CsvDbDocImporter>()
;
}
private static void ConfigureServices(IServiceCollection services)
{
services.TryAddTransient<MainWindow>();
services.AddJsonLocalization(options => options.ResourcesPathType = ResourcesPathType.CultureBased);

services.TryAddSingleton<IModelNameConverter, ModelNameConverter>();
services.TryAddSingleton<IModelCodeGenerator, DefaultCSharpModelCodeGenerator>();
services.TryAddSingleton<IModelCodeExtractor, DefaultCSharpModelCodeExtractor>();
services.TryAddSingleton<IDbHelperFactory, DbHelperFactory>();
services.TryAddSingleton<DbProviderFactory>();

services
.AddDbProvider<SqlServerDbProvider>()
.AddDbProvider<MySqlDbProvider>()
.AddDbProvider<PostgreSqlDbProvider>()
;

services.AddDbDocExporter<ExcelDbDocExporter>()
.AddDbDocExporter<CsvDbDocExporter>()
;
services.AddDbDocImporter<ExcelDbDocImporter>()
.AddDbDocImporter<CsvDbDocImporter>()
;
}

private static void Init()
{
#region Init Settings
private static void Init()
{
#region Init Settings

FluentSettings.LoadMappingProfiles(typeof(ColumnEntityMappingProfile).Assembly);
FluentSettings.LoadMappingProfiles(typeof(ColumnEntityMappingProfile).Assembly);

var settings = new SettingsViewModel();
settings.ConnectionString = settings.DefaultConnectionString;
// set current culture
Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo(settings.DefaultCulture);
Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(settings.DefaultCulture);
var settings = new SettingsViewModel();
settings.ConnectionString = settings.DefaultConnectionString;
// set current culture
Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo(settings.DefaultCulture);
Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(settings.DefaultCulture);

#endregion Init Settings
#endregion Init Settings

#region Init Services and plugins
#region Init Services and plugins

IServiceCollection services = new ServiceCollection();
services.AddSingleton(settings);
ConfigureServices(services);
IServiceCollection services = new ServiceCollection();
services.AddSingleton(settings);
ConfigureServices(services);

// load plugins
var interfaces = typeof(IDbProvider).Assembly
.GetExportedTypes()
.Where(x => x.IsInterface)
.ToArray();
var pluginDir = ApplicationHelper.MapPath("plugins");
if (Directory.Exists(pluginDir))
{
// load plugins
var interfaces = typeof(IDbProvider).Assembly
.GetExportedTypes()
.Where(x => x.IsInterface)
var plugins = Directory.GetFiles(pluginDir)
.Where(_ => _.EndsWith(".dll", StringComparison.OrdinalIgnoreCase))
.ToArray();
var pluginDir = ApplicationHelper.MapPath("plugins");
if (Directory.Exists(pluginDir))
if (plugins.Length > 0)
{
// load plugins
var plugins = Directory.GetFiles(pluginDir)
.Where(_ => _.EndsWith(".dll", StringComparison.OrdinalIgnoreCase))
var assemblies = plugins.Select(AssemblyLoadContext.Default.LoadFromAssemblyPath).ToArray();
var exportedTypes = assemblies
.Select(x => x.GetExportedTypes())
.SelectMany(t => t)
.Where(t => !t.IsInterface && !t.IsAbstract)
.ToArray();
var pluginTypes = exportedTypes
.Where(t => interfaces.Any(i => i.IsAssignableFrom(t)))
.ToArray();
if (plugins.Length > 0)
foreach (var type in pluginTypes)
{
var assemblies = plugins.Select(AssemblyLoadContext.Default.LoadFromAssemblyPath).ToArray();
var exportedTypes = assemblies
.Select(x => x.GetExportedTypes())
.SelectMany(t => t)
.Where(t => !t.IsInterface && !t.IsAbstract)
.ToArray();
var pluginTypes = exportedTypes
.Where(t => interfaces.Any(i => i.IsAssignableFrom(t)))
.ToArray();
foreach (var type in pluginTypes)
{
services.RegisterTypeAsImplementedInterfaces(type);
}

// load service modules
services.RegisterAssemblyModules(assemblies);
services.RegisterTypeAsImplementedInterfaces(type);
}

// load service modules
services.RegisterAssemblyModules(assemblies);
}
}

DependencyResolver.SetDependencyResolver(services);
DependencyResolver.SetDependencyResolver(services);

#endregion Init Services and plugins
#endregion Init Services and plugins

DependencyResolver.ResolveRequiredService<MainWindow>().Show();
}
DependencyResolver.ResolveRequiredService<MainWindow>().Show();
}
}
3 changes: 3 additions & 0 deletions src/DbTool/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Copyright (c) Weihan Li. All rights reserved.
// Licensed under the MIT license.

using System.Windows;

[assembly: ThemeInfo(
Expand Down
58 changes: 30 additions & 28 deletions src/DbTool/ConfigurationConstants.cs
Original file line number Diff line number Diff line change
@@ -1,35 +1,37 @@
namespace DbTool
// Copyright (c) Weihan Li. All rights reserved.
// Licensed under the MIT license.

namespace DbTool;

internal static class ConfigurationConstants
{
internal static class ConfigurationConstants
{
/// <summary>
/// Excel 模板下载地址
/// </summary>
public const string ExcelTemplateDownloadLink = "ExcelTemplateDownloadLink";
/// <summary>
/// Excel 模板下载地址
/// </summary>
public const string ExcelTemplateDownloadLink = "ExcelTemplateDownloadLink";

/// <summary>
/// 默认连接字符串
/// </summary>
public const string DefaultConnectionString = "DefaultConnString";
/// <summary>
/// 默认连接字符串
/// </summary>
public const string DefaultConnectionString = "DefaultConnString";

/// <summary>
/// 数据库类型
/// </summary>
public const string DbType = "DbType";
/// <summary>
/// 数据库类型
/// </summary>
public const string DbType = "DbType";

/// <summary>
/// 生成数据库描述类型
/// </summary>
public const string GenerateDbDescription = "GenerateDbDescription";
/// <summary>
/// 生成数据库描述类型
/// </summary>
public const string GenerateDbDescription = "GenerateDbDescription";

/// <summary>
/// 生成私有字段
/// </summary>
public const string GeneratePrivateField = "GeneratePrivateField";
/// <summary>
/// 生成私有字段
/// </summary>
public const string GeneratePrivateField = "GeneratePrivateField";

/// <summary>
/// 生成 [Description]/[Table] 等注解
/// </summary>
public const string GenerateDataAnnotation = "GenerateDataAnnotation";
}
/// <summary>
/// 生成 [Description]/[Table] 等注解
/// </summary>
public const string GenerateDataAnnotation = "GenerateDataAnnotation";
}
100 changes: 51 additions & 49 deletions src/DbTool/ConfigurationHelper.cs
Original file line number Diff line number Diff line change
@@ -1,58 +1,60 @@
using System.Collections.Specialized;
// Copyright (c) Weihan Li. All rights reserved.
// Licensed under the MIT license.

using System.Collections.Specialized;
using System.Configuration;
using WeihanLi.Extensions;

namespace DbTool
namespace DbTool;

/// <summary>
/// Helper for ConfigurationManager
/// https://github.com/WeihanLi/WeihanLi.Common/blob/dev/src/WeihanLi.Common/Helpers/ConfigurationHelper.cs
/// </summary>
public static class ConfigurationHelper
{
private static NameValueCollection _appSettings;

static ConfigurationHelper()
{
_appSettings = ConfigurationManager.AppSettings;
}

/// <summary>
/// 获取配置文件中AppSetting节点的值
/// </summary>
/// <param name="key">设置的键值</param>
/// <returns>键值对应的值</returns>
public static string AppSetting(string key) => _appSettings[key] ?? string.Empty;

/// <summary>
/// 获取配置文件中AppSetting节点的值
/// </summary>
/// <param name="key">设置的键值</param>
/// <returns>键值对应的值</returns>
public static T AppSetting<T>(string key) => AppSetting(key).StringToType<T>();

/// <summary>
/// 更新 AppSetting 配置
/// </summary>
/// <typeparam name="T">value type</typeparam>
/// <param name="key">app setting key</param>
/// <param name="value">app setting value</param>
public static void UpdateAppSetting<T>(string key, T value) => UpdateAppSetting(key, value.ToJsonOrString());

/// <summary>
/// Helper for ConfigurationManager
/// https://github.com/WeihanLi/WeihanLi.Common/blob/dev/src/WeihanLi.Common/Helpers/ConfigurationHelper.cs
/// 更新 AppSetting 配置
/// </summary>
public static class ConfigurationHelper
/// <param name="key">app setting key</param>
/// <param name="value">app setting value</param>
public static void UpdateAppSetting(string key, string value)
{
private static NameValueCollection _appSettings;

static ConfigurationHelper()
{
_appSettings = ConfigurationManager.AppSettings;
}

/// <summary>
/// 获取配置文件中AppSetting节点的值
/// </summary>
/// <param name="key">设置的键值</param>
/// <returns>键值对应的值</returns>
public static string AppSetting(string key) => _appSettings[key] ?? string.Empty;

/// <summary>
/// 获取配置文件中AppSetting节点的值
/// </summary>
/// <param name="key">设置的键值</param>
/// <returns>键值对应的值</returns>
public static T AppSetting<T>(string key) => AppSetting(key).StringToType<T>();

/// <summary>
/// 更新 AppSetting 配置
/// </summary>
/// <typeparam name="T">value type</typeparam>
/// <param name="key">app setting key</param>
/// <param name="value">app setting value</param>
public static void UpdateAppSetting<T>(string key, T value) => UpdateAppSetting(key, value.ToJsonOrString());

/// <summary>
/// 更新 AppSetting 配置
/// </summary>
/// <param name="key">app setting key</param>
/// <param name="value">app setting value</param>
public static void UpdateAppSetting(string key, string value)
{
var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.AppSettings.Settings.Remove(key);
config.AppSettings.Settings.Add(key, value);
config.Save(ConfigurationSaveMode.Minimal);
// Force a reload of a changed section.
ConfigurationManager.RefreshSection("appSettings");
_appSettings = ConfigurationManager.AppSettings;
}
var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.AppSettings.Settings.Remove(key);
config.AppSettings.Settings.Add(key, value);
config.Save(ConfigurationSaveMode.Minimal);
// Force a reload of a changed section.
ConfigurationManager.RefreshSection("appSettings");
_appSettings = ConfigurationManager.AppSettings;
}
}
Loading

0 comments on commit d6cdb7e

Please sign in to comment.