Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I should use Ebean or EbeanServer under what case?

I know I can use Ebean or EbeanServer to access a database. EbeanServer has more API methods than Ebean, Many methods on Ebean such as Ebean.find(Class) etc are actually just a convenient way to call methods on the 'default/primary' EbeanServer.

My questions:

  • Can I get EbeanServer object by calling Ebean.getServer(String name) to use the all apis provided by EbeanServer?
  • In what case should I use Ebean over EbeanServer (or vice versa)?
  • EbeanServer is suitable for creating EbeanServer programmatically?
  • Ebean is suitable for creating EbeanServer by config file?

In Play 2.4, ebean.properties is not needed, ebean configuration can be put in application.conf, I think I should use Ebean in Play framwork 2.4, right?

Thanks for your advice.

EbeanServer api doc

Ebean api doc

like image 214
deezh Avatar asked Feb 09 '23 08:02

deezh


1 Answers

Firstly one way to look at it is that Ebean holds a register/map of EbeanServer instances and one of those instances can be defined as the default server.

However there is NO REQUIREMENT to use Ebean or to register an EbeanServer instance. That is, EbeanServer instances can be created and NOT registered with Ebean and in this way you never need/use/want Ebean (more pure DI style).

Said differently - EbeanServer is the real/actual implementation and you MUST have an EbeanServer instance. You can OPTIONALLY register an EbeanServer instance with Ebean (most people do this) and if you do register the EbeanServer instance then it will be available to be used by the Ebean singleton and the methods on Ebean are quick shortcuts.

You can use both and this is reasonable in a dependency injection scenario (refer to http://ebean-orm.github.io/docs/setup/guice). So you create the EbeanServer instance using a Guice provider or Spring factory bean AND have that instance registered with the Ebean singleton. In this way you can both @Inject EbeanServer instances using DI (Guice/Spring) and at the same time obtain the exact same EbeanServer instance via Ebean.getDefaultServer() (and in this way also use Model/Finder/Ebean ala active record).

So onto your questions:

Q: EbeanServer is suitable for creating EbeanServer programmatically?

EbeanServerFactory is used to create EbeanServer instances. Refer to http://ebean-orm.github.io/docs/setup/serverconfig

Q: Can I get EbeanServer object by calling Ebean.getServer(String name) to use the all apis provided by EbeanServer?

Yes assuming the EbeanServer instance is registered (which is optional). Refer to ServerConfig.setRegister(boolean) and ServerConfig.setDefaultServer(boolean).

Q: In what case should I use Ebean over EbeanServer (or vice versa)?

In all cases you technically are using EbeanServer instance so really this is a question of style. You can optionally access the EbeanServer instance via the Ebean singleton when the instance is registered as the default server etc.

In what I would call a "traditional dependency injection" style you would not use Ebean at all. EbeanServer instances are injected into you services using Guice/Spring etc.

In what I would call a "active record" style you like the simplicity and relatively clean code that results from using Model/Finders/Ebean. I discuss this style at http://ebean-orm.github.io/docs/setup/activerecord

In summary the choice between using Ebean or EbeanServer is a question of style. I'd describe these as "traditional DI" and "active record" styles.

Now I also need to point out that in the DI case you can use both styles if you want. You can both @Inject the EbeanServer instance and access it via Ebean to enable the use of Model/Finders and active record style. This is reasonable in the DI case where you want programmatic configuration of the EbeanServer instance but you still want to be able to use the active record style via Model and Finders.

TESTING:

Testing also comes into this. The avaje-ebeanorm-mocker project specifically supplies the mechanisms to mock out EbeanServer instances when they are used via Ebean/Model/Finder so in using avaje-ebeanorm-mocker that gives the same level of testablity that you would otherwise get when using traditional injection of EbeanServer.

Q: Ebean is suitable for creating EbeanServer by config file?

Well yes and no. To me you start by saying "Are we using DI/Guice/Spring" and generally if you are then you have a guice provider/spring factory bean to create the EbeanServer instance (because it is good to have programmatic configuration with more advanced EbeanServer features - e.g. inject in a CurrentUserProvider implementation). You then additionally chose if you want to register the EbeanServer instance with the Ebean singleton and in that way additionally be able to use Model/Finder/Ebean.

In terms of config file, my thought is that even in programmatic configuration it is good to also enable the use of external properties configuration so you first serverConfig.loadFromProperties(properties) and then after that programmatically set things into serverConfig. This gives you the ability (even if you don't use it initially) to have external environment specific configuration via properties/file.

If you don't use dependency injection at all (small simple app etc) then Ebean and ebean.properties is a pretty fast simple mechanism to use.

Hopefully that helps with some of your questions.

Cheers, Rob.

like image 137
Rob Bygrave Avatar answered Mar 08 '23 02:03

Rob Bygrave