I was trying to use FluentValidation library in F Sharp sample. But I got stuck as I can't even turn a simple C Sharp code to F Sharp code.
我试图在F Sharp样本中使用FluentValidation库。但是我被困住了,因为我甚至不能把一个简单的C夏普代码转换成F夏普代码。
But then I thought that this wonderful library is just trying to bring functional bits of programming side to CSharp, so instead of using this I should create my own library in FSharp only. That will be easy and appropriate way.
但是后来我觉得这个很棒的库只是想把编程方面的功能部分带到CSharp,所以我应该用FSharp来创建我自己的库,而不是使用它。这将是一种简单而恰当的方式。
So, I need a opinion in that, which way will be better. And if someone can create FSharp sample for this, that will be great. It is just for learning purpose as I mostly use fluent library in C#. And I like to go with them in F#.
所以,我需要一个意见,哪种方式更好。如果有人能创造出FSharp样本,那就太好了。这只是为了学习,因为我在c#中主要使用fluent库。我喜欢和他们一起去f#。
1 个解决方案
#1
4
F# supports fluent DSLs, and there are several F# libraries with a fluent API. F#'s type system is a bit different from C#'s, and most of the differences pop-up with fluent APIs, but still, this works:
f#支持fluent dsl,并且有几个f#库具有fluent API。f#的类型系统与c#有一点不同,大多数的差异都是用流畅的api弹出的,但是仍然是这样的:
#r @"C:\Users\Ramon\Downloads\FluentValidation\FluentValidation\FluentValidation.dll"
open System
open FluentValidation
type Customer =
{ Surname : string
Forename : string
Company : string
Discout : int
Address : string
Postcode : string
Discount : int
HasDiscount : bool }
type IRuleBuilder<'T,'Property> with
member __.Ignore = ()
type CustomerValidator =
inherit AbstractValidator<Customer>
new () =
let beAValidPostcode postcode = true
base.RuleFor(fun customer -> customer.Surname).NotEmpty().Ignore
base.RuleFor(fun customer -> customer.Forename).NotEmpty().WithMessage("Please specify a first name").Ignore
base.RuleFor(fun customer -> customer.Company).NotNull().Ignore
base.RuleFor(fun customer -> customer.Discount).NotEqual(0).When(fun customer -> customer.HasDiscount).Ignore
base.RuleFor(fun customer -> customer.Address).Length(20, 250).Ignore
base.RuleFor(fun customer -> customer.Postcode).Must(beAValidPostcode).WithMessage("Please specify a valid postcode").Ignore
{ }
#1
4
F# supports fluent DSLs, and there are several F# libraries with a fluent API. F#'s type system is a bit different from C#'s, and most of the differences pop-up with fluent APIs, but still, this works:
f#支持fluent dsl,并且有几个f#库具有fluent API。f#的类型系统与c#有一点不同,大多数的差异都是用流畅的api弹出的,但是仍然是这样的:
#r @"C:\Users\Ramon\Downloads\FluentValidation\FluentValidation\FluentValidation.dll"
open System
open FluentValidation
type Customer =
{ Surname : string
Forename : string
Company : string
Discout : int
Address : string
Postcode : string
Discount : int
HasDiscount : bool }
type IRuleBuilder<'T,'Property> with
member __.Ignore = ()
type CustomerValidator =
inherit AbstractValidator<Customer>
new () =
let beAValidPostcode postcode = true
base.RuleFor(fun customer -> customer.Surname).NotEmpty().Ignore
base.RuleFor(fun customer -> customer.Forename).NotEmpty().WithMessage("Please specify a first name").Ignore
base.RuleFor(fun customer -> customer.Company).NotNull().Ignore
base.RuleFor(fun customer -> customer.Discount).NotEqual(0).When(fun customer -> customer.HasDiscount).Ignore
base.RuleFor(fun customer -> customer.Address).Length(20, 250).Ignore
base.RuleFor(fun customer -> customer.Postcode).Must(beAValidPostcode).WithMessage("Please specify a valid postcode").Ignore
{ }