Which object is responsible for this...?

Which object is responsible for this...?

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


reagan123 posted on Friday, November 07, 2008

Hello.
Hopefully someone can help me out. 

Lets say we have the following scenario. 

A Car object.

A CarEvent object

Basically a car has its basic properties which includes a property for miles.

A CarEvent is basically information stating that someone is going to drive the car and captures details about the event (like how many miles will be driven).

When the CarEvent is submitted as completed, the mileage count on the car is incremented automatically.

My question is this... If I delete a CarEvent, I need to "rollback" the mileage on the Car.  Which object is responsible for the rollback.  I'm assuming it would be the Car.  If this is the case, would I just add code to the DataPortal_Delete method on the CarEvent that asks the Car to roll its counts back?  If not, where is the best place for this type of code?

I'm still trying to grasp the concept on how to do some of this stuff.

Any help greatly appreciated?

Hope this makes sense.

dlambert replied on Friday, November 07, 2008

There's a time element here that I'm not quite grasping yet.

I understand that you can create a CarEvent to "reserve" a car for usage that's going to happen in the future, right?  So if you delete the CarEvent before it's been driven, there's no work to be done re: mileage, right?

When the trip is taken, you've probably got some sort of method on car like Car.RecordTrip(CarEvent) that takes the info in CarEvent and applies it to its properties, such that mileage is racked up, and so on.

I get the sense you're also interested in deleting a CarEvent after it's been recorded, and it might help to have you clarify your intent here, as well.  Are you trying to actually roll back state, as if the trip never happened?  If so, do you intend to be able to roll back more than one trip?  What happens if you try to delete a CarEvent that's not the most recent?  Don't you end up with a Car object in a pretty goofy state if you start doing this?

Sorry if I'm missing something here...

reagan123 replied on Friday, November 07, 2008

"I get the sense you're also interested in deleting a CarEvent after it's been recorded,"

Yes, That is exactly the scenario.  It nees to roll back state.  You are also correct in the fact that they can only delete the most recent trip.

Thanks for the quick reply :)

RockfordLhotka replied on Saturday, November 08, 2008

reagan123:
"I get the sense you're also interested in deleting a CarEvent after it's been recorded,"

Yes, That is exactly the scenario.  It nees to roll back state.  You are also correct in the fact that they can only delete the most recent trip.

Thanks for the quick reply :)

It sounds to me then, like you are confusing two different use cases. One use case is to reserve the car, another use case is to record the results of a trip.

Objects should be designed on a per use case basis. So CarEvent would be for one or the other use case, but not both.

Once you've designed separate objects for each use case you can look at the resulting objects and decide if you can achieve reuse. If you have an object in each use case that has exactly the same responsibility and which wouldn't need to be altered to be used in both use cases, then you can reuse it.

Odds are you'll have a read-only CarInfo object that can be reused, but that you'll end up with a TripReservation object in the first use case, and a TripRecord object in the second use case.

JonStonecash replied on Friday, November 07, 2008

If I take a "purist" approach, I would suggest that you compute the effective mileage by summing the values of the currently relevant CarEvents each time that you need it; that is, there is no "summed mileage" property in the Car class.  If you do that, your problem, as stated, goes away.  The sum operation will only consider those CarEvents that exist at the time of the sum operation.

On the other hand, that sum operation could chew up a lot of resources.  If the summing does need to occur very often and you can afford to spend the resources, then you are done.  There is no need to handle the delete condition.  This would be the case if you only needed the sum for periodic reporting.    You might also want to consider performing the calculation with a CSLA command that calls the database to do the summing with SQL.  If you make the make the GetSum method (property or function) of the Car class a "lazy" method, it would check to see if it already had the sum.  If it did not have the sum, it would compute it (by calling the command object).  If it did have the sum (perhaps just computed), it would return that sum.  If this needs to be more dynamic, you could add an event to the CarEvents Collection that signalled any change in the collection.  The event handler for that event would reset the "I have already computed the sum" indicator.  The next time that you asked for the sum, it would be recomputed as above.

There are a lot of other ways to handle this.  Unless you have very exacting requirements for timeliness, I would not use any way that requires you to maintain the sum in the Car class.  It will just drive you nuts.  Been there, done that.

Jon Stonecash



reagan123 replied on Friday, November 07, 2008

Jon,
Thank you so much for your reply as well.  I only had a chance to skim it, but I'll take the time to think about both of these responses.

Thanks once again to both of you. :)

RockfordLhotka replied on Saturday, November 08, 2008

reagan123:
Hello.

My question is this... If I delete a CarEvent, I need to "rollback" the mileage on the Car.  Which object is responsible for the rollback.  I'm assuming it would be the Car.  If this is the case, would I just add code to the DataPortal_Delete method on the CarEvent that asks the Car to roll its counts back?  If not, where is the best place for this type of code?

In real life I think you can go to jail for mileage fraud if you do this :)

Copyright (c) Marimer LLC