-3

Tell me anybody, is it good practice to call service layer methods through domain object getters? Let me show you with an example:

public class User {
    private long id;
    private String name;
    private String email;
    private Address address;
    // constructors ...
    // getters and setters...
    public Address getAddress() {
        if (address == null) {
            address = new UserService().getAddress(this);
        }
        return address;
    }
}

public class UserService {
    public Address getAddress(User user) throws SQLException {
        return dao.getAddress(user.getId()); // execute query to user table for retrieving addr link and execute query to addr table for retrieving addr
    }
    // etc.
}

Some details of the database architecture: User and Address are different database tables. The address field in the user table contains a link to the corresponding row in the address table.

5
  • Whoever creates Users (most likely UserService itself) should check if address is null. In addition to tying a User to a specific UserService, you also are coupling a specific type of UserService. Commented Aug 28, 2020 at 22:47
  • 1
    That depends on what you mean by "good practice." Usually, you need some context to determine what that means, such as specific software requirements. Commented Aug 28, 2020 at 22:47
  • @MarioIshac, yes, UserService creates Users. You gave a couple of reasons against - this is what I need, if you provide an answer, I can accept it. I was thinking about loading the address during user creation, but I was confused by two requests in one UserService.getUser method. Commented Aug 28, 2020 at 23:06
  • @RobertHarvey, Software requirements are simple product support, scalling and application performance. Commented Aug 28, 2020 at 23:10
  • I don't see anything in your code that would preclude those things. As Mario alluded to, you've got a lot of tight coupling of classes in your code, but it may not matter if it's a smaller application. Commented Aug 28, 2020 at 23:18

1 Answer 1

1

is it good practice to call service layer methods through domain object getters?

I strongly suspect you would come to regret that over the lifetime of a project.

The usual pattern is that your domain model -- primarily entities and values -- are in memory representations of your domain information. The information that you need is right here, and all of the concerns about "where does the information come from?" and "where does it go?" live somewhere else (usually in the application code).

The better pattern is to take the information you need out of the domain entity (in this case the identifier) and then let the application code perform the I/O. For another domain entity, that would usually involve a "repository", which might be AddressBook

public class AddressBook {
    public Address getAddress(long userId) throws SQLException {
        return dao.getAddress(userId);
    }
    // etc.
}

What you will sometimes see is that the lookup capability is passed to the domain entity

public Address getAddress(AddressBook addressBook) {
    if (address == null) {

        address = addressBook.getAddress(this.getId());
    }
    return address;
}

Of course, when you do this, you now have to consider how/if your domain model code should be handling the various SQLException that may be thrown.

You get somewhat better testing if you introduce an interface, and the ability to substitute different AddressBook implementations.

That said, lazy loading is something you should treat with a great deal of suspicion, as it often is used to cover up a different deficiency in your code (entity boundaries are drawn incorrectly, or you are using a domain entity that you don't actually need, or something along those lines).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.