objectbox / objectbox-java

Android Database - first and fast, lightweight on-device vector database
https://objectbox.io
Apache License 2.0
4.35k stars 300 forks source link

ObjectBox on multi-module project #588

Open JuanFcoMiranda opened 5 years ago

JuanFcoMiranda commented 5 years ago

I have a project with a app and multi modules(app, domain, data).

I created an Application in module-app, init objectbox in the module app application, for regenerate the database from some json files.

My entities are in the domain module, and some repositories in the data module.

My problem is that MyObjectBox class is not generated.

How should I make it work?

abdurahmanadilovic commented 5 years ago

I also have a multi module project with similar structure. Before MyObjectBox is generated you need to build the project (Make -> Build Project) after you annotate your DB classes with @Entity annotation.

JuanFcoMiranda commented 5 years ago

I also have a multi module project with similar structure. Before MyObjectBox is generated you need to build the project (Make -> Build Project) after you annotate your DB classes with @entity annotation.

I tried that, but it didn't generate MyObjectBox class.

greenrobot-team commented 5 years ago

MyObjectBox generation will only work for the module where the ObjectBox plugin is applied and only if the plugin can find @Entity classes in that modules sources.

So it is not possible to have the plugin applied to a module that depends on another module that has the actual @Entity classes.

Edit: The common solution here is to have a separate database for each feature module (define @Entity classes, apply the plugin and supply a name when building the store). This also clearly separates concerns. -ut

JuanFcoMiranda commented 5 years ago

Would it be possible in the future?

greenrobot-team commented 5 years ago

For now we have no concrete plans for this.

Leaving this open as a suggestion. -ut

Gaket commented 5 years ago

Android community runs into modularization pretty quickly now. So it would be great to have this feature.

greenrobot commented 5 years ago

Just to make sure I understood the requirements here. If you would put all entities in one module, and apply the ObjectBox to it to generate MyObjectBox, would that be sufficient?

Gaket commented 5 years ago

It may depend from team to team.

I am inclined to do it that way now and it looks like it works: all the entities in android-library module, and a class providing a BoxStore in the same module. Then, app module takes the BoxStore and provides boxes to other classes using DI.

The small drawback is that initially we wanted to make a module with our modules java-only and generate the MyObjectBox class in the main app module, but it seems impossible for now, because we need to generate a BoxStore in the same module where entities lie and we need a Context as we work in Android project. But that's not a critical thing.

However, I saw that some people try to put different entities into different modules, according to features. I can't comment on that approach.

pgetsos commented 4 years ago

Would love to be able to have entities in one module and be able to use them (create a Box) in another one

shashi-appgolive commented 2 years ago

Keeping all the Entities in one module may not be feasible solution, there will be requirement to have the the Entities defined in their individual modules specially when projects wants to follow the Clean Architecture approach.

Is there a support for this, if not may have to look for alternative..

greenrobot-team commented 2 years ago

@shashi-appgolive It should be fine to have entities in different modules, if you also apply the plugin and have a separate database (e.g. supply a different name when building BoxStore) in each of those feature modules. This will ensure the modules remain independent.

E.g. how would you access BoxStore from each of the feature modules?

greenrobot-team commented 2 years ago

There is now an example Android app that uses multiple feature modules, each with its own ObjectBox database: https://github.com/objectbox/objectbox-examples/tree/main/android-app-multimodule

shashi-appgolive commented 2 years ago

Wonderful, thanks for providing this.

shashi-appgolive commented 2 years ago

I see that there are 2 different DBs created for with names notes and tasks
store = MyObjectBox.builder() .androidContext(context) .name("tasks") .build()

store = MyObjectBox.builder() .androidContext(context) .name("notes") .build()

Screenshot 2022-05-23 at 6 43 28 PM

Is it possible to have one DB name and different entities ? Or is it like we will be having different DBs/Box for different modules ?

greenrobot-team commented 2 years ago

different DBs/Box for different modules ?

This. Each module is completely separate from each other, so also has its own database files.

showwiki commented 2 years ago

different DBs/Box for different modules ?

This. Each module is completely separate from each other, so also has its own database files.

Maybe the key is the MyObjectBox.java produce by gradle plugin, which has a reference of Bean_._INSTANCE, Bean.__INSTANCE has a reference of Bean , the bean of other module is not avaiable for current module,

Maybe the arouter give an example , produce the template java code in every module, through interface to communicate for each module, not the interface implementation . Its arouter-compiler module scan other module annotation or interface implementation through DexClassLoader.

by the way , are the sources of objectbox-processor objectbox-gradle-plugin objectbox-generator avaiable now?