Page 3 of 3 FirstFirst 123
Results 21 to 24 of 24

Thread: Merging service layer and DAO layer into single layer

  1. #21
    Join Date
    Mar 2008
    Posts
    257

    Default

    I love these old posts from Constv. I wish I had been working with the guy. Always clear and understandable real world explanations.

    Now, regarding this merging Dao into service issue, if following Constv's advice, I would need to have my service contain the following save method:

    Code:
    	public void save(Contact contact) {
    		contact = contactDao.makePersistent(contact);
    	}
    Somehow I'm not so impressed by my skimpy service method here.

    Should I have it return a boolean if the save fails ?

  2. #22
    Join Date
    Nov 2006
    Location
    Boston, MA
    Posts
    303

    Default

    Should I have it return a boolean if the save fails ?
    The signature of your service method should be determined by the type of contract you want the API to expose to the outside world. In some cases, the original object that is being saved might be updated to store a newly set - by the service - value (e.g. some id, status, or whatever.) If that is a possibility, it might be useful to return the object itself. Returning a boolean sometimes makes sense and sometimes adds no value. It all depends on your requirements. If failure to save means a critical error, an exception would be appropriate. If you really don't know what to do with it, I'd say leave it as is - you can always come back and change it once your requirements shape up. Don't rush to add complexity just for the sake of it. Simplicity has not hurt anyone. Unnecessary complexity - that's what kills projects. Good luck.

  3. #23

    Default

    @stephaneeybert

    "Should I have it return a boolean if the save fails ?"

    No. In such case what you should do is throw a RuntimeException. You can define your own exceptions so that it is meaningful to your application (as long as it extends RuntimeException). Though there is a slight expense in using exceptions, it makes it easier to handle issues and makes for more meaningful stacktraces when you log those exceptions. So, if an SQLException is thrown within your DAO, wrap it with your RuntimeException and throw it back up the application stack. Capture it within your presentation layer and handle it from there (log it and display an error comfort page for example). In all, you get more value using exceptions.

    Shah.

    http://richdomain.org
    Last edited by shahnawazshahin; Nov 24th, 2010 at 03:24 AM. Reason: Modify signature

  4. #24
    Join Date
    Mar 2008
    Posts
    257

    Default

    I just found out about Constantin's blog article on error handling at
    http://articles.vconst.com/2009/08/e...s-in-java.html
    What a gem.
    Thanks guys.
    Stephane

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •