• Post Reply Bookmark Topic Watch Topic
  • New Topic

Cohesion Doubt  RSS feed

 
Vishnu Prakash
Ranch Hand
Posts: 1026
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it a good design to have both insert & update(into DB through JDBC) functionaly in the same method like

Design - I



or to have 2 different methods like (1) Insert (2) Update seperately
Design - II

 
Frank Carver
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I probably would not do either.

Your doubt may stem from confusion over the purpose of "insert" and "update".

"Update" is for changing value(s) of any matching existing records. "Insert" is for creating a whole new record. With this in mind I usually have one chunk of code related to creation of a record (setting default values, generating a new id, etc.) and calling "insert", and different chunks of code related to updating existing records.

Can you explain a bit more about why you don't care until this method call whether you are doing an insert or an update?
 
Vishnu Prakash
Ranch Hand
Posts: 1026
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your response


"Update" is for changing value(s) of any matching existing records. "Insert" is for creating a whole new record.


I understand. I have a new button in my search screen which gets me the new screen and when I hit save it is an "insert" functionality. In the same search screen i have a table which lists records with the first column having a hyper link on-click of which takes me to the pre-poluated editable page which is an "update" functionality.

With this in my mind should I design my method like the one below (or) follow design-I from my previous post


[ September 04, 2007: Message edited by: Vishnu Prakash ]
 
Frank Carver
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess what worries me is that you have this "saveData" method which does both an insert and an update. You have described two different operations in your UI, so I don't understand why they have to call the same method.

Is there something I am missing? Where does your sequencekey value come from?
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We've done the "save calls insert or update" thing on public APIs. We accept an XML structure that might cause us to update existing rows or append new ones, but we don't force the caller to mark each one. Isn't there even SQL syntax for "insert or update as appropriate"? Maybe not standard.

If it's an opportunity to move the if test out of a dozen clients into one service and always get it right then a convenience method wouldn't bother me.
 
Pat Farrell
Rancher
Posts: 4686
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Its clearly an engineering call, but I tend to use the


approach with the meaning "save this object to the persistent store (dbms, etc.) and don't bother me with how.

I generally define an Interface for PersistentObject that is implemented for things bound to the database.

The domain/business classes don't have to care about the database.

The real problem is that after 20 years of writing OO code, binding the object to the RDBMS is still a royal PITA.
 
Vishnu Prakash
Ranch Hand
Posts: 1026
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Is there something I am missing? Where does your sequencekey value come from?


For an "insert" I will be generating the sequenceKey(By calling a utility method - using PL/SQL sequence), from search screen I have a table which lists records with the first column having a hyper link on-click of which takes me to the pre-poluated editable page(sequenceKey is fetched from database for this corresponding record) and hitting the save which is an "update" functionality. So for update we will have sequenceKey in place

I wanted to know should I code the entire logic for insert & update in the same method like scenario-I or use scenario-II.

I feel with scenario-I cohesion concept is NOT being followed whereas in scenario-II we follow cohesion concept. Am I correct here.

scenario-I



scenario-II

 
Frank Carver
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think I see what you are getting at.

My personal preference would probably be for the first example, but that's mainly because I can easily imagine factoring out common JDBC stuff from your two paths into common code. For example:



Does that make sense?
 
Mr. C Lamont Gilbert
Ranch Hand
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I also would not do either. First I would try to use an existing ORM and persistence technology. Outside of that I think "save" and "Insert" and "Update" belong in different layers.

Save is one layer which is not a DB layer. "insert" and "update" are db aware behaviors. And certainly I think "save" should not have sql in it. maybe Save calls Insert or Update.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!