How to handle insertable but non-updateable properties?

How to handle insertable but non-updateable properties?

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


david.wendelken posted on Wednesday, October 24, 2007

I have standard editable business objects (in editable collections, of course).

Some of the properties can be set when the object is new, but they cannot be updated once the object has been saved.  I cannot limit the setting of the properties to constructors only, so read-only properties won't do. 

I can't imagine wanting to have two sets of objects, one for new and one for old, and trying to manage them in the same strongly typed collection - unless there is some way to avoid duplicating code.

Since I can set the values some of the time, the properties have to have setters.

Should I throw an exception when someone tries to change one of these property values when it's an old object instead of a new one?

Or is there a better way?.

 

tmg4340 replied on Wednesday, October 24, 2007

I know this has been discussed in the forums before, but I can't remember under what topic - otherwise I'd send you there.

IIRC, the gist of the prior conversations is that you override CanWriteProperty() in your business object.  Then you can look at IsNew/IsDirty to determine whether the property should be writable.  That allows you to write your setters like you always would, and decide whether you want to throw an exception (which I probably would) or do something else.

david.wendelken replied on Wednesday, October 24, 2007

Thanks!  Much better than the idea I had!

KKoteles replied on Wednesday, October 24, 2007

David,

I added custom rules; however, that doesn't provide the best feedback since the user doesn't know until they try to make a change.  It would be much better to address this through authorization rules.  Could you post an example of what you might come up with here?

Here is an example of my validation rule:

protected void AddCustomRules()
{
               
//
               
// Name
               
//
                ValidationRules.AddRule<StatusType>(CanChangeOnlyWhenNewName<StatusType>, "Name");
}

private static bool CanChangeOnlyWhenNewName<T>(T target, Csla.Validation.RuleArgs e) where T: StatusType
{
               
if (target.IsNew != true)
                {
                               
if (target._name != target._originalName)
                                {
                                                e.Description =
"Can only change Name when object is new.";
                                               
return false;
                                }
                               
else
                                {
                                               
return true;
                                }
                }
               
else
                {
                               
return true;
                }
}


 Thanks,

Ken

KKoteles replied on Monday, October 29, 2007

Wow - found this in another thread.  I feel pretty foolish now that I see how simple it is:

public override bool CanWriteProperty(string propertyName)
{
    if (this.IsNew != true)
    {
        if (propertyName == "Name")
        {
            return false;
        }
    }

    return base.CanWriteProperty(propertyName);
}

tmg4340 replied on Monday, October 29, 2007

This is what I was talking about earlier - I knew it was covered in another thread.  Can you link to the thread you found?  IIRC, the discussion was rather long, and involved a couple of other facets to this issue.

I wouldn't feel foolish about your solution - everybody has to learn this stuff somehow.  Smile [:)]

KKoteles replied on Monday, October 29, 2007

tmg4340,

Here is the thread I saw it on, but I'm pretty sure this is not the one you were thinking of because it is so recent:

http://forums.lhotka.net/forums/thread/18730.aspx

I found several others as well:

http://forums.lhotka.net/forums/thread/2642.aspx

http://forums.lhotka.net/forums/thread/10837.aspx

http://forums.lhotka.net/forums/thread/12789.aspx

Ken

Copyright (c) Marimer LLC