-
-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Storage in AppConfiguration instead of backed in
- Loading branch information
1 parent
6ebd382
commit b41e264
Showing
6 changed files
with
123 additions
and
36 deletions.
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
46
...DotNet.Blog.IntegrationTests/Web/RegistrationExtensions/StorageProviderExtensionsTests.cs
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,46 @@ | ||
using System; | ||
using System.Linq; | ||
using FluentAssertions; | ||
using LinkDotNet.Blog.Web.RegistrationExtensions; | ||
using LinkDotNet.Infrastructure.Persistence; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Moq; | ||
using Xunit; | ||
|
||
namespace LinkDotNet.Blog.IntegrationTests.Web.RegistrationExtensions | ||
{ | ||
public class StorageProviderExtensionsTests | ||
{ | ||
[Theory] | ||
[InlineData("SqlServer")] | ||
[InlineData("SqliteServer")] | ||
[InlineData("RavenDb")] | ||
[InlineData("InMemory")] | ||
public void ShouldRegisterPersistenceProvider(string persistenceKey) | ||
{ | ||
var collection = new ServiceCollection(); | ||
var config = new Mock<IConfiguration>(); | ||
config.Setup(c => c["PersistenceProvider"]) | ||
.Returns(persistenceKey); | ||
|
||
collection.AddStorageProvider(config.Object); | ||
|
||
var enumerable = collection.Select(c => c.ServiceType).ToList(); | ||
enumerable.Should().Contain(typeof(IRepository<>)); | ||
} | ||
|
||
[Fact] | ||
public void ShouldThrowExceptionWhenNotKnown() | ||
{ | ||
var collection = new ServiceCollection(); | ||
var config = new Mock<IConfiguration>(); | ||
config.Setup(c => c["PersistenceProvider"]) | ||
.Returns("not known"); | ||
|
||
Action act = () => collection.AddStorageProvider(config.Object); | ||
|
||
act.Should().Throw<Exception>(); | ||
} | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
LinkDotNet.Blog.Web/RegistrationExtensions/StorageProviderExtensions.cs
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,31 @@ | ||
using LinkDotNet.Infrastructure.Persistence; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace LinkDotNet.Blog.Web.RegistrationExtensions | ||
{ | ||
public static class StorageProviderExtensions | ||
{ | ||
public static void AddStorageProvider(this IServiceCollection services, IConfiguration configuration) | ||
{ | ||
var persistenceProvider = PersistenceProvider.Create(configuration["PersistenceProvider"]); | ||
|
||
if (persistenceProvider == PersistenceProvider.InMemory) | ||
{ | ||
services.UseInMemoryAsStorageProvider(); | ||
} | ||
else if (persistenceProvider == PersistenceProvider.RavenDb) | ||
{ | ||
services.UseRavenDbAsStorageProvider(); | ||
} | ||
else if (persistenceProvider == PersistenceProvider.SqliteServer) | ||
{ | ||
services.UseSqliteAsStorageProvider(); | ||
} | ||
else if (persistenceProvider == PersistenceProvider.SqlServer) | ||
{ | ||
services.UseSqlAsStorageProvider(); | ||
} | ||
} | ||
} | ||
} |
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
17 changes: 17 additions & 0 deletions
17
LinkDotNet.Infrastructure/Persistence/PersistenceProvider.cs
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,17 @@ | ||
using LinkDotNet.Domain; | ||
|
||
namespace LinkDotNet.Infrastructure.Persistence | ||
{ | ||
public class PersistenceProvider : Enumeration<PersistenceProvider> | ||
{ | ||
public static readonly PersistenceProvider SqlServer = new(nameof(SqlServer)); | ||
public static readonly PersistenceProvider SqliteServer = new(nameof(SqliteServer)); | ||
public static readonly PersistenceProvider RavenDb = new(nameof(RavenDb)); | ||
public static readonly PersistenceProvider InMemory = new(nameof(InMemory)); | ||
|
||
protected PersistenceProvider(string key) | ||
: base(key) | ||
{ | ||
} | ||
} | ||
} |
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