A tiny teeny suggestion

A tiny teeny suggestion

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


robert_m posted on Tuesday, July 01, 2008

Wouldn't it be nice if SetProperty method returned bool which would indicate if property value was actually modified ? So we could write code like this:

public string Dummy
{
 get {.....}
 set
 {
  if (SetProperty<string>(DummyProperty, value))
   doSomething();
 }
}

ie. setter could trigger some action when Dummy property actually gets a new value....

lwmorris replied on Tuesday, July 01, 2008

I second that motion!

triplea replied on Tuesday, July 01, 2008

Isn't OnPropertyChanged and/or PropertyHasChanged enough?

RockfordLhotka replied on Tuesday, July 01, 2008

Or better yet, use the business rules functionality already in CSLA:

private static bool DoSomething<T>(T target, RuleArgs e) where T : YourClass
{
  // here you can manipulate target (the business object)
  // however you'd like in response to the property
  // having changed
  return true;  // this rule is never broken, it just does work
}

Put your work in this method, then attach the method to the property and it just works.

robert_m replied on Wednesday, July 02, 2008

It's just that, at least to me, setters seem like a good place for this kind of code, more straightforward and intuitive than creating a business rule handler to run it. But it's not really a problem, sure it can be done through business rules functionality, or even within the setter (we can still "manually" compare new and old value of the property...)

RockfordLhotka replied on Wednesday, July 02, 2008

I don’t disagree that setters are a good place for this type of code.

 

But my goal with CSLA since version 2.0 has been to standardize property code to make properties “expendable”. Make it so you can use snippets or code-gen to create and recreate property declarations without risk of losing any meaningful behavior.

 

This is important, because code in a property setter can’t easily be reused across different classes. If you find that a given property needs to exist in multiple object types (which is fairly common), it is relatively easy to reuse business/validation rules, but hard to reuse custom code in a getter/setter.

 

Rocky

 

 

From: robert_m [mailto:cslanet@lhotka.net]
Sent: Wednesday, July 02, 2008 7:07 AM
To: rocky@lhotka.net
Subject: Re: [CSLA .NET] A tiny teeny suggestion

 

It's just that, at least to me, setters seem like a good place for this kind of code, more straightforward and intuitive than creating a business rule handler to run it. But it's not really a problem, sure it can be done through business rules functionality, or even within the setter (we can still "manually" compare new and old value of the property...)



stefan replied on Thursday, July 03, 2008

But remember that CheckRules is sometimes called without knowing which property changed at all...
e.g. after fetching the data from a datastore.

If the action is required ONLY if the property really changed, then reacting to PropertyHasChanged seems the only way to go.

Stefan

Copyright (c) Marimer LLC