Why is IsDirty true for a new BO?

Why is IsDirty true for a new BO?

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


Steven posted on Tuesday, May 09, 2006

When I create a new BO that inherits from BusinessBase my new BO starts off with IsDirty as true. 

Why would a new instance of a BO start off dirty?

 

 

ChrisD replied on Tuesday, May 09, 2006

It's quite simple really - an object IsDirty if it's values/properties do not match a value in the database/datastore.

Since a new object does not yet exist in the datastore this implies that it IsDirty.

Easy, eh?

HTH

ChrisD

razorkai replied on Tuesday, May 09, 2006

Beat me to it Chris Smile [:)]

Steven replied on Tuesday, May 09, 2006

Thanks Chris that makes sense.

I guess my real problem is that the IsSavable is starting off as True because both IsDirty and  IsValid is starting off as true.  I have the following rule code and I was expecting IsValid to be false at start because I have StringRequired for "CODE" and the code property is empty at start.  How can I tell why is is starting with IsValid true when I think it should be false?

Thanks for your help.

Private Sub AddCommonRules()

ValidationRules.AddRule(AddressOf Validation.CommonRules.StringRequired, "CODE")

ValidationRules.AddRule(AddressOf Validation.CommonRules.StringMaxLength, New Validation.CommonRules.MaxLengthRuleArgs("CODE", 10))

xal replied on Tuesday, May 09, 2006

You should be calling CheckRules in dataportal create...
But make sure your shared method calls dataportal.create instead of doing
"return new ObjType()"


It should be something like:
Public Shared Function NewObjType(ByVal someValue As Integer) As ObjType
    Return DataPortal.Create(Of ObjType)(New Criteria(someValue))
End Function

or if you don't have criteria:
Public Shared Function NewObjType() As ObjType
    Return DataPortal.Create(Of ObjType)()
End Function

Andrés

Steven replied on Wednesday, May 10, 2006

I see that ValidationRules.CheckRules is being called from PropertyHasChanged in BusinessBase but I don't see it being called from DataPortal.Create.  So after I create a new BO that has a Code property which is required I get the following results.


            Dim cust As Customer = Customer.NewCustomer
            ' IsValid is True which is wrong since Code is required

            cust.Code = "A"
            ' IsValid is Still True which now is ok as far as Code is concerned.

            cust.Code = ""
            ' IsValid is now False, which is accurate since Code is required.

So modifying the property to trigger the PropertyHasChanged event will cause IsValid to initialize but is there a better way to do this.  Are you saying I should modify the CSLA code to call CheckRules in the DataPortal.Create, if so where exactly is that done.

Thanks

Mark replied on Wednesday, May 10, 2006

You implement DataPortal_Create in your business object and call ValidationRules.CheckRules() from there.  You don't have to modify CSLA itself.  Here's an example from Rocky's ProjectTracker sample (Project class)...

[RunLocal()]
private void DataPortal_Create(Criteria criteria)
{
   _id = Guid.NewGuid();
   _started.Date = DateTime.Today;
   ValidationRules.CheckRules();
}

Steven replied on Wednesday, May 10, 2006

Thanks Mark that was it.  My DataPortal_Create was empty.

When I created this BO I made the wrong assumption that since I was not intializing any of the properties that the ValidationRules.CheckRules call was not needed. 

I think I did that because the example Resources.vb shows DataPortal_Create empty like this:

<RunLocal()> _

Private Overloads Sub DataPortal_Create(ByVal criteria As Criteria)

' nothing to initialize

End Sub

Does this mean Resource.vb DataPortal_Create is wrong or is the call to CheckRules not needed there for some reason?

 

 

RockfordLhotka replied on Wednesday, May 10, 2006

I'd go for the code being wrong. If I recall correctly, the name properties are required and so CheckRules should be called... Oops...

Copyright (c) Marimer LLC