Link to home
Start Free TrialLog in
Avatar of Silas2
Silas2

asked on

fluent c# quesion

I've got this html helper control which has a fluent api which has a Validate(expression,message,severity) bit to the builder, but it doesn't take a Action<> type param, but you can string multip params together, e.g.:
       editor.NumericBox()
            .Validate("Include = true and Quantity = 0",
                T("Must be geater 0."))
 
            .Validate("Include = true and Quantity > Total",
                T("Too many."),
                ValidationSeverity.Error);

Open in new window

my problem is that I have a Validations collection with a variable number of validators in, what syntax do I need to append an indeterminate number of validators on?
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

I am reading this question like you need to add a new method that takes the collection as an argument.  If I am wrong, please explain what you mean in a little more detail.
Avatar of Silas2
Silas2

ASKER

This is in razor markup, the fluent api has a procedure Validatate(msg,sev) for this control, so I can do .Validate("onemsg").Validatet("2ndmsg").Validate("etc")

But I wanna do
foreach(var validation in Model.Validations)
{
     control.Validate(validation.msg)
}
which I could if the fluent api took a validationbuilder Action<> method, like:
Columns=>{
    foreach(var col in model.Columns)

}
if you get what I mean.....
That could be something like this:

editor.NumericBox()
        .ValidateAll(Model.Validations)

Open in new window

Avatar of Silas2

ASKER

so i'll have to modify the api?
I thought there might be a snappy syntax which would allow me to do it as is...
ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Silas2

ASKER

I think you've said enough, i'll go back to the guy how does these api's and see if he'll modify it for me..otherwise i'll have to hard code,
"if one:
if two :
if three:
Is the API in the same assembly as your code?
Avatar of Silas2

ASKER

I've got the source code but its in a different assembly.
Sorry, I meant same solution...

If you have the API project in the same solution, you should be able to add a static class for extension methods.

Here is an example, with a wild guess at your code, to show the realm of possibilities:

public static class ExtensionMethods
{

     public static ValidationBuilder ValidateAll(this ValidationBuilder builder, List<Validator> validatorList)
     {
           validatorList.ForEach(x => x.Validate(builder.Control));
           return builder;
     }
}

Open in new window

Avatar of Silas2

ASKER

Thanks for that, good point.