我在 ASP.NET Core
应用程序中使用库 MediatR
。
我有以下实体 Ad
:
public class Ad
{
public Guid AdId { get; set; }
public AdType AdType { get; set; }
public double Cost { get; set; }
public string Content { get; set; }
// ...
}
public enum AdType
{
TextAd,
HtmlAd,
BannerAd,
VideoAd
}
我想介绍制作新广告的功能。为此,我创建了以下命令:
public class CreateAdCommand : IRequest<Guid>
{
public AdType AdType { get; set; }
public double Cost { get; set; }
public string Content { get; set; }
public class Handler : IRequestHandler<CreateAdCommand, Guid>
{
private readonly MyDbContext _context;
public Handler(MyDbContext context)
{
_context = context;
}
public async Task<Guid> Handle(CreateAdCommand request, CancellationToken cancellationToken)
{
var ad = new Ad {AdType = request.AdType, Cost = request.Cost, Content = request.Content};
_context.Ads.Add(ad);
_context.SaveChangesAsync();
return ad.AdId;
}
}
}
这段代码效果很好。但这里有一个大问题:每种广告类型都有一些额外的广告创建过程逻辑(例如,在创建 TextAd
类型的广告时,我们需要在广告内容中查找关键字)。最简单的解决方案是:
public async Task<Guid> Handle(CreateAdCommand request, CancellationToken cancellationToken)
{
var ad = new Ad {AdType = request.AdType, Cost = request.Cost, Content = request.Content};
_context.Ads.Add(ad);
_context.SaveChangesAsync();
switch (request.AdType)
{
case AdType.TextAd:
// Some additional logic here...
break;
case AdType.HtmlAd:
// Some additional logic here...
break;
case AdType.BannerAd:
// Some additional logic here...
break;
case AdType.VideoAd:
// Some additional logic here...
break;
}
return ad.AdId;
}
这个解决方案违反了开放封闭原则(当我创建一个新的广告类型时,我需要在 case
内创建一个新的 CreateAdCommand
)。
我还有一个想法。我可以为每种广告类型创建单独的命令(例如,CreateTextAdCommand
、CreateHtmlAdCommand
、CreateBannerAdCommand
、CreateVideoAdCommand
)。此解决方案遵循开放封闭原则(当我创建新的广告类型时,我需要为该广告类型创建一个新命令 - 我不需要更改现有代码)。
public class CreateTextAdCommand : IRequest<Guid>
{
public double Cost { get; set; }
public string Content { get; set; }
public class Handler : IRequestHandler<CreateTextAdCommand, Guid>
{
private readonly MyDbContext _context;
public Handler(MyDbContext context)
{
_context = context;
}
public async Task<Guid> Handle(CreateTextAdCommand request, CancellationToken cancellationToken)
{
var ad = new Ad {AdType = AdType.TextAd, Cost = request.Cost, Content = request.Content};
_context.Ads.Add(ad);
await _context.SaveChangesAsync();
// Some additional logic here ...
return ad.AdId;
}
}
}
public class CreateHtmlAdCommand : IRequest<Guid>
{
public double Cost { get; set; }
public string Content { get; set; }
public class Handler : IRequestHandler<CreateHtmlAdCommand, Guid>
{
private readonly MyDbContext _context;
public Handler(MyDbContext context)
{
_context = context;
}
public async Task<Guid> Handle(CreateHtmlAdCommand request, CancellationToken cancellationToken)
{
var ad = new Ad {AdType = AdType.HtmlAd, Cost = request.Cost, Content = request.Content};
_context.Ads.Add(ad);
await _context.SaveChangesAsync();
// Some additional logic here ...
return ad.AdId;
}
}
}
// The same for CreateBannerAdCommand and CreateVideoAdCommand.
此解决方案遵循开放封闭原则,但违反了 DRY 原则。我该如何解决这个问题?
答案 0 :(得分:2)
如果您坚持第二种方法,则可以利用 MediatR 'Behaviors' (https://github.com/jbogard/MediatR/wiki/Behaviors)。它们的作用类似于管道,您可以在其中将常见行为卸载到常用处理程序中。
为此,创建一个标记界面
interface ICreateAdCommand {}
现在让每个 concreate 命令继承它
public class CreateTextAdCommand : ICreateAdCommand
{
public readonly string AdType {get;} = AdType.Text
}
public class CreateHtmltAdCommand : ICreateAdCommand
{
public readonly string AdType {get;} = AdType.Html
}
/*...*/
您可以将其组合或替换为公共抽象基类,以避免重复公共属性。这取决于你。
现在我们为我们的行为创建处理程序:
public class CreateAdBehavior<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse>
where TReq : ICreateAdCommand
{
public CreateAdBehavior()
{
//wire up dependencies.
}
public async Task<TResponse> Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate<TResponse> next)
{
var ad = new Ad {AdType = request.AdType, Cost = request.Cost, Content = request.Content};
_context.Ads.Add(ad);
await _context.SaveChangesAsync();
//go on with the next step in the pipeline
var response = await next();
return response;
}
}
现在连接这个行为。在 asp.net core 中,这将在您的 startup.cs 中
services.AddTransient(typeof(IPipelineBehavior<,>), typeof(CreateAdBehavior<,>));
在这个阶段,每次你的 IRequests
实现 ICreateAdCommand
时,它会自动调用上面的处理程序,完成后它会调用行中的下一个行为,或者如果没有,实际的处理程序。
您的特定处理程序,假设 HtmlAd 现在大致如下所示:
public class CreateHtmlAdCommand : IRequest<Guid>
{
public class Handler : IRequestHandler<CreateHtmlAdCommand, Guid>
{
private readonly MyDbContext _context;
public Handler(MyDbContext context)
{
_context = context;
}
public async Task<Guid> Handle(CreateHtmlAdCommand request, CancellationToken cancellationToken)
{
// Some additional logic here ...
}
}
}
** 更新 **
如果您想跨管道拖动数据,您可以利用实际的请求对象。
public abstract class IRequestWithItems
{
public IDictionary<string, object> Items {get;} = new Dictionary<string,object>();
}
现在在您的 CreateAdBehavior 中,您可以创建广告并将其存储在字典中,以便在下一个处理程序中检索它:
var ad = { ... }
await _context.SaveChangesAsync();
items["newlyCreatedAd"] = ad;
在实际的 Task<Guid> Handle()
方法中,您现在可以随意使用广告,而无需循环回您的数据库再次检索它。
作者详细信息:https://jimmybogard.com/sharing-context-in-mediatr-pipelines/