I'm in the beginning of a project which I want to have the best testability and maintainability possible. To achieve this, I'm reading a lot about concepts in the DDD area, mainly.

To get you contextualized, suppose the following entities:

  • User
    It's basically a container for Accounts (see below) and configurations;
  • Account
    It's a way for a User to login. Each User may have various accounts, one for each OAuth provider (Facebook, Google, etc);
  • Session
    It's a token that points to a User + expiration date. Each login generates a new Session.

I need to find a User by Account (when I'm logging in) or Session (when I'm already logged in).

However, I'm afraid that I'll be bloating my DAOs with findByX methods - these seem to be the most performant and simple to understand, but they kinda couple the DB structure to some business rule - or am I wrong?

This post says that I should be using a Specification interface, which its implementations should return the right query criteria.

Is there any problem I may face upfront - like bloating my DAOs with find methods, or am I just over concerned with this architecture?


Your findBy methods don't couple you to the database structure, they couple you to business requirements. Personally, I don't mind repositories that have multiple findBy methods, as long as they are clean and neat. For example:

User findById(string id);
User findByEmail(string email);
User findByUserName(string userName);

To me seems like a clean interface. It can get a little big in the long run, but I'm not too concerned about that because the methods are small and easy to read.

On the other hand, compare this:

IList<User> findByEmailLikeAndNameLike(string email, string name);
IList<User> findByEmailWithStatusAndLastLoginAfter(string email, UserStatus status, DateTime lastLogin);
IList<User> findByEmailAndStatus(string email, UserStatus status);

These are very specific methods that are still business requirements but get VERY unwieldy in the long run and duplicate a lot of logic. I'd replace those three methods by a generic findBy that I can pass a specification to.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.