gpt4 book ai didi

c# - MediatR 库 : following the DRY principle

转载 作者:行者123 更新时间:2023-12-04 07:19:12 24 4
gpt4 key购买 nike

我使用图书馆 MediatR 在我的ASP.NET Core应用。
我有以下实体 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 )。
我有另一个想法。我可以为每种广告类型创建一个单独的命令(例如, CreateTextAdCommandCreateHtmlAdCommandCreateBannerAdCommandCreateVideoAdCommand)。这个解决方案遵循开放封闭原则(当我创建一个新的广告类型时,我需要为这个广告类型创建一个新的命令——我不需要更改现有的代码)。
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.
该方案遵循 Open Closed 原则,但违反了 DRY 原则。我怎么解决这个问题?

最佳答案

如果您坚持第二种方法,则可以利用 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 核心中,这将在您的 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/

关于c# - MediatR 库 : following the DRY principle,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68613365/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com