Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I remove magic strings from custom model binders?

I've written a couple of custom model binders now, and have realised that I've fallen into the trap of relying on magic strings, e.g.:

    if (bindingContext.ValueProvider.ContainsPrefix("PaymentKey"))
    {
        paymentKey = bindingContext.ValueProvider.GetValue("PaymentKey").AttemptedValue;
    }

I'd like to be able to use an expression to strongly-type the prefix names, but can't figure out how, and would be grateful for some assistance.

Thanks.

like image 957
Paul Suart Avatar asked Nov 14 '22 07:11

Paul Suart


1 Answers

What you are looking for is bindingContext.ModelName so your code could become:

 if (bindingContext.ValueProvider.ContainsPrefix(bindingContext.ModelName))
    {
        paymentKey = bindingContext.ValueProvider.GetValue(bindingContext.ModelName).AttemptedValue;
    }
like image 110
Clicktricity Avatar answered Dec 19 '22 00:12

Clicktricity