Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6895317
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:53:37+00:00 2026-05-27T06:53:37+00:00

The ever so popular discussion on designing proper DAOs always concludes with something along

  • 0

The ever so popular discussion on designing proper DAOs always concludes with something along the lines of “DAOs should only perform simple CRUD operations”.

So what’s the best place to perform things like aggregations and such? And should DAOs return complex object graphs resembling your data source’s schema?

Assume I have the following DAO interface:

public interface UserDao {
    public User getByName(String name);
}

And here are the Objects it returns:

public class Transaction {
    public int amount;
    public Date transactionDate;
}

public class User {
    public String name;
    public Transaction[] transactions;
}

First of all, I consider the DAO to be returning a standard Value Object if all it does is CRUD operations.

So now I have modeled by DAO to return something based on a data store relationship. Is this correct? What if I have a more complex object graph?

Update: I guess what I am asking in this part is, should the return value of a DAO, be it VO, DTO, or whatever you want to call it, be modeled after the data store’s representation of the data? Or should I, say introduce a new DAO to get a user’s transactions and for each user pulled by the UserDAO, invoke a call to the TransactionDAO to get them?

Secondly, let’s say I want to perform an aggregation for all of a user’s transactions. Using this DAO, I can simply get a user, and in my Service loop though the transactions array and perform the aggregation myself. After all, it’s perfectly reasonable to say that such an aggregation is a business rule that belong in the Service.

But what if a user’s transactions number in the tens of thousands. That would have a negative impact on application performance. Would it be incorrect to introduce a new method on the DAO that does said aggregation?

Of course this might be making an assumption that the DAO is backed up by a database where I can write a simple SELECT SUM() query. And if the DAO implementation changes to say a flat file or something, I would need to do the aggregation in memory anyway.

So what’s the best practice here?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T06:53:37+00:00Added an answer on May 27, 2026 at 6:53 am

    I use the DAO as the translation layer: read the db objects, create the java side business objects and vice versa. Sometimes a couple of calls might be used to store or create a business object. For the provided example, I would make two calls: one for the user info, one for the list of the user’s transactions. The cost is an extra database call. I’m not afraid to make an extra call if I’m using connection pooling and I’m not repeating calculations. Separate calls are simpler to use (unpacking an array of composite types from a jdbc call is not simple and typically requires the proprietary connection object) and provide resusable components. Let’s say you wanted the user object for a login screen: you can use the same user dao and not have to pull in the transaction stuff.

    If you didn’t actually want the transaction details but were just interested in the aggregate, I would do the aggregate work on the database side and expose it via a view or a stored procedure. Relational databases are built for and excel at these kinds of set operations. You are unlikely to perform the operations better. Also, there is no point sending all the data over the wire if the result will do. So sure, add another dao for the aggregate if there are times you are only interested in that.

    Is it safe to assume the dao maps to a relational db? If that is how you are starting, I would wager that the backing datastore will remain a relational db. Sometimes there is a lot of fuss and worry to keep it generic, and if you can, great. But it seems to me just changing the type of relational db in the back is further than most apps would go (let alone changing to a non-relational store like a flat file).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am running the ever popular: Jquery Validate: http://bassistance.de/jquery-plugins/jquery-plugin-validation/ $(#lo-form).validate(); Its pretty straight forward.
Now that JavaScript libraries like jQuery are more popular than ever, .js files are
Ever since JQuery came along a few years ago I've been using it in
Ever since I started using .NET, I've just been creating Helper classes or Partial
Ever since switching from TABLE-layout to DIV-layout, one common problem remains: PROBLEM : you
Ever since the publication of Enterprise Integration Patterns people have been using the notation
Ever since Microsoft has introduced the application blocks, I've been bumping into people who
Ever stumbled on a tutorial that you feel is of great value but not
Ever wonder what wikipedia's database schema looks like? I recently read this thread from
Ever since I upgraded to Delphi 2009, I am having terrible experience with TFrame

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.