From 6ba85d1eb3dfc2137b13728ad75f3e900365c030 Mon Sep 17 00:00:00 2001 From: Diego Faria Date: Fri, 23 Jun 2023 18:42:53 -0400 Subject: [PATCH 1/9] Add new enumeration for posgres --- .../Persistence/PersistenceProvider.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/LinkDotNet.Blog.Infrastructure/Persistence/PersistenceProvider.cs b/src/LinkDotNet.Blog.Infrastructure/Persistence/PersistenceProvider.cs index 2c3122aa..5eafc6e5 100644 --- a/src/LinkDotNet.Blog.Infrastructure/Persistence/PersistenceProvider.cs +++ b/src/LinkDotNet.Blog.Infrastructure/Persistence/PersistenceProvider.cs @@ -9,6 +9,7 @@ public sealed class PersistenceProvider : Enumeration public static readonly PersistenceProvider RavenDb = new(nameof(RavenDb)); public static readonly PersistenceProvider InMemory = new(nameof(InMemory)); public static readonly PersistenceProvider MySql = new(nameof(MySql)); + public static readonly PersistenceProvider Postgres = new(nameof(Postgres)); private PersistenceProvider(string key) : base(key) From 26afeb2b64532bd9d7cd08dee0db4e63d9b1a15b Mon Sep 17 00:00:00 2001 From: Diego Faria Date: Fri, 23 Jun 2023 18:43:34 -0400 Subject: [PATCH 2/9] Add Npsql package dependency for postgres connection --- .../LinkDotNet.Blog.Infrastructure.csproj | 1 + 1 file changed, 1 insertion(+) diff --git a/src/LinkDotNet.Blog.Infrastructure/LinkDotNet.Blog.Infrastructure.csproj b/src/LinkDotNet.Blog.Infrastructure/LinkDotNet.Blog.Infrastructure.csproj index 3029edbd..38e453d7 100644 --- a/src/LinkDotNet.Blog.Infrastructure/LinkDotNet.Blog.Infrastructure.csproj +++ b/src/LinkDotNet.Blog.Infrastructure/LinkDotNet.Blog.Infrastructure.csproj @@ -8,6 +8,7 @@ + From c87d12cdfb3bac3c16bde2e7544cbcf6b6e0dfd2 Mon Sep 17 00:00:00 2001 From: Diego Faria Date: Fri, 23 Jun 2023 18:44:11 -0400 Subject: [PATCH 3/9] create an extension method to configure postgres --- .../SqlRegistrationExtensions.cs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/LinkDotNet.Blog.Web/RegistrationExtensions/SqlRegistrationExtensions.cs b/src/LinkDotNet.Blog.Web/RegistrationExtensions/SqlRegistrationExtensions.cs index 8405c94c..f391564c 100644 --- a/src/LinkDotNet.Blog.Web/RegistrationExtensions/SqlRegistrationExtensions.cs +++ b/src/LinkDotNet.Blog.Web/RegistrationExtensions/SqlRegistrationExtensions.cs @@ -61,4 +61,21 @@ public static void UseMySqlAsStorageProvider(this IServiceCollection services) }); services.AddScoped(typeof(IRepository<>), typeof(Repository<>)); } + + public static void UsePostgresAsStorageProvider(this IServiceCollection services) + { + services.AssertNotAlreadyRegistered(typeof(IRepository<>)); + + services.AddPooledDbContextFactory( + (s, builder) => + { + var configuration = s.GetRequiredService(); + var connectionString = configuration.ConnectionString; + builder.UseNpgsql(connectionString) +#if DEBUG + .EnableDetailedErrors() +#endif + ; + }); + } } From 939d40455b0e8370fbcd63c1070918cb50b7610d Mon Sep 17 00:00:00 2001 From: Diego Faria Date: Fri, 23 Jun 2023 18:45:23 -0400 Subject: [PATCH 4/9] Include postgres as a new storage provider map --- .../RegistrationExtensions/StorageProviderExtensions.cs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/LinkDotNet.Blog.Web/RegistrationExtensions/StorageProviderExtensions.cs b/src/LinkDotNet.Blog.Web/RegistrationExtensions/StorageProviderExtensions.cs index a00705a1..5fda2a23 100644 --- a/src/LinkDotNet.Blog.Web/RegistrationExtensions/StorageProviderExtensions.cs +++ b/src/LinkDotNet.Blog.Web/RegistrationExtensions/StorageProviderExtensions.cs @@ -39,6 +39,11 @@ public static void AddStorageProvider(this IServiceCollection services, IConfigu services.UseMySqlAsStorageProvider(); services.RegisterCachedRepository>(); } + else if (persistenceProvider == PersistenceProvider.Postgres) + { + services.UsePostgresAsStorageProvider(); + services.RegisterCachedRepository>(); + } } private static void RegisterCachedRepository(this IServiceCollection services) From d969e3a6de957632fa6412a3b2d4826be6917510 Mon Sep 17 00:00:00 2001 From: Diego Faria Date: Fri, 23 Jun 2023 18:46:05 -0400 Subject: [PATCH 5/9] Update README.md with postgres informations for configuration --- Readme.md | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/Readme.md b/Readme.md index 73ca4491..754fb4be 100644 --- a/Readme.md +++ b/Readme.md @@ -1,4 +1,4 @@ -# LinkDotNet.Blog +# LinkDotNet.Blog [![.NET](https://github.com/linkdotnet/Blog/actions/workflows/dotnet.yml/badge.svg?branch=master)](https://github.com/linkdotnet/Blog/actions/workflows/dotnet.yml) [![CodeQL](https://github.com/linkdotnet/Blog/actions/workflows/codeql.yml/badge.svg)](https://github.com/linkdotnet/Blog/actions/workflows/codeql.yml) @@ -103,6 +103,7 @@ Currently, there are 5 Storage-Provider: * Sqlite - Based on EF Core, it can be easily adapted for other Sql Dialects. The tables are automatically created. * SqlServer - Based on EF Core, it can be easily adapted for other Sql Dialects. The tables are automatically created. * MySql - Based on EF Core - also supports MariaDB. + * Postgres - Based on EF Core. The default (when you clone the repository) is the `InMemory` option. That means every time you restart the service, all posts and related objects are gone. @@ -243,3 +244,10 @@ For MySql use the following: "PersistenceProvider": "MySql" "ConnectionString": "Server=YOURSERVER;User ID=YOURUSERID;Password=YOURPASSWORD;Database=YOURDATABASE" ``` + +For Postgres use the following: + +``` +"PersistenceProvider": "Postgres" +"ConnectionString": "Host=YOURSERVER;Database=YOURDATABASE;Username=YOURUSERNAME;Password=YOURPASSWORD" +``` From 09176308d5e43cf043868c8a0bbf05bf6c2438b3 Mon Sep 17 00:00:00 2001 From: Diego Faria Date: Sat, 24 Jun 2023 22:20:27 -0400 Subject: [PATCH 6/9] Change provider name to postegrsql --- .../Persistence/PersistenceProvider.cs | 2 +- .../RegistrationExtensions/SqlRegistrationExtensions.cs | 2 +- .../RegistrationExtensions/StorageProviderExtensions.cs | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/LinkDotNet.Blog.Infrastructure/Persistence/PersistenceProvider.cs b/src/LinkDotNet.Blog.Infrastructure/Persistence/PersistenceProvider.cs index 5eafc6e5..be015f33 100644 --- a/src/LinkDotNet.Blog.Infrastructure/Persistence/PersistenceProvider.cs +++ b/src/LinkDotNet.Blog.Infrastructure/Persistence/PersistenceProvider.cs @@ -9,7 +9,7 @@ public sealed class PersistenceProvider : Enumeration public static readonly PersistenceProvider RavenDb = new(nameof(RavenDb)); public static readonly PersistenceProvider InMemory = new(nameof(InMemory)); public static readonly PersistenceProvider MySql = new(nameof(MySql)); - public static readonly PersistenceProvider Postgres = new(nameof(Postgres)); + public static readonly PersistenceProvider Postgresql = new(nameof(Postgresql)); private PersistenceProvider(string key) : base(key) diff --git a/src/LinkDotNet.Blog.Web/RegistrationExtensions/SqlRegistrationExtensions.cs b/src/LinkDotNet.Blog.Web/RegistrationExtensions/SqlRegistrationExtensions.cs index f391564c..0c88e8d9 100644 --- a/src/LinkDotNet.Blog.Web/RegistrationExtensions/SqlRegistrationExtensions.cs +++ b/src/LinkDotNet.Blog.Web/RegistrationExtensions/SqlRegistrationExtensions.cs @@ -62,7 +62,7 @@ public static void UseMySqlAsStorageProvider(this IServiceCollection services) services.AddScoped(typeof(IRepository<>), typeof(Repository<>)); } - public static void UsePostgresAsStorageProvider(this IServiceCollection services) + public static void UsePostgresqlAsStorageProvider(this IServiceCollection services) { services.AssertNotAlreadyRegistered(typeof(IRepository<>)); diff --git a/src/LinkDotNet.Blog.Web/RegistrationExtensions/StorageProviderExtensions.cs b/src/LinkDotNet.Blog.Web/RegistrationExtensions/StorageProviderExtensions.cs index 5fda2a23..2451a378 100644 --- a/src/LinkDotNet.Blog.Web/RegistrationExtensions/StorageProviderExtensions.cs +++ b/src/LinkDotNet.Blog.Web/RegistrationExtensions/StorageProviderExtensions.cs @@ -39,9 +39,9 @@ public static void AddStorageProvider(this IServiceCollection services, IConfigu services.UseMySqlAsStorageProvider(); services.RegisterCachedRepository>(); } - else if (persistenceProvider == PersistenceProvider.Postgres) + else if (persistenceProvider == PersistenceProvider.Postgresql) { - services.UsePostgresAsStorageProvider(); + services.UsePostgresqlAsStorageProvider(); services.RegisterCachedRepository>(); } } From 65f03a90b64567f640f1f8094350d86414b57980 Mon Sep 17 00:00:00 2001 From: Diego Faria Date: Sat, 24 Jun 2023 22:20:56 -0400 Subject: [PATCH 7/9] Add DI for repositories --- .../RegistrationExtensions/SqlRegistrationExtensions.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/LinkDotNet.Blog.Web/RegistrationExtensions/SqlRegistrationExtensions.cs b/src/LinkDotNet.Blog.Web/RegistrationExtensions/SqlRegistrationExtensions.cs index 0c88e8d9..663de0d7 100644 --- a/src/LinkDotNet.Blog.Web/RegistrationExtensions/SqlRegistrationExtensions.cs +++ b/src/LinkDotNet.Blog.Web/RegistrationExtensions/SqlRegistrationExtensions.cs @@ -77,5 +77,7 @@ public static void UsePostgresqlAsStorageProvider(this IServiceCollection servic #endif ; }); + + services.AddScoped(typeof(IRepository<>), typeof(Repository<>)); } } From 6493143147dfc6965742c59fb4e5a76ab4858106 Mon Sep 17 00:00:00 2001 From: Diego Faria Date: Sat, 24 Jun 2023 22:21:36 -0400 Subject: [PATCH 8/9] Include new storage provider in tests --- .../StorageProviderRegistrationExtensionsTests.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/LinkDotNet.Blog.UnitTests/StorageProviderRegistrationExtensionsTests.cs b/tests/LinkDotNet.Blog.UnitTests/StorageProviderRegistrationExtensionsTests.cs index 18372a4c..55222c9c 100644 --- a/tests/LinkDotNet.Blog.UnitTests/StorageProviderRegistrationExtensionsTests.cs +++ b/tests/LinkDotNet.Blog.UnitTests/StorageProviderRegistrationExtensionsTests.cs @@ -14,6 +14,7 @@ public class StorageProviderRegistrationExtensionsTests new object[] { new Action(services => services.UseInMemoryAsStorageProvider()) }, new object[] { new Action(services => services.UseRavenDbAsStorageProvider()) }, new object[] { new Action(services => services.UseMySqlAsStorageProvider()) }, + new object[] { new Action(services => services.UsePostgresqlAsStorageProvider()) }, }; [Theory] From 0e88aeea2e31e952158fce5c0ff74a20ac51ebf8 Mon Sep 17 00:00:00 2001 From: Diego Faria Date: Sat, 24 Jun 2023 22:22:07 -0400 Subject: [PATCH 9/9] Update README.md --- Readme.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Readme.md b/Readme.md index 754fb4be..ceaecf4f 100644 --- a/Readme.md +++ b/Readme.md @@ -97,13 +97,13 @@ The appsettings.json file has a lot of options to customize the content of the b | PatreonName | string | Enables the "Become a patreon" button that redirects to patreon.com. Only pass the user name (public profile) as user name. | ## Storage Provider -Currently, there are 5 Storage-Provider: +Currently, there are 6 Storage-Provider: * 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. RavenDb automatically creates all the documents, if a database name is provided. * Sqlite - Based on EF Core, it can be easily adapted for other Sql Dialects. The tables are automatically created. * SqlServer - Based on EF Core, it can be easily adapted for other Sql Dialects. The tables are automatically created. * MySql - Based on EF Core - also supports MariaDB. - * Postgres - Based on EF Core. + * Postgresql - Based on EF Core. The default (when you clone the repository) is the `InMemory` option. That means every time you restart the service, all posts and related objects are gone.