Skip to content

Commit b41e264

Browse files
committed
Storage in AppConfiguration instead of backed in
1 parent 6ebd382 commit b41e264

File tree

6 files changed

+123
-36
lines changed

6 files changed

+123
-36
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using System;
2+
using System.Linq;
3+
using FluentAssertions;
4+
using LinkDotNet.Blog.Web.RegistrationExtensions;
5+
using LinkDotNet.Infrastructure.Persistence;
6+
using Microsoft.Extensions.Configuration;
7+
using Microsoft.Extensions.DependencyInjection;
8+
using Moq;
9+
using Xunit;
10+
11+
namespace LinkDotNet.Blog.IntegrationTests.Web.RegistrationExtensions
12+
{
13+
public class StorageProviderExtensionsTests
14+
{
15+
[Theory]
16+
[InlineData("SqlServer")]
17+
[InlineData("SqliteServer")]
18+
[InlineData("RavenDb")]
19+
[InlineData("InMemory")]
20+
public void ShouldRegisterPersistenceProvider(string persistenceKey)
21+
{
22+
var collection = new ServiceCollection();
23+
var config = new Mock<IConfiguration>();
24+
config.Setup(c => c["PersistenceProvider"])
25+
.Returns(persistenceKey);
26+
27+
collection.AddStorageProvider(config.Object);
28+
29+
var enumerable = collection.Select(c => c.ServiceType).ToList();
30+
enumerable.Should().Contain(typeof(IRepository<>));
31+
}
32+
33+
[Fact]
34+
public void ShouldThrowExceptionWhenNotKnown()
35+
{
36+
var collection = new ServiceCollection();
37+
var config = new Mock<IConfiguration>();
38+
config.Setup(c => c["PersistenceProvider"])
39+
.Returns("not known");
40+
41+
Action act = () => collection.AddStorageProvider(config.Object);
42+
43+
act.Should().Throw<Exception>();
44+
}
45+
}
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using LinkDotNet.Infrastructure.Persistence;
2+
using Microsoft.Extensions.Configuration;
3+
using Microsoft.Extensions.DependencyInjection;
4+
5+
namespace LinkDotNet.Blog.Web.RegistrationExtensions
6+
{
7+
public static class StorageProviderExtensions
8+
{
9+
public static void AddStorageProvider(this IServiceCollection services, IConfiguration configuration)
10+
{
11+
var persistenceProvider = PersistenceProvider.Create(configuration["PersistenceProvider"]);
12+
13+
if (persistenceProvider == PersistenceProvider.InMemory)
14+
{
15+
services.UseInMemoryAsStorageProvider();
16+
}
17+
else if (persistenceProvider == PersistenceProvider.RavenDb)
18+
{
19+
services.UseRavenDbAsStorageProvider();
20+
}
21+
else if (persistenceProvider == PersistenceProvider.SqliteServer)
22+
{
23+
services.UseSqliteAsStorageProvider();
24+
}
25+
else if (persistenceProvider == PersistenceProvider.SqlServer)
26+
{
27+
services.UseSqlAsStorageProvider();
28+
}
29+
}
30+
}
31+
}

LinkDotNet.Blog.Web/Startup.cs

+2-11
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,8 @@ public void ConfigureServices(IServiceCollection services)
2929
{
3030
services.AddRazorPages();
3131
services.AddServerSideBlazor();
32-
services.AddSingleton(service =>
33-
AppConfigurationFactory.Create(service.GetService<IConfiguration>()));
34-
35-
// This can be extended to use other repositories
36-
services.UseSqlAsStorageProvider();
37-
/****************
38-
* Possible Storage Providers:
39-
* services.UseSqliteAsStorageProvider();
40-
* services.UseRavenDbAsStorageProvider();
41-
* services.UseInMemoryAsStorageProvider();
42-
*/
32+
services.AddSingleton(_ => AppConfigurationFactory.Create(Configuration));
33+
services.AddStorageProvider(Configuration);
4334

4435
// Here you can setup up your identity provider
4536
if (environment.IsDevelopment())

LinkDotNet.Blog.Web/appsettings.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"BackgroundUrl": "assets/profile-background.webp",
1616
"ProfilePictureUrl": "assets/profile-picture.webp"
1717
},
18+
"PersistenceProvider": "SqlServer",
1819
"ConnectionString": "",
1920
"DatabaseName": "",
2021
"Auth0": {
@@ -29,4 +30,4 @@
2930
"Heading": "Software Engineer",
3031
"ProfilePictureUrl": "assets/profile-picture.webp"
3132
}
32-
}
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using LinkDotNet.Domain;
2+
3+
namespace LinkDotNet.Infrastructure.Persistence
4+
{
5+
public class PersistenceProvider : Enumeration<PersistenceProvider>
6+
{
7+
public static readonly PersistenceProvider SqlServer = new(nameof(SqlServer));
8+
public static readonly PersistenceProvider SqliteServer = new(nameof(SqliteServer));
9+
public static readonly PersistenceProvider RavenDb = new(nameof(RavenDb));
10+
public static readonly PersistenceProvider InMemory = new(nameof(InMemory));
11+
12+
protected PersistenceProvider(string key)
13+
: base(key)
14+
{
15+
}
16+
}
17+
}

