Csla 3.5 and private variables

Csla 3.5 and private variables

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


Wal972 posted on Sunday, September 14, 2008

Hi Gang

Just a small question recently migrated to 3.5 and noticed all the changes with the way properties work etc, and my question relates to calculations. VB.Net

Old 3.x you would do something like this

private mproperty1 as int16

private mproperty2 as int16

property Property1 as int16

etc and then if you were calculating you would just go

Dim manswer1 as int16

manswer1 = mproperty1 * mproperty2

how do you do that type of thing now ?

Thanks in advance

Ellie

triplea replied on Monday, September 15, 2008

SetProperty<int>(manswer1, GetProperty<int>(mproperty1) * GetProperty<int>(mproperty2));

That would bypass the authorization checks, setting dirty etc and will set the private member values directly. Hope this helps.

skagen00 replied on Monday, September 15, 2008

I think if you want to bypass the authorization checks you may want to use ReadProperty and LoadProperty. You would not, however, get any triggers for PropertyHasChanged, marking the object as dirty or checking validation rules for manswer1, so certainly keep that in mind. 

Using LoadProperty on a managed property really has a similar effect to setting a private field in pre managed property code.

triplea replied on Monday, September 15, 2008

Oops indeed I meant to say ReadProperty/LoadProperty. Thanks for the correction.

Wal972 replied on Monday, September 15, 2008

Thanks guys,

isn't the long way round i mean if i have a calcualtion which is within the BO and uses five or six properties the formulas are going to become unreadable.

Thanks

Ellie

skagen00 replied on Monday, September 15, 2008

It's a trade-off - I agree, you do lose a little readability.

If you used private backing fields (I don't, but you can) then you'd be able I imagine to use them directly in your formula. Additionally, I believe Rocky has said they are more performant overall. 

For more complex calculations (or esp if I'm reusing the result), sometimes I'll do ReadProperty calls into local variables so that my calculations are easier to read.

Wal972 replied on Monday, September 15, 2008

i was originally using private or protected variables behind the properties as in (edited)

Dim mInvoiceNo As int16

Public Property InvoiceNo as Int16

Get

   Return mInvoiceNo

Set

   mInvoiceNo = value

Therefore the calculations used the mInvoiceNo variable not InvoiceNo the property.

Are the private backing fields the equivalent ? and how do you blend them with the new structure of PropertyInfo etc.

Thanks for the assistance

Ellie

SonOfPirate replied on Tuesday, September 16, 2008

If readability is your only reason for avoiding managed fields, keep in mind that the earlier example can be refactored as:

SetProperty<Int16>(manswer, MProperty1 * MProperty2)

assuming that you have properties called MProperty1 and MProperty2 that implement the GetProperty method to return the values of those managed fields.

HTH

 

Wal972 replied on Tuesday, September 16, 2008

No readability is not my only concern. I have a lot of calculations already coded, the new method seems less intuitive and if i remove the private variables they are will need to be recoded and some of the calculations are quite complex. the thought is overwhelming. I use the propertyhaschanged watch (including child collections) to trigger these calculations, so how do i trigger them now.

When is Rocky publishing the VB.Net ebook for this update.

Help

Thanks

Ellie

 

Wal972 replied on Wednesday, September 17, 2008

Any other suggestions / advice ?

Kevin Fairclough replied on Thursday, September 18, 2008

Can you not just avoid using managed fields, and use backing fields with the new PropertyInfo?  This will keep the private fields allowing normal calculations using the privates.

Wal972 replied on Thursday, September 18, 2008

That would be good if I knew how to integrate it. If an example could be given (with the appropriate VB.NET code) I would greatly appreciate it.

Thanks

Ellie

stefan replied on Friday, September 19, 2008

Hi Ellie,

take a look at the current ProjectTracker code, which is using the new property syntax.
The Resource.cs/Resource.vb file is using private backing fields.

Stefan

Copyright (c) Marimer LLC