From c619aff920efe1a89019b3da12ac078c2987c95f Mon Sep 17 00:00:00 2001 From: Savorboard Date: Thu, 19 Oct 2017 18:10:40 +0800 Subject: [PATCH] refactor. --- .../Abstractions/CapPublisherBase.cs | 10 +++++--- .../Abstractions/IContentSerializer.cs | 11 +++++++-- src/DotNetCore.CAP/CAP.Builder.cs | 10 ++++++++ .../CAP.ServiceCollectionExtensions.cs | 1 + .../IQueueExecutor.Publish.Base.cs | 2 +- .../IQueueExecutor.Subscribe.cs | 2 +- src/DotNetCore.CAP/LoggerExtensions.cs | 6 ++--- src/DotNetCore.CAP/Models/CapMessageDto.cs | 23 ++++++++++++++----- 8 files changed, 49 insertions(+), 16 deletions(-) diff --git a/src/DotNetCore.CAP/Abstractions/CapPublisherBase.cs b/src/DotNetCore.CAP/Abstractions/CapPublisherBase.cs index 66930d1..70f1d54 100644 --- a/src/DotNetCore.CAP/Abstractions/CapPublisherBase.cs +++ b/src/DotNetCore.CAP/Abstractions/CapPublisherBase.cs @@ -68,13 +68,17 @@ namespace DotNetCore.CAP.Abstractions protected virtual string Serialize(T obj, string callbackName = null) { - var serializer = (IContentSerializer) ServiceProvider.GetService(typeof(IContentSerializer)); + var serializer = (IContentSerializer)ServiceProvider.GetService(typeof(IContentSerializer)); + var packer = (IMessagePacker)ServiceProvider.GetService(typeof(IMessagePacker)); - var message = new CapMessageDto(obj) + var content = serializer.Serialize(obj); + + var message = new CapMessageDto(content) { CallbackName = callbackName }; - return serializer.Serialize(message); + + return packer.Pack(message); } #region private methods diff --git a/src/DotNetCore.CAP/Abstractions/IContentSerializer.cs b/src/DotNetCore.CAP/Abstractions/IContentSerializer.cs index 952e6bf..1416fea 100644 --- a/src/DotNetCore.CAP/Abstractions/IContentSerializer.cs +++ b/src/DotNetCore.CAP/Abstractions/IContentSerializer.cs @@ -4,8 +4,15 @@ namespace DotNetCore.CAP.Abstractions { public interface IContentSerializer { - string Serialize(T obj) where T : CapMessageDto, new(); + string Serialize(T obj); - T DeSerialize(string content) where T : CapMessageDto, new(); + T DeSerialize(string content); + } + + public interface IMessagePacker + { + string Pack(CapMessage obj); + + CapMessage UnPack(string packingMessage); } } \ No newline at end of file diff --git a/src/DotNetCore.CAP/CAP.Builder.cs b/src/DotNetCore.CAP/CAP.Builder.cs index 910256c..e59270d 100644 --- a/src/DotNetCore.CAP/CAP.Builder.cs +++ b/src/DotNetCore.CAP/CAP.Builder.cs @@ -60,6 +60,16 @@ namespace DotNetCore.CAP return AddSingleton(typeof(IContentSerializer), typeof(T)); } + /// + /// Add a custom message wapper + /// + /// The type of the service. + public CapBuilder AddMessagePacker() + where T : class, IMessagePacker + { + return AddSingleton(typeof(IMessagePacker), typeof(T)); + } + /// /// Adds a scoped service of the type specified in serviceType with an implementation /// diff --git a/src/DotNetCore.CAP/CAP.ServiceCollectionExtensions.cs b/src/DotNetCore.CAP/CAP.ServiceCollectionExtensions.cs index 1a09fb4..826f3ba 100644 --- a/src/DotNetCore.CAP/CAP.ServiceCollectionExtensions.cs +++ b/src/DotNetCore.CAP/CAP.ServiceCollectionExtensions.cs @@ -34,6 +34,7 @@ namespace Microsoft.Extensions.DependencyInjection //Serializer and model binder services.TryAddSingleton(); + services.TryAddSingleton(); services.TryAddSingleton(); services.TryAddSingleton(); services.TryAddSingleton(); diff --git a/src/DotNetCore.CAP/IQueueExecutor.Publish.Base.cs b/src/DotNetCore.CAP/IQueueExecutor.Publish.Base.cs index b78e80c..194e595 100644 --- a/src/DotNetCore.CAP/IQueueExecutor.Publish.Base.cs +++ b/src/DotNetCore.CAP/IQueueExecutor.Publish.Base.cs @@ -70,7 +70,7 @@ namespace DotNetCore.CAP catch (Exception ex) { fetched.Requeue(); - _logger.ExceptionOccuredWhileExecutingJob(message?.Name, ex); + _logger.ExceptionOccuredWhileExecuting(message?.Name, ex); return OperateResult.Failed(ex); } } diff --git a/src/DotNetCore.CAP/IQueueExecutor.Subscribe.cs b/src/DotNetCore.CAP/IQueueExecutor.Subscribe.cs index f32a973..e6ff3a4 100644 --- a/src/DotNetCore.CAP/IQueueExecutor.Subscribe.cs +++ b/src/DotNetCore.CAP/IQueueExecutor.Subscribe.cs @@ -68,7 +68,7 @@ namespace DotNetCore.CAP } catch (Exception ex) { - _logger.ExceptionOccuredWhileExecutingJob(message?.Name, ex); + _logger.ExceptionOccuredWhileExecuting(message?.Name, ex); fetched.Requeue(); diff --git a/src/DotNetCore.CAP/LoggerExtensions.cs b/src/DotNetCore.CAP/LoggerExtensions.cs index 4ebe34e..8104702 100644 --- a/src/DotNetCore.CAP/LoggerExtensions.cs +++ b/src/DotNetCore.CAP/LoggerExtensions.cs @@ -93,7 +93,7 @@ namespace DotNetCore.CAP _exceptionOccuredWhileExecutingJob = LoggerMessage.Define( LogLevel.Error, 6, - "An exception occured while trying to execute a job: '{JobId}'. " + + "An exception occured while trying to execute a message: '{MessageId}'. " + "Requeuing for another retry."); } @@ -157,9 +157,9 @@ namespace DotNetCore.CAP _expectedOperationCanceledException(logger, ex.Message, ex); } - public static void ExceptionOccuredWhileExecutingJob(this ILogger logger, string jobId, Exception ex) + public static void ExceptionOccuredWhileExecuting(this ILogger logger, string messageId, Exception ex) { - _exceptionOccuredWhileExecutingJob(logger, jobId, ex); + _exceptionOccuredWhileExecutingJob(logger, messageId, ex); } public static void ModelBinderFormattingException(this ILogger logger, string methodName, string parameterName, diff --git a/src/DotNetCore.CAP/Models/CapMessageDto.cs b/src/DotNetCore.CAP/Models/CapMessageDto.cs index 83b448f..0b3c699 100644 --- a/src/DotNetCore.CAP/Models/CapMessageDto.cs +++ b/src/DotNetCore.CAP/Models/CapMessageDto.cs @@ -3,7 +3,18 @@ using DotNetCore.CAP.Infrastructure; namespace DotNetCore.CAP.Models { - public class CapMessageDto + public abstract class CapMessage + { + public virtual string Id { get; set; } + + public virtual DateTime Timestamp { get; set; } + + public virtual string Content { get; set; } + + public virtual string CallbackName { get; set; } + } + + public sealed class CapMessageDto: CapMessage { public CapMessageDto() { @@ -11,17 +22,17 @@ namespace DotNetCore.CAP.Models Timestamp = DateTime.Now; } - public CapMessageDto(object content) : this() + public CapMessageDto(string content) : this() { Content = content; } - public virtual string Id { get; set; } + public override string Id { get; set; } - public virtual DateTime Timestamp { get; set; } + public override DateTime Timestamp { get; set; } - public virtual object Content { get; set; } + public override string Content { get; set; } - public virtual string CallbackName { get; set; } + public override string CallbackName { get; set; } } } \ No newline at end of file