1
\$\begingroup\$

I have a bunch of models (using activejdbc) Person, Company, Health, .... etc (I have 8 models). I'm showing an example of just one model Person

In the first approach I have a data manager that handles all my insertions to my various models, for example for Person I have

public class Person extends Model {
    static {
        validatePresenceOf(“name”, “birthdate”);
    }
}

and in the Data Manager I do the insertion

public class DataManager {
    private DBManager dbManager;
    public void insertData(Data data) throws Exception {
        try {
            dbManager.openActiveJDBCBaseConnection();
            insertPerson(data)
        } catch (Exception e) {
            // stuff
        } finally {
            dbManager.closeActiveJDBCBaseConnection();
        }
    }
   public void insertPerson(Data data) {
       Person person = new Person();
       person.set(“name”, data.name);
       person.set(“birthdata”, data.birthdata);
       person.set(“country”, data.country();
       person.saveit();
}

Versus doing the actual insertion from the person model itself using a static method and then calling it from the data manager

public class Person extends Model {
    static {
        validatePresenceOf(“name”, “birthdate”);
    }
    public static void insertPerson(PersonData data) {
        Person person = new Person();
        person.set(“name”, data.name);
        person.set(“birthdata”, data.birthdata);
        person.set(“country”, data.country();
        person.saveit();
    }

}

public class DataManager {
    private DBManager dbManager;
    public void insertPerson(PersonData data) throws Exception {
        try {
        dbManager.openActiveJDBCBaseConnection();
            Person.insertPerson(data);
        } catch (Exception e) {
            // stuff
        } finally {
            dbManager.closeActiveJDBCBaseConnection();
        }
    }
}

Which is better?

\$\endgroup\$
  • \$\begingroup\$ I have the same structure for my get queries \$\endgroup\$ – test Feb 20 '15 at 0:19
1
\$\begingroup\$

In my opinion the first example you've given is the best. It seems to adhere more to the single responsibility principle. What this basically means is that your classes should only one responsibility.

Note: You're actually still breaking the principle, because I think activeJDBC requires objects to save themselves? (I'm not experienced with activeJDBC, but after looking around a little it seems that this is the way it's done). For example Person.saveit() isn't really a great solution in my opinion, but we'll ignore that.

The reason why I would prefer the first example is because it makes a lot more sense to use something called a DataManager for insertions, than a Person class.

If you look the the second example Person.insertPerson() just really doesn't make much sense. Does a Person really insert a person? In my opinion you should stick as close to reality as possible (of course there are exceptions), and letting a person insert a person doesn't really reflect reality. Person.sit() or Person.eat() is something I would expect to find in a Person class.

A DataManager (what's in a name) indicates that it manages data. And at first thought I could assume/think that this would be used for inserting/receiving/updating objects (if I hadn't inspected the code). Saying DataManager.insertPerson() seems like the more logical solution for me (even though it still has to actually use Person.saveit() for it to actually insert/save the object).

TL;DR I guess you're question could be put as "Should I save a model's data in the model or in an external data manager class?". My answer to this would be use the external data manager class (DataManager). Just because it makes more sense to have a data manager have an insertPerson() method, than an actual model have that method.

\$\endgroup\$
1
\$\begingroup\$

Disclaimer: I'm the author of ActiveJDBC.

I built quite a few projects with JavaLite over the past years, and the following pattern emerged:

public class Person extends Model {
   public static Person createPerson(String name, Date birthDate, String country) {
      return Person.createIt("name", name, "birthdate", birthDate, "country", country);
   }
}

Generally, we completely avoid building intermediary classes like your PersonData because it just duplicates code for no good reason. We also do not build so-called managers because ActiveJDBC models know how to take care of themselves.

In other words, if the access to a table requires some logic, we would build a static method on a model class. In some cases though, such a method might manage more than one model. If it gets to complicated, this code is migrated to one level up - to a service class for orchestration.

This approach served us well, building applications with schemas ranging from 50 to 200 tables. The simpler the approach, the easier the maintenance.

\$\endgroup\$

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.