Readme.md

+25-24
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ The appsettings.json file has a lot of options to customize the content of the b
2222

2323
```json
2424
{
25-
...
2625
"BlogName": "linkdotnet",
2726
"GithubAccountUrl": "",
2827
"LinkedInAccountUrl": "",
@@ -31,6 +30,7 @@ The appsettings.json file has a lot of options to customize the content of the b
3130
"BackgroundUrl": "assets/profile-background.webp",
3231
"ProfilePictureUrl": "assets/profile-picture.webp"
3332
},
33+
"PersistenceProvider": "SqlServer",
3434
"ConnectionString": "",
3535
"DatabaseName": "",
3636
"Auth0": {
@@ -49,33 +49,34 @@ The appsettings.json file has a lot of options to customize the content of the b
4949

5050
```
5151

52-
| Property | Type | Description |
53-
| ------------------------- | -------------- | ------------------------------------------------------------------------------------------------------------------------------------------------- |
54-
| BlogName | string | Name of your blog. Is used in the navbar and is used as the title of the page. |
55-
| GithubAccountUrl | string | Url to your github account. If not set the navigation link is not shown |
56-
| LinkedInAccountUrl | string | Url to your LinkedIn account. If not set the navigation link is not shown |
57-
| Introduction | | Is used for the introduction part of the blog |
58-
| 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. |
59-
| BackgroundUrl | string | Url or path to the background image |
60-
| ProfilePictureUrl | string | Url or path to your profile picture |
61-
| ConnectionString | string | Is used for connection to a database. Not used when `InMemoryStorageProvider` is used |
62-
| DatabaseName | string | Name of the database. Only used with `RavenDbStorageProvider` |
63-
| Auth0 | | Configuration for setting up Auth0 |
64-
| Domain | string | See more details here: https://manage.auth0.com/dashboard/ |
65-
| ClientId | string | See more details here: https://manage.auth0.com/dashboard/ |
66-
| ClientSecret | string | See more details here: https://manage.auth0.com/dashboard/ |
67-
| BlogPostsPerPage | int | Gives the amount of blog posts loaded and display per page. For more the user has to use the navigation |
68-
| IsAboutMeEnabled | bool | If true, enalbes the "About Me" page |
69-
| AboutMeProfileInformation | node | If `IsAboutMeEnabled` is set to `false` this node can be left empty. |
70-
| Name | string | Name, which is displayed on top of the profile card |
71-
| Heading | string | Displayed under the name. For example job title |
72-
| ProfilePictureUrl | string | Displayed profile picture |
52+
| Property | Type | Description |
53+
| ------------------------- | -------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------- |
54+
| BlogName | string | Name of your blog. Is used in the navbar and is used as the title of the page. |
55+
| GithubAccountUrl | string | Url to your github account. If not set the navigation link is not shown |
56+
| LinkedInAccountUrl | string | Url to your LinkedIn account. If not set the navigation link is not shown |
57+
| Introduction | | Is used for the introduction part of the blog |
58+
| 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. |
59+
| BackgroundUrl | string | Url or path to the background image |
60+
| ProfilePictureUrl | string | Url or path to your profile picture |
61+
| PersistenceProvider | string | Declares the type of the storage provider (one of the following: `SqlServer`, `SqliteServer`, `RavenDb`, `InMemory`). More in-depth explanation down below |
62+
| ConnectionString | string | Is used for connection to a database. Not used when `InMemoryStorageProvider` is used |
63+
| DatabaseName | string | Name of the database. Only used with `RavenDbStorageProvider` |
64+
| Auth0 | | Configuration for setting up Auth0 |
65+
| Domain | string | See more details here: https://manage.auth0.com/dashboard/ |
66+
| ClientId | string | See more details here: https://manage.auth0.com/dashboard/ |
67+
| ClientSecret | string | See more details here: https://manage.auth0.com/dashboard/ |
68+
| BlogPostsPerPage | int | Gives the amount of blog posts loaded and display per page. For more the user has to use the navigation |
69+
| IsAboutMeEnabled | bool | If true, enalbes the "About Me" page |
70+
| AboutMeProfileInformation | node | If `IsAboutMeEnabled` is set to `false` this node can be left empty. |
71+
| Name | string | Name, which is displayed on top of the profile card |
72+
| Heading | string | Displayed under the name. For example job title |
73+
| ProfilePictureUrl | string | Displayed profile picture |
7374

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

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

0 commit comments

Comments
 (0)