Skip to content

Commit

Permalink
[linqpad6] Support appsettings.json for typed data context (#52)
Browse files Browse the repository at this point in the history
* [LINQPAD6] add support for appsettings.json for static connection

* release notes
  • Loading branch information
MaceWindu authored Apr 24, 2021
1 parent b7c7edf commit 2ef1dcb
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 1 deletion.
1 change: 1 addition & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,6 @@
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp" Version="3.9.0" />
<PackageVersion Include="Npgsql" Version="5.0.4" />
<PackageVersion Include="FirebirdSql.Data.FirebirdClient" Version="8.0.1" />
<PackageVersion Include="System.Text.Json" Version="5.0.2" />
</ItemGroup>
</Project>
56 changes: 56 additions & 0 deletions Source/AppJsonConfig.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#if NETCORE
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.Json;
using LinqToDB.Configuration;

namespace LinqToDB.LINQPad
{
internal class AppJsonConfig : ILinqToDBSettings
{
public static ILinqToDBSettings Load(string configPath)
{
var config = JsonSerializer.Deserialize<JsonConfig>(File.ReadAllText(configPath));

return new AppJsonConfig(config?.ConnectionStrings?.Select(entry => (IConnectionStringSettings)new ConnectionStringSettings(entry.Key, entry.Value)).ToArray()
?? Array.Empty<IConnectionStringSettings>());
}

private readonly IConnectionStringSettings[] _connectionStrings;

public AppJsonConfig(IConnectionStringSettings[] connectionStrings)
{
_connectionStrings = connectionStrings;
}

IEnumerable<IDataProviderSettings> ILinqToDBSettings.DataProviders => Array.Empty<IDataProviderSettings>();
string? ILinqToDBSettings.DefaultConfiguration => null;
string? ILinqToDBSettings.DefaultDataProvider => null;
IEnumerable<IConnectionStringSettings> ILinqToDBSettings.ConnectionStrings => _connectionStrings;

private class JsonConfig
{
public IDictionary<string, string>? ConnectionStrings { get; set; }
}

private class ConnectionStringSettings : IConnectionStringSettings
{
private readonly string _name;
private readonly string _connectionString;

public ConnectionStringSettings(string name, string connectionString)
{
_name = name;
_connectionString = connectionString;
}

string IConnectionStringSettings.ConnectionString => _connectionString;
string IConnectionStringSettings.Name => _name;
string? IConnectionStringSettings.ProviderName => null;
bool IConnectionStringSettings.IsGlobal => false;
}
}
}
#endif
31 changes: 30 additions & 1 deletion Source/ConnectionDialog.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,35 @@ void ChooseConfiguration(object sender, RoutedEventArgs e)
{
var oldCursor = Cursor;

#if NETCORE
try
{
Mouse.OverrideCursor = Cursors.Wait;

if (Model.AppConfigPath!.EndsWith(".json", StringComparison.OrdinalIgnoreCase))
{
var config = AppJsonConfig.Load(Model.AppConfigPath!);
if (config.ConnectionStrings.Any())
{
var result = (string?)Dialogs.PickFromList("Choose Connection String", config.ConnectionStrings.Select(_ => _.Name).ToArray());

if (result != null)
Model.CustomConfiguration = result;
}
}

return;
}
catch (Exception ex)
{
MessageBox.Show(this, ex.Message, "Config file open error", MessageBoxButton.OK, MessageBoxImage.Error);
}
finally
{
Mouse.OverrideCursor = oldCursor;
}
#endif

try
{
Mouse.OverrideCursor = Cursors.Wait;
Expand All @@ -247,7 +276,7 @@ void ChooseConfiguration(object sender, RoutedEventArgs e)

Mouse.OverrideCursor = oldCursor;

var result = (string?)Dialogs.PickFromList("Choose Custom Type", configurations.ToArray());
var result = (string?)Dialogs.PickFromList("Choose Connection String", configurations.ToArray());

if (result != null)
Model.CustomConfiguration = result;
Expand Down
10 changes: 10 additions & 0 deletions Source/LinqToDBStaticDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ public override ParameterDescriptor[] GetContextConstructorParameters(IConnectio

public override object[] GetContextConstructorArguments(IConnectionInfo cxInfo)
{
TryLoadAppSettingsJson(cxInfo);

var configuration = cxInfo.DriverData.Element(CX.CustomConfiguration)?.Value;

if (configuration != null)
Expand Down Expand Up @@ -78,5 +80,13 @@ public override void TearDownContext(IConnectionInfo cxInfo, object context, Que
dynamic ctx = context;
ctx.Dispose();
}

private void TryLoadAppSettingsJson(IConnectionInfo cxInfo)
{
#if NETCORE
if (cxInfo.AppConfigPath?.EndsWith(".json", StringComparison.OrdinalIgnoreCase) == true)
DataConnection.DefaultSettings = AppJsonConfig.Load(cxInfo.AppConfigPath!);
#endif
}
}
}
1 change: 1 addition & 0 deletions Source/linq2db.LINQPad.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
<PackageReference Include="Oracle.ManagedDataAccess.Core" />
<PackageReference Include="IBM.Data.DB2.Core" />
<PackageReference Include="dotMorten.Microsoft.SqlServer.Types" />
<PackageReference Include="System.Text.Json" />
</ItemGroup>

<ItemGroup>
Expand Down
1 change: 1 addition & 0 deletions release-notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- [ALL] fix issue with command timeout option being ignored ([#47](https://github.com/linq2db/linq2db.LINQPad/issues/47))
- [LINQPAD6] dependency update: FirebirdSql.Data.FirebirdClient 7.10.1 -> 8.0.1
- [LINQPAD6] improve fix for [#39](https://github.com/linq2db/linq2db.LINQPad/issues/39)
- [LINQPAD6] support connection configuration using appsettings.json for typed data context ([#38](https://github.com/linq2db/linq2db.LINQPad/issues/38))

# Release 3.3.1
- [LINQPAD6] fix incorrect versions for dependencies in nuget version of driver
Expand Down

0 comments on commit 2ef1dcb

Please sign in to comment.