1

For my job I'm trying to convince my colleague to use ORMLite (just found it) instead of writing our own cumbersome and error-prone beast of DB-accessor code. I've got about 2 hours to get this thing to work so I hope you guys can help me out here!

So I'm now trying to make sense of an example app I found here. As far as I understand you need to create a class for every model/table you will have. In the example there's a Person.java and an App.java file. In these files you need to manually create all the getters and setters for every field. So far so good.

Next to these files I've got the folloowing ones:

  • DatabaseHelper.java
  • DemoRepository.java
  • PersonAdapter.java
  • DemoORMLiteACtivity.java

My questions are:

  1. What is a repository in this sense? Why is it needed? I'm looking through the code and I can't really make sense out of it.
  2. Furthermore, would I need to create all these files myself as well, or are some of these auto-generated?
  3. If I would need to create all these files myself, what's the advantage of using an ORM, except for not needing the couple lines of SQL?
Cœur
  • 37,241
  • 25
  • 195
  • 267
kramer65
  • 50,427
  • 120
  • 308
  • 488

1 Answers1

1

What is a repository in this sense? Why is it needed? I'm looking through the code and I can't really make sense out of it.

Uh the link you provide describes it pretty well:

The repository layer wraps all ORMLite calls hiding any exceptions and code relating to all database operations.

So the "repository" is what provide the specific business logic methods that in turn call the data-access-object or DAO methods. It is the utility class through which you make calls to the database. In their example they have deletePerson(...) which does a bunch of database operations to delete a Person and their associated App entries.

The DAO classes provide all of the entity agnostic queryById(...) type of methods. It looks to me that the repository class adds very specific functionality for the specific application in question.

If you look at ORMLite HelloAndroid example code, you can see that it doesn't have a repository class so it's not a requirement.

Furthermore, would I need to create all these files myself as well, or are some of these auto-generated?

You will need to create the entity files yourself and the database helper. The database helper is the class that handles the onCreate(...) and onUpgrade(...) methods that you probably already have. There is a base class OrmLiteSqliteOpenHelper to help with that.

If I would need to create all these files myself, what's the advantage of using an ORM, except for not needing the couple lines of SQL?

There are a number of reasons why ORMs are a good idea. For me it's about extensibility. Sure it's a "couple of lines of SQL" now but unless you have no plans for growth, the couple of lines can get complicated pretty fast. With an ORM, you manage your entities and you don't worry about the database.

Here's some more information:

Community
  • 1
  • 1
Gray
  • 115,027
  • 24
  • 293
  • 354