CSLA .NET

From Rockford Lhotka's Expert C# 2008 and VB 2008 Business Objects books

Could BusinessBase.SetProperty<P>() return bool if PropertyHasChanged?

rated by 0 users
This post has 6 Replies | 2 Followers

Top 100 Contributor
Posts 68
Michael Posted: Mon, Jan 5 2009 6:50 PM
Hi Rocky et al

I have found myself doing this quite a lot:

set
{
    bool propertyHasChanged = value != m_value;

    SetProperty<int>(ExampleProperty, ref m_value, value);

    if (propertyHasChanged)
        // do something
}


This would be more convenient:

if (
SetProperty<int>(ExampleProperty, ref m_value, value))
    // do something

Any thoughts?

Kind regards
Mike
Top 10 Contributor
Posts 497

The framework supported manner is to override PropertyHasChanged in the business object, which only is fired if a property value has changed - I realize the location is separated from the setter which may be undesirable for some, but just wanted to make sure you were aware of that.

Chris

 

Top 100 Contributor
Posts 68
Michael replied on Mon, Jan 5 2009 7:23 PM
Thanks for the reply Chris. I hadn't thought of overriding PropertyHasChanged. That works, but would be slightly less efficient than having SetProperty return bool, and less desirable as you've mentioned, especially if you have many properties making use of it. I'll use the override for now.

Mike
Top 50 Contributor
Posts 147
vdhant replied on Mon, Jan 5 2009 7:24 PM
Hi Chris
This issue has been discussed a few time in the past, with the below link being the most recent:
Determine whether SetProperty has changed the value - http://forums.lhotka.net/forums/thread/28210.aspx

In short though there was no solid action to make a change, just work arounds.

For something to change here i should guess Rocky would need to get involved.
Hope this helps
Anthony
Top 100 Contributor
Posts 68
Michael replied on Mon, Jan 5 2009 7:29 PM
Thanks Anthony, I did do a search before posting, but didn't see that one.
Top 10 Contributor
Posts 647
skagen00:

The framework supported manner is to override PropertyHasChanged in the business object, which only is fired if a property value has changed - I realize the location is separated from the setter which may be undesirable for some, but just wanted to make sure you were aware of that.

Chris

 

An almost equivalent mechanism is to implement a business rule associated with the property ("almost" because the rule will also fire if you call CheckRules explicitly even if the property hasn't changed). However, careful implementation of the business rule logic can achieve the same results as a PropertyHasChanged override and is somewhat cleaner IMHO.

Top 500 Contributor
Posts 12
I just ran into this issue and understand the need to hook PropertyChanged (vs running code after SetProperty) otherwise subscribers will be notified BEFORE your after code runs which can be about 99.99% guaranteed that isn't what you wanted.

What about adding an override to SetProperty where you could pass in a lamda/anonymous method that SetProperty could invoke before changing the value something along the lines of:
get { return GetProperty(TaxRateProperty); }
set
{
    SetProperty(TaxRateProperty, value,
        (o, n) =>
        {
            Log.DebugFormat("Changed TaxRate from {0} to {1}.", o, n);
            //Other stuff here
        });
}
I like this alot as if you just need to do a quick one-off you can use short form [ n,o => _something += n ]  or the entire block, or ever call another method if needed.

I realize that I am coming late to the game on this discussion so is this recommendation has come up and been vetoed before I apologize.
Page 1 of 1 (7 items) | RSS

Please contact Magenic for your .NET consulting
and CSLA .NET mentoring needs.
Please consider making a donation to help support the ongoing development of CSLA .NET.

Make donation through PayPal - it's fast, free and secure!
Why donate?
Copyright (c) 2006-2010 Marimer LLC. All rights reserved.
Email admin@lhotka.net for support.
Powered by Community Server (Non-Commercial Edition), by Telligent Systems