From def5649097547e17f725e4812bae3b893b3d12c1 Mon Sep 17 00:00:00 2001 From: Vincent Cloutier Date: Fri, 30 Dec 2022 12:28:48 -0500 Subject: [PATCH] some cleanups --- .../DataAccessLayers/Base/PostgresBase.cs | 11 +++++++- .../DataAccessLayers/FollowersPostgresDal.cs | 18 ------------- .../TwitterUserPostgresDal.cs | 25 +------------------ 3 files changed, 11 insertions(+), 43 deletions(-) diff --git a/src/DataAccessLayers/BirdsiteLive.DAL.Postgres/DataAccessLayers/Base/PostgresBase.cs b/src/DataAccessLayers/BirdsiteLive.DAL.Postgres/DataAccessLayers/Base/PostgresBase.cs index 1c59387..df2fc79 100644 --- a/src/DataAccessLayers/BirdsiteLive.DAL.Postgres/DataAccessLayers/Base/PostgresBase.cs +++ b/src/DataAccessLayers/BirdsiteLive.DAL.Postgres/DataAccessLayers/Base/PostgresBase.cs @@ -6,19 +6,28 @@ namespace BirdsiteLive.DAL.Postgres.DataAccessLayers.Base public class PostgresBase { protected readonly PostgresSettings _settings; + protected NpgsqlDataSource _dataSource; #region Ctor protected PostgresBase(PostgresSettings settings) { _settings = settings; + _dataSource = NpgsqlDataSource.Create(settings.ConnString); } #endregion + protected NpgsqlDataSource DataSource + { + get + { + return _dataSource; + } + } protected NpgsqlConnection Connection { get { - return new NpgsqlConnection(_settings.ConnString); + return _dataSource.CreateConnection(); } } } diff --git a/src/DataAccessLayers/BirdsiteLive.DAL.Postgres/DataAccessLayers/FollowersPostgresDal.cs b/src/DataAccessLayers/BirdsiteLive.DAL.Postgres/DataAccessLayers/FollowersPostgresDal.cs index db2f9f7..a5bb75d 100644 --- a/src/DataAccessLayers/BirdsiteLive.DAL.Postgres/DataAccessLayers/FollowersPostgresDal.cs +++ b/src/DataAccessLayers/BirdsiteLive.DAL.Postgres/DataAccessLayers/FollowersPostgresDal.cs @@ -32,8 +32,6 @@ namespace BirdsiteLive.DAL.Postgres.DataAccessLayers using (var dbConnection = Connection) { - dbConnection.Open(); - await dbConnection.ExecuteAsync( $"INSERT INTO {_settings.FollowersTableName} (acct,host,inboxRoute,sharedInboxRoute,followings,followingsSyncStatus,actorId) VALUES(@acct,@host,@inboxRoute,@sharedInboxRoute,@followings,CAST(@followingsSyncStatus as json),@actorId)", new { acct, host, inboxRoute, sharedInboxRoute, followings, followingsSyncStatus = serializedDic, actorId }); @@ -46,8 +44,6 @@ namespace BirdsiteLive.DAL.Postgres.DataAccessLayers using (var dbConnection = Connection) { - dbConnection.Open(); - var result = (await dbConnection.QueryAsync(query)).FirstOrDefault(); return result; } @@ -59,8 +55,6 @@ namespace BirdsiteLive.DAL.Postgres.DataAccessLayers using (var dbConnection = Connection) { - dbConnection.Open(); - var result = (await dbConnection.QueryAsync(query)).FirstOrDefault(); return result; } @@ -75,8 +69,6 @@ namespace BirdsiteLive.DAL.Postgres.DataAccessLayers using (var dbConnection = Connection) { - dbConnection.Open(); - var result = (await dbConnection.QueryAsync(query, new { acct, host })).FirstOrDefault(); return Convert(result); } @@ -90,8 +82,6 @@ namespace BirdsiteLive.DAL.Postgres.DataAccessLayers using (var dbConnection = Connection) { - dbConnection.Open(); - var result = await dbConnection.QueryAsync(query, new { id = followedUserId}); return result.Select(Convert).ToArray(); } @@ -103,8 +93,6 @@ namespace BirdsiteLive.DAL.Postgres.DataAccessLayers using (var dbConnection = Connection) { - dbConnection.Open(); - var result = await dbConnection.QueryAsync(query); return result.Select(Convert).ToArray(); } @@ -120,8 +108,6 @@ namespace BirdsiteLive.DAL.Postgres.DataAccessLayers using (var dbConnection = Connection) { - dbConnection.Open(); - await dbConnection.QueryAsync(query, new { follower.Id, follower.Followings, followingsSyncStatus = serializedDic, postingErrorCount = follower.PostingErrorCount }); } } @@ -134,8 +120,6 @@ namespace BirdsiteLive.DAL.Postgres.DataAccessLayers using (var dbConnection = Connection) { - dbConnection.Open(); - await dbConnection.QueryAsync(query, new { id }); } } @@ -152,8 +136,6 @@ namespace BirdsiteLive.DAL.Postgres.DataAccessLayers using (var dbConnection = Connection) { - dbConnection.Open(); - await dbConnection.QueryAsync(query, new { acct, host }); } } diff --git a/src/DataAccessLayers/BirdsiteLive.DAL.Postgres/DataAccessLayers/TwitterUserPostgresDal.cs b/src/DataAccessLayers/BirdsiteLive.DAL.Postgres/DataAccessLayers/TwitterUserPostgresDal.cs index db0b9a4..348b659 100644 --- a/src/DataAccessLayers/BirdsiteLive.DAL.Postgres/DataAccessLayers/TwitterUserPostgresDal.cs +++ b/src/DataAccessLayers/BirdsiteLive.DAL.Postgres/DataAccessLayers/TwitterUserPostgresDal.cs @@ -6,6 +6,7 @@ using BirdsiteLive.DAL.Models; using BirdsiteLive.DAL.Postgres.DataAccessLayers.Base; using BirdsiteLive.DAL.Postgres.Settings; using Dapper; +using Npgsql; namespace BirdsiteLive.DAL.Postgres.DataAccessLayers { @@ -24,8 +25,6 @@ namespace BirdsiteLive.DAL.Postgres.DataAccessLayers using (var dbConnection = Connection) { - dbConnection.Open(); - await dbConnection.ExecuteAsync( $"INSERT INTO {_settings.TwitterUserTableName} (acct,lastTweetPostedId,lastTweetSynchronizedForAllFollowersId) VALUES(@acct,@lastTweetPostedId,@lastTweetSynchronizedForAllFollowersId)", new { acct, lastTweetPostedId, lastTweetSynchronizedForAllFollowersId = lastTweetPostedId }); @@ -40,8 +39,6 @@ namespace BirdsiteLive.DAL.Postgres.DataAccessLayers using (var dbConnection = Connection) { - dbConnection.Open(); - var result = (await dbConnection.QueryAsync(query, new { acct })).FirstOrDefault(); return result; } @@ -53,8 +50,6 @@ namespace BirdsiteLive.DAL.Postgres.DataAccessLayers using (var dbConnection = Connection) { - dbConnection.Open(); - var result = (await dbConnection.QueryAsync(query, new { id })).FirstOrDefault(); return result; } @@ -66,8 +61,6 @@ namespace BirdsiteLive.DAL.Postgres.DataAccessLayers using (var dbConnection = Connection) { - dbConnection.Open(); - var result = (await dbConnection.QueryAsync(query)).FirstOrDefault(); return result; } @@ -79,8 +72,6 @@ namespace BirdsiteLive.DAL.Postgres.DataAccessLayers using (var dbConnection = Connection) { - dbConnection.Open(); - var result = (await dbConnection.QueryAsync(query)).FirstOrDefault(); return result; } @@ -92,8 +83,6 @@ namespace BirdsiteLive.DAL.Postgres.DataAccessLayers using (var dbConnection = Connection) { - dbConnection.Open(); - var result = await dbConnection.QueryAsync(query, new { maxNumber }); return result.ToArray(); } @@ -105,8 +94,6 @@ namespace BirdsiteLive.DAL.Postgres.DataAccessLayers using (var dbConnection = Connection) { - dbConnection.Open(); - var result = await dbConnection.QueryAsync(query, new { maxNumber }); return result.ToArray(); } @@ -118,8 +105,6 @@ namespace BirdsiteLive.DAL.Postgres.DataAccessLayers using (var dbConnection = Connection) { - dbConnection.Open(); - var result = await dbConnection.QueryAsync(query); return result.ToArray(); } @@ -134,8 +119,6 @@ namespace BirdsiteLive.DAL.Postgres.DataAccessLayers using (var dbConnection = Connection) { - dbConnection.Open(); - await dbConnection.QueryAsync(query, new { username, twitterUserId }); } } @@ -150,8 +133,6 @@ namespace BirdsiteLive.DAL.Postgres.DataAccessLayers using (var dbConnection = Connection) { - dbConnection.Open(); - await dbConnection.QueryAsync(query, new { id, lastTweetPostedId, lastTweetSynchronizedForAllFollowersId, fetchingErrorCount, lastSync = lastSync.ToUniversalTime() }); } } @@ -171,8 +152,6 @@ namespace BirdsiteLive.DAL.Postgres.DataAccessLayers using (var dbConnection = Connection) { - dbConnection.Open(); - await dbConnection.QueryAsync(query, new { acct }); } } @@ -185,8 +164,6 @@ namespace BirdsiteLive.DAL.Postgres.DataAccessLayers using (var dbConnection = Connection) { - dbConnection.Open(); - await dbConnection.QueryAsync(query, new { id }); } }