Cannot bind to Set Only properties

Cannot bind to Set Only properties

Old forum URL: forums.lhotka.net/forums/t/7144.aspx


Slayer posted on Monday, June 22, 2009

Hi,

I have a user object -> this object encapsulates an authenticated user. The password field can only be SET using a property. The reason for only having a SET property, is because the password is encrypted one way. The reason why i made this a property, is to add certain validation rules like max length or string required. However, I am not able to bind to this property from the UI -> should I handle the binding and error provider control manually ?

My colleagues tell me that I should make a SetPassword() method, but then I cannot associate validation rules to that property.

Does anyone have any ideas on this?

Regards

JoeFallon1 replied on Monday, June 22, 2009

A SetPassword method could end with the code:
ValidationRules.CheckRules("fakePropertyNameOfYourChoice")

Then you could set up a rule for fakePropertyNameOfYourChoice and it would run whenever your method was called.

What I don't get is: how would this method help you with databinding?

Joe

Slayer replied on Monday, June 22, 2009

Hi Joe

Exactly, that is why I am not to keen on using a method to tackle this issue.

Do you have any other ideas ?

I though I might have a boolean property called IsPasswordSet.
I would then add a validation rule to this property. If the property returns false, the 'password is a required field' validation rule will be broken. So then I would still have my SetPassword(string password) method. This method would then resolve the broken rule. The IsPasswordSet property would then return true.

Thats the best I could come up with.

triplea replied on Monday, June 22, 2009

I might be missing something but why dont you just return an empty string from your Get? This way you can bind, use validation and never expose the password value back to the UI.

Slayer replied on Tuesday, June 23, 2009

Yes, this is most certainly an option.

But this is a little confusing to the UI developer. The UI developer has no idea that the password is being encrypted behind the scenes. So whenever he requests the password`s Get property he will wonder why the property returns an empty string. This or, i should document my libraries.

tmg4340 replied on Tuesday, June 23, 2009

I guess my question is how you think databinding will work on a property where the databinding infrastructure has no way to get the value of the property?

I think you either have to have a get of some description, or else you have to drop the idea of using databinding for this value.  I agree that whatever the get returns would be potentially confusing, but I don't know of any other option.  I would also think a little documentation should do the trick for your UI developers.

HTH

- Scott

Copyright (c) Marimer LLC