-
Notifications
You must be signed in to change notification settings - Fork 161
Open
Description
Im new guy of using this lib, i follow example but get error ! Please help me
I create my ICommand
using Mediator;
using taskcoper_be.contract.abstractions.shared;
namespace taskcoper_be.contract.abstractions.messages
{
/// <summary>
/// Marker for commands with no explicit response payload.
/// Wraps Mediator IRequest to always return a <see cref="Result"/>.
/// </summary>
public interface ICommand : IRequest<Result>
{
}
/// <summary>
/// Marker for commands with a response payload.
/// Ensures all commands return a standardized <see cref="Result{T}"/>.
/// </summary>
public interface ICommand<TResponse> : IRequest<Result<TResponse>>
{
}
}
my command handler
using Mediator;
using taskcoper_be.contract.abstractions.shared;
namespace taskcoper_be.contract.abstractions.messages
{
/// <summary>
/// Handles commands that do not return a payload.
/// </summary>
/// <typeparam name="TCommand">Command type.</typeparam>
public interface ICommandHandler<in TCommand> : IRequestHandler<TCommand, Result>
where TCommand : ICommand
{
}
/// <summary>
/// Handles commands that return a payload wrapped in Result.
/// </summary>
/// <typeparam name="TCommand">Command type.</typeparam>
/// <typeparam name="TResponse">Response payload type.</typeparam>
public interface ICommandHandler<in TCommand, TResponse> : IRequestHandler<TCommand, Result<TResponse>>
where TCommand : ICommand<TResponse>
{
}
}
public record RegisterCommand(
string Email, string Password,
string FirstName, string LastName, string PhoneNumber, int Role
) : ICommand;
using taskcoper_be.application.abstractions;
using taskcoper_be.contract.abstractions.messages;
using taskcoper_be.contract.abstractions.shared;
using taskcoper_be.contract.services.user;
using taskcoper_be.domain.abtractions.repositories;
using taskcoper_be.domain.entities;
namespace taskcoper_be.application.usecases.commands.user;
public class RegisterCommandHandler : ICommandHandler<Command.RegisterCommand>
{
private readonly IUnitOfWork _unitOfWork;
private readonly IPasswordHasherService _passwordHasherService;
public RegisterCommandHandler(IUnitOfWork unitOfWork, IPasswordHasherService passwordHasherService)
{
_unitOfWork = unitOfWork;
_passwordHasherService = passwordHasherService;
}
public async ValueTask<Result> Handle(Command.RegisterCommand request, CancellationToken cancellationToken)
{
var userExisted =
await _unitOfWork.UserRepository.FindSingleAsync(x =>
x.Email.Equals(request.Email), cancellationToken);
if (userExisted is not null)
{
throw new Exception("User Existed !");
}
var hashingPassword = _passwordHasherService.HashPassword(request.Password);
var user = new User
{
Id = Guid.NewGuid(),
Email = request.Email,
FirstName = request.FirstName,
LastName = request.LastName,
Role = request.Role == 0 ? "User" : "Admin",
HashedPassword = hashingPassword,
};
_unitOfWork.UserRepository.Add(user);
return Result.Success(user);
}
}
and my config
public static IServiceCollection AddMediatorApplication(this IServiceCollection services)
=> services.AddMediator(builder =>
{
// Scan both contract (messages) and application (handlers/validators) assemblies
builder.Assemblies = [contract.AssemblyReference.Assembly, AssemblyReference.Assembly];
builder.PipelineBehaviors = [
typeof(ValidationPipelineBehavior<,>),
typeof(PerformancePipelineBehavior<,>),
typeof(CachingPipelineBehaviorCachingBehavior<,>),
typeof(TransactionPipelineBehavior<,>),
typeof(TracingPipelineBehavior<,>)
];
})
.AddValidatorsFromAssembly(AssemblyReference.Assembly, includeInternalTypes: true);
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels