Changes of DB implementation

This commit is contained in:
Serraniel 2022-11-18 19:51:33 +01:00
parent 91eb7fea58
commit 2a239dba58
Signed by: Serraniel
GPG key ID: 3690B4E7364525D3
5 changed files with 41 additions and 36 deletions

View file

@ -4,18 +4,6 @@
<name>SweetLib.Storage</name>
</assembly>
<members>
<member name="T:SweetLib.Storage.Database.IConnectionProvider`1">
<summary>
Provider for database connection.
</summary>
<typeparam name="T">Specific type of the database connection. Must inherit <see cref="T:System.Data.Common.DbConnection" />.</typeparam>
</member>
<member name="M:SweetLib.Storage.Database.IConnectionProvider`1.GetConnection">
<summary>
Gets an instance of a database connection.
</summary>
<returns></returns>
</member>
<member name="T:SweetLib.Storage.Database.IDatabaseObject`1">
<summary>
Object to store in database.
@ -48,6 +36,17 @@
</summary>
<returns></returns>
</member>
<member name="T:SweetLib.Storage.IConnectionProvider`1">
<summary>
Provider for database connection.
</summary>
</member>
<member name="M:SweetLib.Storage.IConnectionProvider`1.GetConnection">
<summary>
Gets an instance of a database connection.
</summary>
<returns></returns>
</member>
<member name="T:SweetLib.Storage.IIdentified`1">
<summary>
Interface for identified objects.

View file

@ -2,7 +2,6 @@
using System.Data.Common;
using System.Linq;
using System.Threading.Tasks;
using MySql.Data.MySqlClient;
using SweetLib.Storage.Database;
using SweetLib.Storage.MySql.Helper;
@ -10,7 +9,7 @@ namespace SweetLib.Storage.MySql;
public abstract class CustomMySqlObject<T> : IDatabaseObject<T>
{
protected CustomMySqlObject(IConnectionProvider<MySqlConnection> connectionProvider,
protected CustomMySqlObject(IDatabaseConnectionProvider connectionProvider,
IIdGenerator<T> idGenerator)
{
ConnectionProvider = connectionProvider;
@ -18,7 +17,7 @@ public abstract class CustomMySqlObject<T> : IDatabaseObject<T>
Id = UnidentifiedId();
}
protected IConnectionProvider<MySqlConnection> ConnectionProvider { get; }
protected IDatabaseConnectionProvider ConnectionProvider { get; }
protected IIdGenerator<T> IdGenerator { get; }
public T Id { get; set; }
@ -50,7 +49,7 @@ public abstract class CustomMySqlObject<T> : IDatabaseObject<T>
}
finally
{
await connection.CloseAsync();
connection.Close();
}
}
@ -59,7 +58,7 @@ public abstract class CustomMySqlObject<T> : IDatabaseObject<T>
return Id.Equals(UnidentifiedId());
}
public IEnumerable<string> AssignFieldNames()
public virtual IEnumerable<string> AssignFieldNames()
{
var result = new List<string>();
result.Add(IdFieldName());
@ -67,15 +66,16 @@ public abstract class CustomMySqlObject<T> : IDatabaseObject<T>
return result;
}
public async Task AssignToAsync(DbParameterCollection parameters)
public virtual async Task AssignToAsync(DbParameterCollection parameters)
{
if (IsNew()) Id = await IdGenerator.GenerateNewIdAsync();
parameters.AddMySqlParameterWithValue("@Id", Id);
}
public async Task AssignFromAsync(DbDataReader reader)
public virtual async Task AssignFromAsync(DbDataReader reader)
{
await reader.ReadAsync();
Id = (T)reader["Id"];
}
@ -89,7 +89,7 @@ public abstract class CustomMySqlObject<T> : IDatabaseObject<T>
{
var command = connection.CreateCommand();
command.CommandText = $"SELECT * FROM {TableName()} WHERE Id=@id";
command.Parameters.AddWithValue("@id", identifier);
command.Parameters.AddMySqlParameterWithValue("@id", identifier);
var reader = await command.ExecuteReaderAsync();
if (!reader.HasRows)
@ -100,7 +100,7 @@ public abstract class CustomMySqlObject<T> : IDatabaseObject<T>
}
finally
{
await connection.CloseAsync();
connection.Close();
}
}

View file

@ -1,8 +1,8 @@
using System.Data.Common;
using MySql.Data.MySqlClient;
namespace SweetLib.Storage.MySql.Helper
{
namespace SweetLib.Storage.MySql.Helper;
public static class MySqlDbParameterExtensions
{
public static MySqlParameter AddMySqlParameterWithValue(this DbParameterCollection parameterCollection,
@ -14,4 +14,3 @@ namespace SweetLib.Storage.MySql.Helper
return param;
}
}
}

View file

@ -0,0 +1,8 @@
using System.Data.Common;
namespace SweetLib.Storage.Database
{
public interface IDatabaseConnectionProvider : IConnectionProvider<DbConnection>
{
}
}

View file

@ -1,12 +1,11 @@
using System.Data.Common;
using System.Data;
namespace SweetLib.Storage.Database
namespace SweetLib.Storage
{
/// <summary>
/// Provider for database connection.
/// </summary>
/// <typeparam name="T">Specific type of the database connection. Must inherit <see cref="DbConnection" />.</typeparam>
public interface IConnectionProvider<T> where T : DbConnection
public interface IConnectionProvider<out T> where T : IDbConnection
{
/// <summary>
/// Gets an instance of a database connection.