feat : RDB 기능 추가 (라이브러리, base 코드 구현)
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
using System.Data;
|
||||
|
||||
namespace ServerLib.RDB.Database;
|
||||
|
||||
public interface IDbConnectionFactory
|
||||
{
|
||||
public IDbConnection CreateConnection();
|
||||
}
|
||||
22
MMOTestServer/ServerLib/RDB/Handlers/HelperHandler.cs
Normal file
22
MMOTestServer/ServerLib/RDB/Handlers/HelperHandler.cs
Normal file
@@ -0,0 +1,22 @@
|
||||
using System.Text.Json;
|
||||
|
||||
namespace ServerLib.RDB.Handlers;
|
||||
|
||||
public static class HandlerHelper
|
||||
{
|
||||
public static string Success<T>(T data)
|
||||
{
|
||||
return JsonSerializer.Serialize(new Response<T> { Success = true, Data = data });
|
||||
}
|
||||
|
||||
public static string Error(string message)
|
||||
{
|
||||
return JsonSerializer.Serialize(new Response<object> { Success = false, Error = message });
|
||||
}
|
||||
|
||||
public static T? Deserialize<T>(string payload)
|
||||
{
|
||||
try { return JsonSerializer.Deserialize<T>(payload); }
|
||||
catch { return default; }
|
||||
}
|
||||
}
|
||||
8
MMOTestServer/ServerLib/RDB/Handlers/Response.cs
Normal file
8
MMOTestServer/ServerLib/RDB/Handlers/Response.cs
Normal file
@@ -0,0 +1,8 @@
|
||||
namespace ServerLib.RDB.Handlers;
|
||||
|
||||
public class Response<T>
|
||||
{
|
||||
public bool Success { get; init; }
|
||||
public T? Data { get; init; }
|
||||
public string? Error { get; init; }
|
||||
}
|
||||
90
MMOTestServer/ServerLib/RDB/Repositories/ARepository.cs
Normal file
90
MMOTestServer/ServerLib/RDB/Repositories/ARepository.cs
Normal file
@@ -0,0 +1,90 @@
|
||||
using System.Data;
|
||||
using Dapper;
|
||||
using Dapper.Contrib.Extensions;
|
||||
using ServerLib.RDB.Database;
|
||||
|
||||
namespace ServerLib.RDB.Repositories;
|
||||
|
||||
public abstract class ARepository<T> where T : class
|
||||
{
|
||||
private readonly IDbConnectionFactory factory;
|
||||
|
||||
protected ARepository(IDbConnectionFactory factory)
|
||||
{
|
||||
this.factory = factory;
|
||||
}
|
||||
|
||||
protected IDbConnection CreateConnection()
|
||||
{
|
||||
return factory.CreateConnection();
|
||||
}
|
||||
|
||||
// Dapper.Contrib 기본 CRUD
|
||||
public async Task<T?> GetByIdAsync(int id)
|
||||
{
|
||||
IDbConnection conn = CreateConnection();
|
||||
return await conn.GetAsync<T>(id);
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<T>> GetAllAsync()
|
||||
{
|
||||
IDbConnection conn = CreateConnection();
|
||||
return await conn.GetAllAsync<T>();
|
||||
}
|
||||
|
||||
public async Task<long> InsertAsync(T entity)
|
||||
{
|
||||
IDbConnection conn = CreateConnection();
|
||||
return await conn.InsertAsync(entity);
|
||||
}
|
||||
|
||||
public async Task<bool> UpdateAsync(T entity)
|
||||
{
|
||||
IDbConnection conn = CreateConnection();
|
||||
return await conn.UpdateAsync(entity);
|
||||
}
|
||||
|
||||
public async Task<bool> DeleteAsync(T entity)
|
||||
{
|
||||
IDbConnection conn = CreateConnection();
|
||||
return await conn.DeleteAsync(entity);
|
||||
}
|
||||
|
||||
// 커스텀 쿼리 헬퍼 (복잡한 쿼리용)
|
||||
protected async Task<IEnumerable<T>> QueryAsync(string sql, object? param = null)
|
||||
{
|
||||
IDbConnection conn = CreateConnection();
|
||||
return await conn.QueryAsync<T>(sql, param);
|
||||
}
|
||||
|
||||
protected async Task<T?> QueryFirstOrDefaultAsync(string sql, object? param = null)
|
||||
{
|
||||
IDbConnection conn = CreateConnection();
|
||||
return await conn.QueryFirstOrDefaultAsync<T>(sql, param);
|
||||
}
|
||||
|
||||
protected async Task<int> ExecuteAsync(string sql, object? param = null)
|
||||
{
|
||||
IDbConnection conn = CreateConnection();
|
||||
return await conn.ExecuteAsync(sql, param);
|
||||
}
|
||||
|
||||
// 트랜잭션 헬퍼
|
||||
protected async Task<TResult> WithTransactionAsync<TResult>(Func<IDbConnection, IDbTransaction, Task<TResult>> action)
|
||||
{
|
||||
IDbConnection conn = CreateConnection();
|
||||
conn.Open();
|
||||
IDbTransaction tx = conn.BeginTransaction();
|
||||
try
|
||||
{
|
||||
TResult result = await action(conn, tx);
|
||||
tx.Commit();
|
||||
return result;
|
||||
}
|
||||
catch
|
||||
{
|
||||
tx.Rollback();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -8,7 +8,11 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Dapper" Version="2.1.66" />
|
||||
<PackageReference Include="Dapper.Contrib" Version="2.0.78" />
|
||||
<PackageReference Include="LiteNetLib" Version="2.0.2" />
|
||||
<PackageReference Include="MySql.Data" Version="9.6.0" />
|
||||
<PackageReference Include="MySqlConnector" Version="2.5.0" />
|
||||
<PackageReference Include="protobuf-net" Version="3.2.56" />
|
||||
<PackageReference Include="Serilog" Version="4.3.1" />
|
||||
<PackageReference Include="Serilog.Sinks.Console" Version="6.1.1" />
|
||||
|
||||
Reference in New Issue
Block a user