From 2e2b45f83370ea0282f24be63b44b85d2e0319d8 Mon Sep 17 00:00:00 2001 From: Savorboard Date: Mon, 27 Dec 2021 13:53:29 +0800 Subject: [PATCH] Enable #nullable for mysql storage. --- .../IMonitoringApi.InMemory.cs | 8 ++++---- src/DotNetCore.CAP.MySql/CAP.EFOptions.cs | 2 +- src/DotNetCore.CAP.MySql/CAP.MySqlOptions.cs | 2 +- src/DotNetCore.CAP.MySql/DotNetCore.CAP.MySql.csproj | 8 +------- src/DotNetCore.CAP.MySql/IDataStorage.MySql.cs | 10 +++++----- src/DotNetCore.CAP.MySql/IDbConnection.Extensions.cs | 10 +++++----- src/DotNetCore.CAP.MySql/IDbContextTransaction.CAP.cs | 4 ++-- src/DotNetCore.CAP.MySql/IMonitoringApi.MySql.cs | 8 ++++---- src/DotNetCore.CAP/Monitoring/IMonitoringApi.cs | 4 ++-- 9 files changed, 25 insertions(+), 31 deletions(-) diff --git a/src/DotNetCore.CAP.InMemoryStorage/IMonitoringApi.InMemory.cs b/src/DotNetCore.CAP.InMemoryStorage/IMonitoringApi.InMemory.cs index eae2cbd..2dcb4cb 100644 --- a/src/DotNetCore.CAP.InMemoryStorage/IMonitoringApi.InMemory.cs +++ b/src/DotNetCore.CAP.InMemoryStorage/IMonitoringApi.InMemory.cs @@ -15,14 +15,14 @@ namespace DotNetCore.CAP.InMemoryStorage { internal class InMemoryMonitoringApi : IMonitoringApi { - public Task GetPublishedMessageAsync(long id) + public Task GetPublishedMessageAsync(long id) { - return Task.FromResult((MediumMessage)InMemoryStorage.PublishedMessages.Values.FirstOrDefault(x => x.DbId == id.ToString(CultureInfo.InvariantCulture))); + return Task.FromResult(InMemoryStorage.PublishedMessages.Values.FirstOrDefault(x => x.DbId == id.ToString(CultureInfo.InvariantCulture))); } - public Task GetReceivedMessageAsync(long id) + public Task GetReceivedMessageAsync(long id) { - return Task.FromResult((MediumMessage)InMemoryStorage.ReceivedMessages.Values.FirstOrDefault(x => x.DbId == id.ToString(CultureInfo.InvariantCulture))); + return Task.FromResult(InMemoryStorage.ReceivedMessages.Values.FirstOrDefault(x => x.DbId == id.ToString(CultureInfo.InvariantCulture))); } public StatisticsDto GetStatistics() diff --git a/src/DotNetCore.CAP.MySql/CAP.EFOptions.cs b/src/DotNetCore.CAP.MySql/CAP.EFOptions.cs index e770eb1..636dea7 100644 --- a/src/DotNetCore.CAP.MySql/CAP.EFOptions.cs +++ b/src/DotNetCore.CAP.MySql/CAP.EFOptions.cs @@ -18,7 +18,7 @@ namespace DotNetCore.CAP /// /// EF db context type. /// - internal Type DbContextType { get; set; } + internal Type? DbContextType { get; set; } /// /// Data version diff --git a/src/DotNetCore.CAP.MySql/CAP.MySqlOptions.cs b/src/DotNetCore.CAP.MySql/CAP.MySqlOptions.cs index ea1f503..a6f78f9 100644 --- a/src/DotNetCore.CAP.MySql/CAP.MySqlOptions.cs +++ b/src/DotNetCore.CAP.MySql/CAP.MySqlOptions.cs @@ -13,7 +13,7 @@ namespace DotNetCore.CAP /// /// Gets or sets the database's connection string that will be used to store database entities. /// - public string ConnectionString { get; set; } + public string ConnectionString { get; set; } = default!; } internal class ConfigureMySqlOptions : IConfigureOptions diff --git a/src/DotNetCore.CAP.MySql/DotNetCore.CAP.MySql.csproj b/src/DotNetCore.CAP.MySql/DotNetCore.CAP.MySql.csproj index 59d3685..7d0e8bf 100644 --- a/src/DotNetCore.CAP.MySql/DotNetCore.CAP.MySql.csproj +++ b/src/DotNetCore.CAP.MySql/DotNetCore.CAP.MySql.csproj @@ -2,15 +2,9 @@ net6.0;netstandard2.1 - DotNetCore.CAP.MySql + enable $(PackageTags);MySQL - - - bin\$(Configuration)\netstandard2.1\DotNetCore.CAP.MySql.xml - 1701;1702;1705;CS1591 - - diff --git a/src/DotNetCore.CAP.MySql/IDataStorage.MySql.cs b/src/DotNetCore.CAP.MySql/IDataStorage.MySql.cs index 72a0074..b2866f9 100644 --- a/src/DotNetCore.CAP.MySql/IDataStorage.MySql.cs +++ b/src/DotNetCore.CAP.MySql/IDataStorage.MySql.cs @@ -46,7 +46,7 @@ namespace DotNetCore.CAP.MySql public async Task ChangeReceiveStateAsync(MediumMessage message, StatusName state) => await ChangeMessageStateAsync(_recName, message, state); - public MediumMessage StoreMessage(string name, Message content, object dbTransaction = null) + public MediumMessage StoreMessage(string name, Message content, object? dbTransaction = null) { var sql = $"INSERT INTO `{_pubName}`(`Id`,`Version`,`Name`,`Content`,`Retries`,`Added`,`ExpiresAt`,`StatusName`)" + $" VALUES(@Id,'{_options.Value.Version}',@Name,@Content,@Retries,@Added,@ExpiresAt,@StatusName);"; @@ -68,7 +68,7 @@ namespace DotNetCore.CAP.MySql new MySqlParameter("@Content", message.Content), new MySqlParameter("@Retries", message.Retries), new MySqlParameter("@Added", message.Added), - new MySqlParameter("@ExpiresAt", message.ExpiresAt.HasValue ? (object)message.ExpiresAt.Value : DBNull.Value), + new MySqlParameter("@ExpiresAt", message.ExpiresAt.HasValue ? message.ExpiresAt.Value : DBNull.Value), new MySqlParameter("@StatusName", nameof(StatusName.Scheduled)), }; @@ -85,7 +85,7 @@ namespace DotNetCore.CAP.MySql dbTrans = dbContextTrans.GetDbTransaction(); } - var conn = dbTrans?.Connection; + var conn = dbTrans!.Connection!; conn.ExecuteNonQuery(sql, dbTrans, sqlParams); } @@ -128,7 +128,7 @@ namespace DotNetCore.CAP.MySql new MySqlParameter("@Content", _serializer.Serialize(mdMessage.Origin)), new MySqlParameter("@Retries", mdMessage.Retries), new MySqlParameter("@Added", mdMessage.Added), - new MySqlParameter("@ExpiresAt", mdMessage.ExpiresAt.HasValue ? (object) mdMessage.ExpiresAt.Value : DBNull.Value), + new MySqlParameter("@ExpiresAt", mdMessage.ExpiresAt.HasValue ? mdMessage.ExpiresAt.Value : DBNull.Value), new MySqlParameter("@StatusName", nameof(StatusName.Scheduled)) }; @@ -198,7 +198,7 @@ namespace DotNetCore.CAP.MySql messages.Add(new MediumMessage { DbId = reader.GetInt64(0).ToString(), - Origin = _serializer.Deserialize(reader.GetString(1)), + Origin = _serializer.Deserialize(reader.GetString(1))!, Retries = reader.GetInt32(2), Added = reader.GetDateTime(3) }); diff --git a/src/DotNetCore.CAP.MySql/IDbConnection.Extensions.cs b/src/DotNetCore.CAP.MySql/IDbConnection.Extensions.cs index f203025..3dcb0bf 100644 --- a/src/DotNetCore.CAP.MySql/IDbConnection.Extensions.cs +++ b/src/DotNetCore.CAP.MySql/IDbConnection.Extensions.cs @@ -9,7 +9,7 @@ namespace DotNetCore.CAP.MySql { internal static class DbConnectionExtensions { - public static int ExecuteNonQuery(this IDbConnection connection, string sql, IDbTransaction transaction = null, + public static int ExecuteNonQuery(this IDbConnection connection, string sql, IDbTransaction? transaction = null, params object[] sqlParams) { if (connection.State == ConnectionState.Closed) @@ -33,7 +33,7 @@ namespace DotNetCore.CAP.MySql return command.ExecuteNonQuery(); } - public static T ExecuteReader(this IDbConnection connection, string sql, Func readerFunc, + public static T ExecuteReader(this IDbConnection connection, string sql, Func? readerFunc, params object[] sqlParams) { if (connection.State == ConnectionState.Closed) @@ -51,7 +51,7 @@ namespace DotNetCore.CAP.MySql var reader = command.ExecuteReader(); - T result = default; + T result = default!; if (readerFunc != null) { result = readerFunc(reader); @@ -77,14 +77,14 @@ namespace DotNetCore.CAP.MySql var objValue = command.ExecuteScalar(); - T result = default; + T result = default!; if (objValue != null) { var returnType = typeof(T); var converter = TypeDescriptor.GetConverter(returnType); if (converter.CanConvertFrom(objValue.GetType())) { - result = (T)converter.ConvertFrom(objValue); + result = (T)converter.ConvertFrom(objValue)!; } else { diff --git a/src/DotNetCore.CAP.MySql/IDbContextTransaction.CAP.cs b/src/DotNetCore.CAP.MySql/IDbContextTransaction.CAP.cs index 2f89d47..015edf1 100644 --- a/src/DotNetCore.CAP.MySql/IDbContextTransaction.CAP.cs +++ b/src/DotNetCore.CAP.MySql/IDbContextTransaction.CAP.cs @@ -19,7 +19,7 @@ namespace Microsoft.EntityFrameworkCore.Storage public CapEFDbTransaction(ICapTransaction transaction) { _transaction = transaction; - var dbContextTransaction = (IDbContextTransaction)_transaction.DbTransaction; + var dbContextTransaction = (IDbContextTransaction)_transaction.DbTransaction!; TransactionId = dbContextTransaction.TransactionId; } @@ -60,7 +60,7 @@ namespace Microsoft.EntityFrameworkCore.Storage { get { - var dbContextTransaction = (IDbContextTransaction)_transaction.DbTransaction; + var dbContextTransaction = (IDbContextTransaction)_transaction.DbTransaction!; return dbContextTransaction.GetDbTransaction(); } } diff --git a/src/DotNetCore.CAP.MySql/IMonitoringApi.MySql.cs b/src/DotNetCore.CAP.MySql/IMonitoringApi.MySql.cs index 5864bdb..aaea317 100644 --- a/src/DotNetCore.CAP.MySql/IMonitoringApi.MySql.cs +++ b/src/DotNetCore.CAP.MySql/IMonitoringApi.MySql.cs @@ -247,18 +247,18 @@ WHERE `Key` >= @minKey return result; } - public async Task GetPublishedMessageAsync(long id) => await GetMessageAsync(_pubName, id); + public async Task GetPublishedMessageAsync(long id) => await GetMessageAsync(_pubName, id); - public async Task GetReceivedMessageAsync(long id) => await GetMessageAsync(_recName, id); + public async Task GetReceivedMessageAsync(long id) => await GetMessageAsync(_recName, id); - private async Task GetMessageAsync(string tableName, long id) + private async Task GetMessageAsync(string tableName, long id) { var sql = $@"SELECT `Id` as DbId, `Content`,`Added`,`ExpiresAt`,`Retries` FROM `{tableName}` WHERE Id={id};"; await using var connection = new MySqlConnection(_options.ConnectionString); var mediumMessage = connection.ExecuteReader(sql, reader => { - MediumMessage message = null; + MediumMessage? message = null; while (reader.Read()) { diff --git a/src/DotNetCore.CAP/Monitoring/IMonitoringApi.cs b/src/DotNetCore.CAP/Monitoring/IMonitoringApi.cs index 1c2343c..eb0b020 100644 --- a/src/DotNetCore.CAP/Monitoring/IMonitoringApi.cs +++ b/src/DotNetCore.CAP/Monitoring/IMonitoringApi.cs @@ -11,9 +11,9 @@ namespace DotNetCore.CAP.Monitoring { public interface IMonitoringApi { - Task GetPublishedMessageAsync(long id); + Task GetPublishedMessageAsync(long id); - Task GetReceivedMessageAsync(long id); + Task GetReceivedMessageAsync(long id); StatisticsDto GetStatistics();