Click here to Skip to main content
15,912,578 members

Welcome to the Lounge

   

For discussing anything related to a software developer's life but is not for programming questions. Got a programming question?

The Lounge is rated Safe For Work. If you're about to post something inappropriate for a shared office environment, then don't post it. No ads, no abuse, and no programming questions. Trolling, (political, climate, religious or whatever) will result in your account being removed.

 
GeneralMQOTD Pin
V.18-Feb-16 21:21
professionalV.18-Feb-16 21:21 
GeneralRe: MQOTD Pin
Herman<T>.Instance18-Feb-16 21:29
Herman<T>.Instance18-Feb-16 21:29 
GeneralRe: MQOTD Pin
HobbyProggy18-Feb-16 21:50
professionalHobbyProggy18-Feb-16 21:50 
GeneralRe: MQOTD Pin
Brittle161818-Feb-16 22:49
Brittle161818-Feb-16 22:49 
Generalnot quite serendipity, but impressive: Jeremy Skinner's 'FluentValidation github project PinPopular
BillWoodruff18-Feb-16 11:43
professionalBillWoodruff18-Feb-16 11:43 
GeneralRe: not quite serendipity, but impressive: Jeremy Skinner's 'FluentValidation github project Pin
Kevin Marois18-Feb-16 11:46
professionalKevin Marois18-Feb-16 11:46 
GeneralRe: not quite serendipity, but impressive: Jeremy Skinner's 'FluentValidation github project Pin
Marc Clifton18-Feb-16 12:26
mvaMarc Clifton18-Feb-16 12:26 
GeneralRe: not quite serendipity, but impressive: Jeremy Skinner's 'FluentValidation github project Pin
BillWoodruff18-Feb-16 18:37
professionalBillWoodruff18-Feb-16 18:37 
Disclaimer: the working code shown here reflects the initial efforts of a newcomer to using FluentValidation. The code shown probably does not demonstrate "best practices" in using that tool. The "take-away" I would like you to have is that: if someone like me can quickly bumble their way to something that smells like it could be useful ... so easily, so rapidly ... than: you (my technical "betters") will probably have an even easier time.

Hi Marc, oh, would I had a smattering of your depth and experience ! But, eager septuagenarian fool-for-code that I am, I took:
Marc Clifton wrote:
Besides, what if you want to change:
customer => customer.HasDiscount to customer => customer.HasDiscount || store.OffersPromotionDeal ?
As an interesting challenge ... for a newcomer to FluentValidation ... to see what I could come up to handle that. So, an hour later:
C#
// suggested by Marc Clifton:
// Besides, what if you want to change:
// customer => customer.HasDiscount to
// customer => customer.HasDiscount || store.OffersPromotionDeal ?

// Store object
public class Store
{
    public string Name { set; get; }

    public bool OffersPromotionalDeal { set; get; }

    public Store(string name, bool offerspromo)
    {
        Name = name;
        OffersPromotionalDeal = offerspromo;
    }
}

// Store Validator
public class StoreValidator : AbstractValidator<Store>
{
    public StoreValidator()
    {
        RuleFor(store => store.OffersPromotionalDeal);
    }

    // stub
    public bool OffersPromotionalDeal(bool isoffered)
    {
        return isoffered;
    }
}
Then, I added this to the 'Customer Class:
C#
public List<Store> CurrentStores = new List<Store>();
public Store CurrentCustomerStore { set; get; }
The "Rule" that matches your criteria (plus one criterion I added) now becomes:
C#
// added by bw
// edited to add custom error message over-rides
RuleFor(customer => customer.HasDiscount).Must(disc => disc == true).WithMessage("Customer must have a current Discount");
RuleFor(customer => customer.Discount).LessThan(100).WithMessage("Discount must be less than 100%");
RuleFor(customer => customer.CurrentStore).NotNull().WithMessage("Customer must have a Current Store");
RuleFor(customer => customer.CurrentStore.OffersPromotionalDeal).Must(deal => deal == true).WithMessage("Current Store must have a current promotional offer");

