Why MarkDirty in MarkNew? (One2One problem)

Why MarkDirty in MarkNew? (One2One problem)

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


nickinchina posted on Friday, October 05, 2007

I always wonder why csla objects call markdirty in marknew. The problem i have is to create one to one relationship object.

For example, i have customer object and customerAddress object which is one2one. i like to code like:

MyCustomer.id = 1

MyCustomer.Name = "test"

MyCustomer.MyCustomerAddress.Street = "no where"

...

but sometimes i might not like to create MyCustomerAddress when it's not changed and since it's markdirty from beginning , i have no idea it's changed or not.

Also sometimes we have an object is a one2one child of itself, like an employee which has a suppervisor object which is also an employee. Now it's impossible for me to create object like that 'cause it creates infinite loop when saving the object.

I have no idea how your guys handle these scenarios, please enlighten me!!! thank in advance.

ajj3085 replied on Friday, October 05, 2007

Dirty means that the instance does not match what is in the database.  A new object, since it doesn't exist at all yet in the database, matches this definition.

nickinchina replied on Friday, October 05, 2007

thx a lot. Yes that makes some sense but how do you solve this one 2 one relationship?

public class employee

public property employeeid as integer

public suppervisor as employee

end class

in my client code, if i do:

objEmployee.id = 1

objEmployee.Save()

that would enter into an infinite loop(--->objEmployee.supervisor.save--->objEmployee.supervisor.supervisor.save...)

so i have to code like:

dim obj as employee = employee.newemployee()

obj.supervisor.delete()

obj.id= 1

obj.save

That's really weird, and i have no idea when i should call "obj.supervisor.delete()" and when i should not.

 

nickinchina replied on Friday, October 05, 2007

Also I am not really convinced by this dirty definition for a number of reasons:

1) why give object user something which is only vaguely correct? i think you can only say "dirty = false" mean the object probably matches the database. As an object user,  how does that help me?

2) business objects are created for their users. As an object user, i need a way of knowing whether my cleint app made any change to the object or not? That sounds like a very important feature to me.

3) how do you explain "deleted object"? yes they are marked as deleted but they are still matched to the database, so why are they "dirty"?

 

ajj3085 replied on Friday, October 05, 2007

Well you would do that.  You'd accept a SupervisorId (probably a Nullable<Int32>, for those without supervisors).  At most, you'd accept an EmployeeInfo, hopefully with a flag for IsSuperVisor, which the Employee object would check to ensure that the person assigned is really a supervisor. 

Also, your Employee class should be managing the employee id, not the UI (unless yoru ID is something like an SSN).

Andy

Copyright (c) Marimer LLC