In my ASP.NET Core-6 Web API, I have this code:
Identity.ApplicationUser:
public class ApplicationUser : IdentityUser
{
public string MobileNumber { get; set; }
[DefaultValue(false)]
public bool? IsAdmin { get; set; }
}
Helper:
public class ServiceResult<T> : ServiceResult
{
public T Data { get; set; }
public ServiceResult(T data)
{
Data = data;
}
public ServiceResult(T data, ServiceError error) : base(error)
{
Data = data;
}
public ServiceResult(ServiceError error) : base(error)
{
}
}
public class ServiceResult
{
public bool Successful => this.Error == null;
public ServiceError Error { get; set; }
public ServiceResult(ServiceError error)
{
if (error == null)
{
error = ServiceError.DefaultError;
}
Error = error;
}
public ServiceResult() { }
public static ServiceResult Failed(ServiceError error)
{
return new ServiceResult(error);
}
public static ServiceResult<T> Failed<T>(ServiceError error)
{
return new ServiceResult<T>(error);
}
public static ServiceResult<T> Failed<T>(T data, ServiceError error)
{
return new ServiceResult<T>(data, error);
}
public static ServiceResult<T> Success<T>(T data)
{
return new ServiceResult<T>(data);
}
}
I am using ASP.NET IdentityDbContext.
Request wrapper:
public interface IRequestWrapper<T> : IRequest<ServiceResult<T>>
{
}
Request handler wrapper:
public interface IRequestHandlerWrapper<in TRequest, TResponse> : IRequestHandler<TRequest, ServiceResult<TResponse>>
where TRequest : IRequestWrapper<TResponse>
{
}
Then the DTO:
public class AdminUserListDto : IRequest
{
public Guid Id { get; set; }
public string Firstname { get; set; }
public string Lastname { get; set; }
public string Email { get; set; }
public string UserName { get; set; }
public string MobileNumber { get; set; }
public bool? IsAdmin { get; set; }
public void Register(TypeAdapterConfig config)
{
config.NewConfig<ApplicationUser, AdminUserListDto>();
}
}
DependencyInjection:
public static class DependencyInjection
{
public static IServiceCollection AddApplication(this IServiceCollection services)
{
// Register Application Health Checks
services.AddHealthChecks()
.AddCheck<ApplicationHealthCheck>(name: "DDM API");
// Register Fluent Validation service
services.AddValidatorsFromAssembly(Assembly.GetExecutingAssembly());
// Register MediatR Services
services.AddMediatR(Assembly.GetExecutingAssembly());
services.AddTransient(typeof(IPipelineBehavior<,>), typeof(UnhandledExceptionBehaviour<,>));
services.AddTransient(typeof(IPipelineBehavior<,>), typeof(ValidationBehaviour<,>));
services.AddTransient(typeof(IPipelineBehavior<,>), typeof(PerformanceBehaviour<,>));
return services;
}
}
Program.cs:
var builder = WebApplication.CreateBuilder(args);
// Add library project references for DependencyInjection
builder.Services.AddApplication();
CreateAdminUserCommand:
public class CreateAdminUserCommand : IRequestWrapper<AdminUserListDto>
{
public string UserName { get; set; }
public string Email { get; set; }
public string MobileNumber { get; set; }
public string Password { get; set; }
public bool? IsAdmin { get; set; }
}
CreateAdminUserCommandValidator:
public class CreateAdminUserCommandValidator : AbstractValidator<CreateAdminUserCommand>
{
public CreateAdminUserCommandValidator()
{
RuleFor(u => u.UserName)
.NotEmpty().WithMessage("User Name field is required. ERROR!");
RuleFor(u => u.MobileNumber)
.NotEmpty().WithMessage("Mobile Number field is required. ERROR!");
RuleFor(u => u.Email)
.NotEmpty().WithMessage("Email field is required. ERROR!");
RuleFor(u => u.Password)
.NotEmpty().WithMessage("Password field is required. ERROR!");
}
}
Lastly, I have :
public class CreateAdminUserCommandHandler : IRequestHandlerWrapper<CreateAdminUserCommand, AdminUserListDto>
{
private readonly IMediator _mediator;
private readonly IAppDbContext _dbContext;
private readonly UserManager<ApplicationUser> _userManager;
private readonly RoleManager<ApplicationRole> _roleManager;
public CreateAdminUserCommandHandler(IMediator mediator, IAppDbContext dbContext, UserManager<ApplicationUser> userManager, RoleManager<ApplicationRole> roleManager)
{
_mediator = mediator;
_dbContext = dbContext;
_userManager = userManager;
_roleManager = roleManager;
}
public async Task<ServiceResult<AdminUserListDto>> Handle(CreateAdminUserCommand request, CancellationToken cancellationToken)
{
var adminUser = new ApplicationUser()
{
MobileNumber = request.MobileNumber,
UserName = request.UserName,
Email = request.Email,
IsAdmin = request.IsAdmin
};
var result = await _userManager.CreateAsync(adminUser, request.Password);
return ServiceResult.Success(_mediator.Send<AdminUserListDto>(adminUser));
}
}
After success, I expect it to return:
AdminUserListDto
But I got this error:
Argument 1: cannot convert from 'Identity.ApplicationUser' to 'MediatR.IRequest<Response.AdminUserListDto>'
Then adminUser is highlighted in:
return ServiceResult.Success(_mediator.Send(adminUser));
How do I resolve this?
Thank you
CodePudding user response:
You are probably confusing mediator
and mapper
.
What you need is:
return ServiceResult.Success(_mapper.Map<AdminUserListDto>(adminUser));