// note: I am sure there's a better way to define this rule using 
// FluentValidation syntax I have not explored ... yet
// this is just an initial pass ...
I suspect this "Rule" can be greatly improved once I understand the rich set of "Rule" functionalities FluidValidation exposes.

Here's the output of my first test:
failed validation with 3 errors

Error Message: 0 : Customer must have a current Discount
Error Code: predicate_error

Error Message: 1 : Discount must be less than 100%
Error Code: lessthan_error

Error Message: 2 : Current Store must have a current promotional offer
Error Code: predicate_error
When I think I have an implementation of all this in optimal FluentValidation style, I'll post a link to a working sample as a Tip/Trick.
«In art as in science there is no delight without the detail ... Let me repeat that unless these are thoroughly understood and remembered, all “general ideas” (so easily acquired, so profitably resold) must necessarily remain but worn passports allowing their bearers short cuts from one area of ignorance to another.» Vladimir Nabokov, commentary on translation of “Eugene Onegin.”


modified 19-Feb-16 7:42am.

GeneralRe: not quite serendipity, but impressive: Jeremy Skinner's 'FluentValidation github project Pin
Marc Clifton20-Feb-16 8:13
mvaMarc Clifton20-Feb-16 8:13 
GeneralI'm as mad as hell and I'm not going to take this anymore! PinPopular
GuyThiebaut18-Feb-16 8:13
professionalGuyThiebaut18-Feb-16 8:13 
GeneralRe: I'm as mad as hell and I'm not going to take this anymore! Pin
Manfred Rudolf Bihy18-Feb-16 8:25
professionalManfred Rudolf Bihy18-Feb-16 8:25 
GeneralRe: I'm as mad as hell and I'm not going to take this anymore! Pin
GuyThiebaut18-Feb-16 8:31
professionalGuyThiebaut18-Feb-16 8:31 
GeneralRe: I'm as mad as hell and I'm not going to take this anymore! Pin
Manfred Rudolf Bihy18-Feb-16 8:43
professionalManfred Rudolf Bihy18-Feb-16 8:43 
PraiseRe: I'm as mad as hell and I'm not going to take this anymore! Pin
Duncan Edwards Jones18-Feb-16 8:26
professionalDuncan Edwards Jones18-Feb-16 8:26 
GeneralRe: I'm as mad as hell and I'm not going to take this anymore! Pin
Cornelius Henning18-Feb-16 8:37
professionalCornelius Henning18-Feb-16 8:37 
GeneralRe: I'm as mad as hell and I'm not going to take this anymore! Pin
GuyThiebaut18-Feb-16 8:43
professionalGuyThiebaut18-Feb-16 8:43 
GeneralRe: I'm as mad as hell and I'm not going to take this anymore! Pin
Cornelius Henning18-Feb-16 8:57
professionalCornelius Henning18-Feb-16 8:57 
GeneralRe: I'm as mad as hell and I'm not going to take this anymore! Pin
BillWoodruff18-Feb-16 11:26
professionalBillWoodruff18-Feb-16 11:26 
GeneralRe: I'm as mad as hell and I'm not going to take this anymore! Pin
GuyThiebaut19-Feb-16 6:49
professionalGuyThiebaut19-Feb-16 6:49 
GeneralRe: I'm as mad as hell and I'm not going to take this anymore! Pin
JHizzle18-Feb-16 21:58
JHizzle18-Feb-16 21:58 
GeneralRe: I'm as mad as hell and I'm not going to take this anymore! Pin
GuyThiebaut19-Feb-16 0:38
professionalGuyThiebaut19-Feb-16 0:38 
GeneralDo You Think ASP.NET has a Future? Pin
User 1218466518-Feb-16 8:00
User 1218466518-Feb-16 8:00 
GeneralRe: Do You Think ASP.NET has a Future? PinPopular
Kevin Marois18-Feb-16 8:08
professionalKevin Marois18-Feb-16 8:08 
GeneralRe: Do You Think ASP.NET has a Future? Pin
ClockMeister19-Feb-16 6:10
professionalClockMeister19-Feb-16 6:10 
GeneralRe: Do You Think ASP.NET has a Future? Pin
Kevin Marois19-Feb-16 6:15
professionalKevin Marois19-Feb-16 6:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.