Generics MediatR发送泛型类型类
我一直试图通过中介发送泛型类型类。Generics MediatR发送泛型类型类,generics,dependency-injection,asp.net-core-3.0,mediator,mediatr,Generics,Dependency Injection,Asp.net Core 3.0,Mediator,Mediatr,我一直试图通过中介发送泛型类型类。 我创建了一个CreateSettingCommand类,该类通过以下方式接受任何模型: var createUserCommand = new CreateSettingCommand<Database.Models.Product> { Setting = Product }; var result = await mediator.Send(createUserCommand); 这是我的CreateSettingCommand类: publ
我创建了一个CreateSettingCommand类,该类通过以下方式接受任何模型:
var createUserCommand = new CreateSettingCommand<Database.Models.Product> { Setting = Product };
var result = await mediator.Send(createUserCommand);
这是我的CreateSettingCommand类:
public class CreateSettingCommand<T> : IRequest<CreateSettingCommandResponse>
{
public T Setting { get; set; }
}
public class CreateSettingCommandResponse : BaseResponse
{
}
public class CreateSettingCommandHandler<T> : IRequestHandler<CreateSettingCommand<T>, CreateSettingCommandResponse>
{
private readonly ApplicationDbContext context;
private readonly IHttpContextAccessor httpContext;
private readonly UserManager<ApplicationUser> userManager;
public CreateSettingCommandHandler(ApplicationDbContext context, IHttpContextAccessor httpContext, UserManager<ApplicationUser> userManager)
{
this.context = context;
this.httpContext = httpContext;
this.userManager = userManager;
}
public async Task<CreateSettingCommandResponse> Handle(CreateSettingCommand<T> request, CancellationToken cancellationToken)
{
var response = new CreateSettingCommandResponse();
var currentUser = await userManager.FindByNameAsync(httpContext.HttpContext.User.Identity.Name);
if (currentUser != null)
{
try
{
// set value to generic class
Type type = request.Setting.GetType();
type.GetProperty("ActionBy").SetValue(type, currentUser.UserName);
type.GetProperty("ActionOn").SetValue(type, DateTime.Now);
type.GetProperty("ApplicationUserId").SetValue(type, currentUser.Id);
await context.SaveChangesAsync();
}
catch (Exception ex)
{
response.AddError(ex.Message);
}
}
else
{
response.AddError("Current logged in user not found.");
}
return response;
}
}
公共类CreateSettingCommand:IRequest
{
公共T设置{get;set;}
}
公共类CreateSettingCommandResponse:BaseResponse
{
}
公共类CreateSettingCommandHandler:IRequestHandler
{
私有只读ApplicationDbContext上下文;
专用只读IHttpContextAccessor httpContext;
私有只读用户管理器用户管理器;
公共CreateSettingCommandHandler(ApplicationDbContext上下文、IHttpContextAccessor httpContext、UserManager UserManager)
{
this.context=上下文;
this.httpContext=httpContext;
this.userManager=userManager;
}
公共异步任务句柄(CreateSettingCommand请求、CancellationToken CancellationToken)
{
var response=新建CreateSettingCommandResponse();
var currentUser=await userManager.FindByNameAsync(httpContext.httpContext.User.Identity.Name);
如果(currentUser!=null)
{
尝试
{
//将值设置为泛型类
Type Type=request.Setting.GetType();
type.GetProperty(“ActionBy”).SetValue(type,currentUser.UserName);
type.GetProperty(“ActionOn”).SetValue(type,DateTime.Now);
type.GetProperty(“ApplicationUserId”).SetValue(type,currentUser.Id);
wait context.saveChangesSync();
}
捕获(例外情况除外)
{
响应。添加错误(例如消息);
}
}
其他的
{
AddError(“未找到当前登录用户”);
}
返回响应;
}
}
这是我的创业:
services.AddMediatR(AppDomain.CurrentDomain.GetAssemblies());
services.AddScoped(typeof(IRequestHandler<,>), typeof(CreateSettingCommandHandler<>));
services.AddMediatR(AppDomain.CurrentDomain.getAssemblys());
addScope(typeof(IRequestHandler),typeof(CreateSettingCommandHandler));
任何帮助都将不胜感激这是一个web应用程序
services.AddMediatR(AppDomain.CurrentDomain.GetAssemblies());
services.AddScoped(typeof(IRequestHandler<,>), typeof(CreateSettingCommandHandler<>));