Skip to content

Commit 9540c17

Browse files
Refactor DatabaseConnection
1 parent c109e54 commit 9540c17

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

GitTrends/Database/BaseDatabase.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,20 @@ namespace GitTrends
1111
{
1212
public abstract class BaseDatabase
1313
{
14+
readonly SQLiteAsyncConnection _databaseConnection;
15+
1416
protected BaseDatabase(IFileSystem fileSystem, IAnalyticsService analyticsService, TimeSpan expiresAt)
1517
{
1618
ExpiresAt = expiresAt;
1719
AnalyticsService = analyticsService;
1820

1921
var databasePath = Path.Combine(fileSystem.AppDataDirectory, $"{nameof(GitTrends)}.db3");
20-
DatabaseConnection = new SQLiteAsyncConnection(databasePath, SQLiteOpenFlags.ReadWrite | SQLiteOpenFlags.Create | SQLiteOpenFlags.SharedCache);
22+
_databaseConnection = new SQLiteAsyncConnection(databasePath, SQLiteOpenFlags.ReadWrite | SQLiteOpenFlags.Create | SQLiteOpenFlags.SharedCache);
2123
}
2224

2325
public TimeSpan ExpiresAt { get; }
2426
protected IAnalyticsService AnalyticsService { get; }
2527

26-
SQLiteAsyncConnection DatabaseConnection { get; }
27-
2828
public abstract Task<int> DeleteAllData();
2929

3030
protected static Task<T> AttemptAndRetry<T>(Func<Task<T>> action, int numRetries = 12)
@@ -38,22 +38,22 @@ protected static Task<T> AttemptAndRetry<T>(Func<Task<T>> action, int numRetries
3838

3939
protected async ValueTask<SQLiteAsyncConnection> GetDatabaseConnection<T>()
4040
{
41-
if (!DatabaseConnection.TableMappings.Any(x => x.MappedType == typeof(T)))
41+
if (!_databaseConnection.TableMappings.Any(x => x.MappedType == typeof(T)))
4242
{
43-
await DatabaseConnection.EnableWriteAheadLoggingAsync().ConfigureAwait(false);
43+
await _databaseConnection.EnableWriteAheadLoggingAsync().ConfigureAwait(false);
4444

4545
try
4646
{
47-
await DatabaseConnection.CreateTablesAsync(CreateFlags.None, typeof(T)).ConfigureAwait(false);
47+
await _databaseConnection.CreateTablesAsync(CreateFlags.None, typeof(T)).ConfigureAwait(false);
4848
}
4949
catch (SQLiteException e) when (e.Message.Contains("PRIMARY KEY", StringComparison.OrdinalIgnoreCase))
5050
{
51-
await DatabaseConnection.DropTableAsync(DatabaseConnection.TableMappings.First(x => x.MappedType == typeof(T)));
52-
await DatabaseConnection.CreateTablesAsync(CreateFlags.None, typeof(T)).ConfigureAwait(false);
51+
await _databaseConnection.DropTableAsync(_databaseConnection.TableMappings.First(x => x.MappedType == typeof(T)));
52+
await _databaseConnection.CreateTablesAsync(CreateFlags.None, typeof(T)).ConfigureAwait(false);
5353
}
5454
}
5555

56-
return DatabaseConnection;
56+
return _databaseConnection;
5757
}
5858
}
5959
}

0 commit comments

Comments
 (0)