Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to decouple data from business logic

Here is the scenario,

Let's say I have a user class like so:

public class User{
  private String firstName;
  private String lastName;
//...
// setter, getters
}

Then I have a class like so to handle user Comments:

public class Comments{
  // some fields
  public static loadComments(User user, int count){...}
}

So far very basic stuff. However, I want to add some helpers of course to make it easier load comments for user. So I could create something in User class:

final static int defaultCount = 10;
...
public Comment comments(){
  return Comments.loadComments(this, defaultCount);
}

I think this is an easy way to not have to pass around user instances around. But at this point, I am unhappy because I have coupled my user bean object with business logic that loads the comment. I have also saved the default count in user class which shouldn't belong there. So what is the best way to do this? My goal is to pass this object to a jsp so that JSTL functions can be called. I had an idea to create a UserWrapper which would look like this...

public class UserWrapper{
  private final static defaultCount = 10;
  private final User user;
  public UserWrapper(User user){
    this.user = user;
  }

  // should probably cache this but i am not going to show this for simplicity
  public Comments getComments(){return Comments.loadComments(user, 10);}
}

I hope I was clear. I don't like using useBean tag because its just not necessary for something like this. I am hoping there is a cleaner way to approach something like this! Any help would be appreciated!

Edit: One thing I forgot to mention. I want to be able to use this code in JSTL. Which means it must be a getter. The DAO model is well known but it doesn't help too much when my front-end developer needs to write a scriplet or I need load it for places that he may or may not need.

like image 905
Amir Raminfar Avatar asked Oct 26 '10 19:10

Amir Raminfar


People also ask

How do you separate business logic?

Separating the UI from Business Logic The concept is simple: divide a computer program into sections that each address a separate and distinct concern of the application. This so-called "concern" can be something as simple as naming a class that is instantiated or as broad as platform specific details (Figure 1).

How do I separate business logic from presentation layer?

When separating the business and presentation logic, you need to consider the following: Avoid affinities between the two parts of the application. Be aware of the DPL-restricted API; see Exception conditions for LINK command for details. Be aware of hidden presentation dependencies, such as EIBTRMID usage.

Why do we separate business logic?

By separating your models from views, you reduce the number of dependencies between them. Therefore, changes made to one of the layers have the lowest possible impact on other layers. This separation also improves the code reusability.


2 Answers

From a technology independent stand-point...

Yes, you're absolutely right about that coupling being prohibitive. Look to the Layers architectural pattern to offer some guidance about structure business logic and data. For example, it might be an excellent idea to design two subsystems: One for your Logic, the other for Layers. Restrict communication between them by only allowing the Logic to pass messages to the Data Layer. Furthermore, you can use the Facade pattern to represent each subsystem, and thus decrease coupling even more. Treat each Layer as a black box.

Another pattern that might come in handy is the Data Access Object pattern. It will help you in defining a contract between the Layers to pass data around when it's necessary.

like image 86
Mike Avatar answered Sep 19 '22 02:09

Mike


I like to use Data Access Objects (DAOs) for this. Your User and Comment classes will only contain the fields for those objects and then you create separate classes to retrieve data about those objects. The DAOs are where you include the logic about how to retrieve, update, select, etc For example,

public class UserDAO {

     public UserDAO() {
        //maybe setup a Hibernate connection or JDBC connection here
     }

     public User getUser(int userId) {
        User user;
        //code to retrieve user from datasource
        return user;
     }

     //other methods for update, delete, select, etc
  }

  public class CommentsDAO {
     private static int DEFAULT_COUNT = 10;

     public CommentsDAO() {
        //maybe setup a Hibernate connection or JDBC connection here
     }

     public Collection getCommmentsForUserWithCount(User user, int count) {
        Collection comments = new Set();
        //retrieve comments from datasource limit to count

        return comments;
     }

     public Collection getCommentsForUser(User user) {
        return getCommentsForUserWithCount(user, DEFAULT_COUNT);
     }

     //other methods for update, delete, etc
  }
like image 34
Owen Avatar answered Sep 20 '22 02:09

Owen