Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changing IRequest to just be a normal Task instead of Task<Unit> #835

Merged
merged 1 commit into from
Feb 9, 2023

Conversation

jbogard
Copy link
Owner

@jbogard jbogard commented Feb 9, 2023

This includes the following API changes to change IRequest to use Task instead of Task<Unit> to make void handlers easier:

- public interface IRequest : IRequest<Unit> { }
+ public interface IRequest : IBaseRequest { }
- public interface IRequestHandler<in TRequest> : IRequestHandler<TRequest, Unit>
-     where TRequest : IRequest<Unit>
+ public interface IRequestHandler<in TRequest>
+    where TRequest : IRequest {
+     Task Handle(TRequest request, CancellationToken cancellationToken);
+ }
public interface IMediator {
+    Task Send<TRequest>(TRequest request, CancellationToken cancellationToken = default)
+        where TRequest : IRequest;
}

Handlers that implement IRequestHandler<IRequest> need to change their return types to Task from Task<Unit>:

public class VoidRequest : IRequest { }

public class VoidRequestHandler : IRequestHandler<VoidRequest>
{
-    public Task<Unit> Handle(VoidRequest request, CancellationToken cancellationToken)
+    public Task Handle(VoidRequest request, CancellationToken cancellationToken)
    {
-        return Unit.Task;
+        return Task.CompletedTask;
    }
}

Async version:

public class VoidRequest : IRequest { }

public class VoidRequestHandler : IRequestHandler<VoidRequest>
{
-    public async Task<Unit> Handle(VoidRequest request, CancellationToken cancellationToken)
+    public async Task Handle(VoidRequest request, CancellationToken cancellationToken)
    {
          await SomeThing();
-        return Unit.Value;
+        return;
    }
}

@jbogard jbogard merged commit 2bf8b6b into master Feb 9, 2023
@jbogard jbogard deleted the unit-less branch February 9, 2023 16:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant