forked from Kros-sk/Kros.KORM.Extensions.Asp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Connection settings are separated from connection string (Kros-sk#8)
- Loading branch information
Showing
11 changed files
with
213 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
using Kros.KORM; | ||
using Kros.KORM.Extensions.Asp; | ||
|
||
namespace Microsoft.Extensions.Configuration | ||
{ | ||
/// <summary> | ||
/// Extensions for <see cref="IConfiguration"/>. | ||
/// </summary> | ||
public static class ConfigurationExtensions | ||
{ | ||
/// <summary> | ||
/// Returns connection settings with <paramref name="name"/> from configuration. Connection settings are merged from | ||
/// sections <c>ConnectionStrings</c> and <c>KormSettings</c>. If key <paramref name="name"/> does not exist | ||
/// in either section, <see langword="null"/> is returned. | ||
/// </summary> | ||
/// <param name="configuration">The configuration.</param> | ||
/// <param name="name">The connection string key.</param> | ||
/// <returns><see cref="KormConnectionSettings"/> or <see langword="null"/>.</returns> | ||
public static KormConnectionSettings GetKormConnectionString(this IConfiguration configuration, string name) | ||
{ | ||
string connectionString = configuration.GetConnectionString(name); | ||
IConfigurationSection section = configuration.GetSection(KormBuilder.KormSettingsSectionName + ":" + name); | ||
|
||
if (section.Exists()) | ||
{ | ||
KormConnectionSettings settings = section.Get<KormConnectionSettings>(); | ||
settings.ConnectionString = connectionString; | ||
return settings; | ||
} | ||
else if (connectionString != null) | ||
{ | ||
return new KormConnectionSettings() { ConnectionString = connectionString }; | ||
} | ||
return null; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
using FluentAssertions; | ||
using Microsoft.Extensions.Configuration; | ||
using Xunit; | ||
|
||
namespace Kros.KORM.Extensions.Api.UnitTests | ||
{ | ||
public class ConfigurationExtensionsShould | ||
{ | ||
private const string TestConnectionString = "server=servername\\instancename;initial catalog=database"; | ||
private const string DefaultKormProvider = Kros.Data.SqlServer.SqlServerDataHelper.ClientId; | ||
private const bool DefaultAutoMigrate = false; | ||
|
||
[Theory] | ||
[InlineData("NoKormSettings", TestConnectionString, DefaultKormProvider, DefaultAutoMigrate)] | ||
[InlineData("NoConnectionString", null, "DolorSitAmet", DefaultAutoMigrate)] | ||
[InlineData("AutoMigrateTrue", TestConnectionString, DefaultKormProvider, true)] | ||
[InlineData("CustomProvider", TestConnectionString, "LoremIpsum", true)] | ||
[InlineData("InvalidSettings", TestConnectionString, DefaultKormProvider, DefaultAutoMigrate)] | ||
public void LoadCorrectKormConnectionSetings(string name, string connectionString, string kormProvider, bool autoMigrate) | ||
{ | ||
IConfigurationRoot configuration = ConfigurationHelper.GetConfiguration(); | ||
var expected = new KormConnectionSettings() | ||
{ | ||
ConnectionString = connectionString, | ||
KormProvider = kormProvider, | ||
AutoMigrate = autoMigrate | ||
}; | ||
|
||
KormConnectionSettings actual = configuration.GetKormConnectionString(name); | ||
|
||
actual.Should().BeEquivalentTo(expected); | ||
} | ||
|
||
[Fact] | ||
public void ReturnNullIfNameIsNotInEitherSection() | ||
{ | ||
IConfigurationRoot configuration = ConfigurationHelper.GetConfiguration(); | ||
configuration.GetKormConnectionString("ThisNameDoesNotExist").Should().BeNull(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.