使用参数的 FluentValidation 验证器

发布于 2024-09-11 10:50:49 字数 1368 浏览 4 评论 0原文

我有一个 FluentValidation 验证器,我想用它来验证预订。预订时,您必须选择您选择的旅行中可用房型的房型。我需要从服务中获取可用的房间类型,并传递旅游代码。获取旅游代码的最佳方法是什么?

到目前为止我得到了什么:

public class BookingValidator : AbstractValidator<Booking>, IBookingValidator

public BookingValidator()
{
    RuleFor(booking => booking.Rooms).SetValidator(new RoomValidator())
}

public class RoomValidator : AbstractValidator<Room>

public RoomValidator()
{
    //validate that room.Type (eg. TWIN) exists in availableRoomTypes (eg List<string> {'SINGLE','TWIN'}
}

解决问题的一些技巧:

public class BookingValidator : AbstractValidator<Booking>

//should/can i pass in arguments here when IoC container is wiring up IBookingValidator to BookingValidator? Seems awkward
public BookingValidator(string tourCode)
{

//if so, use argument to get available room types, pass to RoomValidator
var availableRooms = RoomTypeService.GetAvailableRoomTypesForTour(tourCode);

RuleFor(booking => booking.Rooms).SetValidator(new RoomValidator(availableRooms))

//alternatively, tourCode is available from booking - is there some way to pass it to RoomValidator?
RuleFor(booking => booking.Rooms).SetValidator(new RoomValidator(),booking => booking.TourCode);

//Or is there some way I should be using .Must() or Custom()??

}

所以主要问题是如何或在哪里将旅游代码输入验证器......?

I have a FluentValidation validator that I want to use to validate a booking. On a booking you must choose a room type that exists as an available room type on the tour that you are choosing. I need to get the available room types from a service, passing in the code for the tour. What is the best way to handle getting the tour code where it needs to be?

What I've got so far:

public class BookingValidator : AbstractValidator<Booking>, IBookingValidator

public BookingValidator()
{
    RuleFor(booking => booking.Rooms).SetValidator(new RoomValidator())
}

public class RoomValidator : AbstractValidator<Room>

public RoomValidator()
{
    //validate that room.Type (eg. TWIN) exists in availableRoomTypes (eg List<string> {'SINGLE','TWIN'}
}

Some hack at the problem:

public class BookingValidator : AbstractValidator<Booking>

//should/can i pass in arguments here when IoC container is wiring up IBookingValidator to BookingValidator? Seems awkward
public BookingValidator(string tourCode)
{

//if so, use argument to get available room types, pass to RoomValidator
var availableRooms = RoomTypeService.GetAvailableRoomTypesForTour(tourCode);

RuleFor(booking => booking.Rooms).SetValidator(new RoomValidator(availableRooms))

//alternatively, tourCode is available from booking - is there some way to pass it to RoomValidator?
RuleFor(booking => booking.Rooms).SetValidator(new RoomValidator(),booking => booking.TourCode);

//Or is there some way I should be using .Must() or Custom()??

}

So the main problem is how or where to get tour code into the validator...?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦幻的味道 2024-09-18 10:50:49

我建议创建一个依赖于 IRoomTypeService 和 IBookingValidator 的服务。它从 IRoomTypeService 依赖项获取可用的房间类型,并通过属性将它们传递给验证器。请参阅以下示例代码:

public class BookingValidationService : IBookingValidationService
{
    public IRoomTypeService RoomTypeService { get; set; }

    public IBookingValidator BookingValidator { get; set; }

    public ValidationResult ValidateBooking(Booking booking, string tourCode)
    {
        BookingValidator.AvailableRooms = RoomTypeService.GetAvailableRoomTypesForTour(tourCode);

        return BookingValidator.Validate(booking);
    }
}

I would suggest creating a service that has dependencies on IRoomTypeService and IBookingValidator. It gets the available room types from the IRoomTypeService dependency and passes them to the validator via a property. See the following code by way of example:

public class BookingValidationService : IBookingValidationService
{
    public IRoomTypeService RoomTypeService { get; set; }

    public IBookingValidator BookingValidator { get; set; }

    public ValidationResult ValidateBooking(Booking booking, string tourCode)
    {
        BookingValidator.AvailableRooms = RoomTypeService.GetAvailableRoomTypesForTour(tourCode);

        return BookingValidator.Validate(booking);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文