VB.NET Lambda Expressions on RegisterProperty

VB.NET Lambda Expressions on RegisterProperty

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


Israel posted on Wednesday, September 23, 2009

I have not seen any example yet of VB.NET implementation of RegisterProperty using lamba expressions similar to the C# version. I have created this test class that does that. It works fine using my limited testing. Any reason why I should avoid this notation?

Imports Csla

Public Class Class1
Inherits BusinessBase(Of Class1)

Private Shared sProp As PropertyInfo(Of String) = RegisterProperty(Of String)(Function(ss As Class1) ss.TestProperty)
Public Property TestProperty() As String
Get
Return ReadProperty(Of String)(sProp)
End Get
Set(ByVal value As String)
LoadProperty(sProp, value)
End Set
End Property

End Class

ajj3085 replied on Wednesday, September 23, 2009

Hmm... probably no reason to avoid it, but compared to the C# syntax, thats much less clear to me than just using a string literal.

RockfordLhotka replied on Wednesday, September 23, 2009

The primary advantage of the lambda expression in both C# and VB is that refactoring will automatically rename the property everywhere. So while the syntax is a little verbose, it is probably worth it to get the maintainability of having no string literals at all.

Israel replied on Wednesday, September 23, 2009

That is what I am after. I do not want to include the string literals and be a maintenance hassle later on. Thanks for the input. Greatly appreciated.

ajj3085 replied on Thursday, September 24, 2009

Ahh, hadn't considered that.

cdkisa replied on Thursday, September 24, 2009

I like the syntax and the refactoring support of lambda expressions.

My question is, when you add validation rules using the lamda syntax, how do you get the "friendly name" and default values during validation?

Currently I use:
Private Shared IdProperty As PropertyInfo(Of Integer) = RegisterProperty(Of Integer)(GetType(Class1), New PropertyInfo(Of Integer)("Identity", "Identity", -1))

If I change this to:
Private Shared IdProperty As PropertyInfo(Of Integer) = RegisterProperty(Of Integer)(Function(cls As Class1) cls.Identity)

How do you support validation?

ajj3085 replied on Thursday, September 24, 2009

In your validation rule you should be able to use the PropertyInfo.Name value. I think there's another one allowing you to get the default.

cdkisa replied on Thursday, September 24, 2009

ajj3085:
In your validation rule you should be able to use the PropertyInfo.Name value. I think there's another one allowing you to get the default.


If you use lamda expressions, how do you set the values for friendly name and default value? Do you have to set that in the constructor or the dataportal.create method?

ajj3085 replied on Thursday, September 24, 2009

There should be overloads that still take FriendlyName and default value.

Isn't this what you're looking for?

protected static PropertyInfo

RegisterProperty

(Expression> propertyLambdaExpression, string friendlyName);

cdkisa replied on Friday, September 25, 2009

Yes there is!

Thank you for pointing that out. I didn't take the time to look for myself.

Copyright (c) Marimer LLC