顺序命令处理程序

时间:2020-06-29 19:32:10

标签: c# .net

我创建了通用处理程序,该处理程序按顺序处理命令:

public abstract class SequentialRequestHandler<TRequest, TResponse> : IRequestHandler<TRequest, TResponse>
        where TRequest : IRequest<TResponse>
        where TResponse : class
    {
        private static class TypeLock<T>
        {
            public static readonly object Locker = new object();
        }

        protected abstract Task<TResponse> MonopolyHandle(TRequest request, CancellationToken cancellationToken);

        public Task<TResponse> Handle(TRequest request, CancellationToken cancellationToken)
        {
            lock (TypeLock<TRequest>.Locker)
            {
                return MonopolyHandle(request, cancellationToken);
            }
        }
    }

使用此通用处理程序:

 public class EditCommandHandler : SequentialRequestHandler<EditCommand, ApiResult<Guid>>
    {
        protected override async Task<ApiResult<Guid>> MonopolyHandle(EditCommand request, CancellationToken cancellationToken)
        {
            //handling functionality
        }
    }

public class SaveCommandHandler : SequentialRequestHandler<SaveCommand, ApiResult<Guid>>
    {
        protected override async Task<ApiResult<Guid>> MonopolyHandle(SaveCommand request, CancellationToken cancellationToken)
        {
            //handling functionality
        }
    }

如何更改SequentialRequestHandler,让他按顺序处理命令而不使用lock

0 个答案:

没有答案