Using modules inside services

14 views
Skip to first unread message

Hyun Joon Seol

unread,
Jul 12, 2017, 1:57:28 AM7/12/17
to finatra-users
I'm fairly new to Finatra and injection in general, so I may not have the right concept here. 

I have a service that does some application logic. The interface is defined as

trait Foo {
  def foo(): Unit
  def bar(): Unit
}

And let's say I have some different implementations of Foo. I understand I can use `.bind` inside modules to bind specific implementations of Foo.

One of these implementations uses postgres. So before I write the application logic for that implementation, I want to have a module that just deals with postgres connections, so I think I need a module that @provides a connection. I've written that part.

And now I want to write the implementation. Should the implementation be another module, or should it be something like a `service` i.e. https://github.com/twitter/finatra/blob/develop/examples/twitter-clone/src/main/scala/finatra/quickstart/services/TweetsService.scala ?

If the implementation should be a service, how can I inject a module inside that service? will using @inject suffice? What would be the most "Finatra" way of implementing this?

Yufan Gong

unread,
Jul 12, 2017, 1:40:44 PM7/12/17
to Hyun Joon Seol, finatra-users
Hi Hyun,

I think you are able to write each implementation as a service, not sure if this is the most "Finatra" approach though. You could take a look at RedisUrlShortenerService(https://github.com/twitter/finatra/blob/develop/examples/tiny-url/src/main/scala/com/twitter/tiny/services/impl/RedisUrlShortenerService.scala). In this example, it also uses @Inject for JedisClient which is provided in JedisClientModule(https://github.com/twitter/finatra/blob/develop/examples/tiny-url/src/main/scala/com/twitter/tiny/modules/JedisClientModule.scala), so I think it is doable as long as you add your postgresClient module to the additional modules sequence.

Thanks!

--
You received this message because you are subscribed to the Google Groups "finatra-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to finatra-users+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Hyun Joon Seol

unread,
Jul 12, 2017, 9:34:17 PM7/12/17
to finatra-users, hyunjo...@gmail.com
@Inject actually worked like a charm. Thanks for your help!
To unsubscribe from this group and stop receiving emails from it, send an email to finatra-user...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages