111,093
社区成员




/// <summary>
/// base repository
/// </summary>
public class BaseRepository
{
#region Constants
public static readonly int DefaultSqlCommandTimeout = 10;
public static readonly int LongSqlCommandTimeout = 15;
protected static readonly int TableAccountTradingStatementsSplitCount = 32;
#endregion
#region private fields
private readonly string _connectionName;
private readonly int _commandTimeout = 2;
const int _sharding = 32;
#endregion
#region Ctor
protected BaseRepository(string connectionName= "Default")
{
_connectionName = connectionName;
}
#endregion
/// <summary>
/// 异步的sqlconnection管理
/// 参考 http://www.joesauve.com/async-dapper-and-async-sql-connection-management/
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="getData"></param>
/// <returns></returns>
protected async Task<T> WithConnection<T>(Func<IDbConnection, Task<T>> getData)
{
try
{
var connectionString = ConfigurationManager.ConnectionStrings[this._connectionName].ConnectionString;
using (var connection = new MySqlConnection(connectionString))
{
try
{
await connection.OpenAsync(); // Asynchronously open a connection to the database
return await getData(connection);
// Asynchronously execute getData, which has been passed in as a Func<IDBConnection, Task<T>>
}
catch (Exception ex)
{
Console.WriteLine(ex);
throw;
}
}
}
catch (TimeoutException ex)
{
throw new Exception($"{GetType().FullName}.WithConnection() experienced a SQL timeout", ex);
}
catch (SqlException ex)
{
throw new Exception($"{GetType().FullName}.WithConnection() experienced a SQL exception", ex);
}
}
protected async Task<int> ExecuteAsync(string sql, DynamicParameters p)
{
return await WithConnection(
async c =>
{
var ret = await c.ExecuteAsync(sql, param: p, commandTimeout: _commandTimeout);
return ret;
});
}
protected async Task<IEnumerable<T>> QueryAsync<T>(string sql, DynamicParameters p)
{
return await WithConnection(
async c =>
{
var ret = await c.QueryAsync<T>(sql, param: p, commandTimeout: _commandTimeout);
return ret;
});
}
}
public class NotificationRepository : BaseRepository
{
public async Task<bool> Add(NotificationModel model, bool trans = false)
{
return await WithConnection(
async c =>
{
if (trans)
{
((DbConnection)c).EnlistTransaction(Transaction.Current);
}
var sql = @"INSERT INTO `asyncnotifications`
(`JobFullName`,`JobData`,`ExecutedNumber`,`ExecutedUtcTime`,`NotifyStatus`,`CreationTime`)
VALUES
(@JobFullName,@JobData,@ExecutedNumber,@ExecutedUtcTime,@NotifyStatus,@CreationTime);
SELECT CONVERT(LAST_INSERT_ID(), unsigned integer) AS ID";
var id = await c.ExecuteScalarAsync<long>(sql, model, commandTimeout: DefaultSqlCommandTimeout);
model.Id = id;
return id > 0;
});
}
public async Task<bool> Update(NotificationModel model)
{
return await WithConnection(
async c =>
{//`JobFullName` = @JobFullName,`JobData`= @JobData,
var sql = @"update `asyncnotifications`
set `ExecutedNumber`=@ExecutedNumber,
`ExecutedUtcTime`=@ExecutedUtcTime,`NotifyStatus`= @NotifyStatus,`LastModificationTime`= @LastModificationTime
where `Id`=@id";
return await c.ExecuteAsync(sql, model, commandTimeout: DefaultSqlCommandTimeout) > 0;
});
}
public async Task<IEnumerable<NotificationModel>> FindAllNotFinished()
{
return await this.WithConnection(async c =>
{
string sql = @"select `Id`,`JobFullName`,`JobData`,`ExecutedNumber`,`ExecutedUtcTime`,`NotifyStatus`,`CreationTime`,`LastModificationTime`
from `asyncnotifications`
where `NotifyStatus`=0";
return await c.QueryAsync<NotificationModel>(sql).ConfigureAwait(false);
});
}
}