From df677948a5616e1bfa08c3831f20b83ff7b3075f Mon Sep 17 00:00:00 2001 From: Savorboard Date: Tue, 12 Nov 2019 17:31:37 +0800 Subject: [PATCH] Code refactoring --- .../IMonitoringApi.PostgreSql.cs | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/src/DotNetCore.CAP.PostgreSql/IMonitoringApi.PostgreSql.cs b/src/DotNetCore.CAP.PostgreSql/IMonitoringApi.PostgreSql.cs index 8fde3f5..b026034 100644 --- a/src/DotNetCore.CAP.PostgreSql/IMonitoringApi.PostgreSql.cs +++ b/src/DotNetCore.CAP.PostgreSql/IMonitoringApi.PostgreSql.cs @@ -30,20 +30,16 @@ namespace DotNetCore.CAP.PostgreSql var sql = $"SELECT * FROM \"{_options.Value.Schema}\".\"published\" WHERE \"Id\"={id} FOR UPDATE SKIP LOCKED"; - using (var connection = new NpgsqlConnection(_options.Value.ConnectionString)) - { - return await connection.QueryFirstOrDefaultAsync(sql); - } + await using var connection = new NpgsqlConnection(_options.Value.ConnectionString); + return await connection.QueryFirstOrDefaultAsync(sql); } public async Task GetReceivedMessageAsync(long id) { var sql = $"SELECT * FROM \"{_options.Value.Schema}\".\"received\" WHERE \"Id\"={id} FOR UPDATE SKIP LOCKED"; - using (var connection = new NpgsqlConnection(_options.Value.ConnectionString)) - { - return await connection.QueryFirstOrDefaultAsync(sql); - } + await using var connection = new NpgsqlConnection(_options.Value.ConnectionString); + return await connection.QueryFirstOrDefaultAsync(sql); } public StatisticsDto GetStatistics() @@ -138,7 +134,7 @@ select count(""Id"") from ""{0}"".""received"" where ""StatusName"" = N'Failed' var sqlQuery = $"select count(\"Id\") from \"{_options.Value.Schema}\".\"{tableName}\" where Lower(\"StatusName\") = Lower(@state)"; - var count = connection.ExecuteScalar(sqlQuery, new {state = statusName}); + var count = connection.ExecuteScalar(sqlQuery, new { state = statusName }); return count; } @@ -180,7 +176,7 @@ with aggr as ( ) select ""Key"",""Count"" from aggr where ""Key""= Any(@keys);"; - var valuesMap = connection.Query(sqlQuery, new {keys = keyMaps.Keys.ToList(), statusName}) + var valuesMap = connection.Query(sqlQuery, new { keys = keyMaps.Keys.ToList(), statusName }) .ToList() .ToDictionary(x => x.Key, x => x.Count);