Skip to content

Commit

Permalink
Storage in AppConfiguration instead of backed in
Browse files Browse the repository at this point in the history
  • Loading branch information
linkdotnet committed Sep 16, 2021
1 parent 6ebd382 commit b41e264
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 36 deletions.
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>();
}
}
}
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();
}
}
}
}
13 changes: 2 additions & 11 deletions LinkDotNet.Blog.Web/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,8 @@ public void ConfigureServices(IServiceCollection services)
{
services.AddRazorPages();
services.AddServerSideBlazor();
services.AddSingleton(service =>
AppConfigurationFactory.Create(service.GetService<IConfiguration>()));

// This can be extended to use other repositories
services.UseSqlAsStorageProvider();
/****************
* Possible Storage Providers:
* services.UseSqliteAsStorageProvider();
* services.UseRavenDbAsStorageProvider();
* services.UseInMemoryAsStorageProvider();
*/
services.AddSingleton(_ => AppConfigurationFactory.Create(Configuration));
services.AddStorageProvider(Configuration);

// Here you can setup up your identity provider
if (environment.IsDevelopment())
Expand Down
3 changes: 2 additions & 1 deletion LinkDotNet.Blog.Web/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"BackgroundUrl": "assets/profile-background.webp",
"ProfilePictureUrl": "assets/profile-picture.webp"
},
"PersistenceProvider": "SqlServer",
"ConnectionString": "",
"DatabaseName": "",
"Auth0": {
Expand All @@ -29,4 +30,4 @@
"Heading": "Software Engineer",
"ProfilePictureUrl": "assets/profile-picture.webp"
}
}
}
17 changes: 17 additions & 0 deletions LinkDotNet.Infrastructure/Persistence/PersistenceProvider.cs
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)
{
}
}
}
49 changes: 25 additions & 24 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ The appsettings.json file has a lot of options to customize the content of the b

```json
{
...
"BlogName": "linkdotnet",
"GithubAccountUrl": "",
"LinkedInAccountUrl": "",
Expand All @@ -31,6 +30,7 @@ The appsettings.json file has a lot of options to customize the content of the b
"BackgroundUrl": "assets/profile-background.webp",
"ProfilePictureUrl": "assets/profile-picture.webp"
},
"PersistenceProvider": "SqlServer",
"ConnectionString": "",
"DatabaseName": "",
"Auth0": {
Expand All @@ -49,33 +49,34 @@ The appsettings.json file has a lot of options to customize the content of the b

```

| Property | Type | Description |
| ------------------------- | -------------- | ------------------------------------------------------------------------------------------------------------------------------------------------- |
| BlogName | string | Name of your blog. Is used in the navbar and is used as the title of the page. |
| GithubAccountUrl | string | Url to your github account. If not set the navigation link is not shown |
| LinkedInAccountUrl | string | Url to your LinkedIn account. If not set the navigation link is not shown |
| Introduction | | Is used for the introduction part of the blog |
| Description | MarkdownString | Small introduction text for yourself. This is also used for `<meta name="description">` tag. For this the markup will be converted to plain text. |
| BackgroundUrl | string | Url or path to the background image |
| ProfilePictureUrl | string | Url or path to your profile picture |
| ConnectionString | string | Is used for connection to a database. Not used when `InMemoryStorageProvider` is used |
| DatabaseName | string | Name of the database. Only used with `RavenDbStorageProvider` |
| Auth0 | | Configuration for setting up Auth0 |
| Domain | string | See more details here: https://manage.auth0.com/dashboard/ |
| ClientId | string | See more details here: https://manage.auth0.com/dashboard/ |
| ClientSecret | string | See more details here: https://manage.auth0.com/dashboard/ |
| BlogPostsPerPage | int | Gives the amount of blog posts loaded and display per page. For more the user has to use the navigation |
| IsAboutMeEnabled | bool | If true, enalbes the "About Me" page |
| AboutMeProfileInformation | node | If `IsAboutMeEnabled` is set to `false` this node can be left empty. |
| Name | string | Name, which is displayed on top of the profile card |
| Heading | string | Displayed under the name. For example job title |
| ProfilePictureUrl | string | Displayed profile picture |
| Property | Type | Description |
| ------------------------- | -------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------- |
| BlogName | string | Name of your blog. Is used in the navbar and is used as the title of the page. |
| GithubAccountUrl | string | Url to your github account. If not set the navigation link is not shown |
| LinkedInAccountUrl | string | Url to your LinkedIn account. If not set the navigation link is not shown |
| Introduction | | Is used for the introduction part of the blog |
| Description | MarkdownString | Small introduction text for yourself. This is also used for `<meta name="description">` tag. For this the markup will be converted to plain text. |
| BackgroundUrl | string | Url or path to the background image |
| ProfilePictureUrl | string | Url or path to your profile picture |
| PersistenceProvider | string | Declares the type of the storage provider (one of the following: `SqlServer`, `SqliteServer`, `RavenDb`, `InMemory`). More in-depth explanation down below |
| ConnectionString | string | Is used for connection to a database. Not used when `InMemoryStorageProvider` is used |
| DatabaseName | string | Name of the database. Only used with `RavenDbStorageProvider` |
| Auth0 | | Configuration for setting up Auth0 |
| Domain | string | See more details here: https://manage.auth0.com/dashboard/ |
| ClientId | string | See more details here: https://manage.auth0.com/dashboard/ |
| ClientSecret | string | See more details here: https://manage.auth0.com/dashboard/ |
| BlogPostsPerPage | int | Gives the amount of blog posts loaded and display per page. For more the user has to use the navigation |
| IsAboutMeEnabled | bool | If true, enalbes the "About Me" page |
| AboutMeProfileInformation | node | If `IsAboutMeEnabled` is set to `false` this node can be left empty. |
| Name | string | Name, which is displayed on top of the profile card |
| Heading | string | Displayed under the name. For example job title |
| ProfilePictureUrl | string | Displayed profile picture |

The usage might shift directly into the extension methods, where they are used.

## Storage Provider
Currently there are 4 Storage-Provider:
* InMemory - Basically a list holding your data (per request)
* InMemory - Basically a list holding your data (per request). If the User hits a hard reload, the data is gone.
* RavenDb - As the name suggests for RavenDb
* Sqlite - Based on EF Core, so it can be easily adapted for other Sql Dialects
* SqlServer - Based on EF Core, so it can be easily adapted for other Sql Dialects
Expand Down Expand Up @@ -120,4 +121,4 @@ Furthermore the following tags are set:
| Tag | Index | Display Blog Post |
| ---------------------------------------- | ------------------------------------ | ----------------------------- |
| Title of the web page | Defined in AppConfiguration.BlogName | Title of the blogpost |
| &lt;meta name="keyword" content="" /&gt; | not set | Tags defined in the Blog Post |
| &lt;meta name="keyword" content="" /&gt; | not set | Tags defined in the Blog Post |

0 comments on commit b41e264

Please sign in to comment.