Code examples for writing and configuring custom components?

Currently working on getting our Lagom application ready for production and some parts of our “cake” layering doesn’t seem quite right. I’ve isolated our component dependencies (in our case an MQTT client and a storage service implementation) in a MyApplicationComponents trait. The provided Lagom components being so complicated by comparison, I’m having trouble determining how/where I should load configuration for my components. Would it be possible to extend the relevant Lagom docs with some example code of a custom component along with loading a configuration object for that component?

Hi @dkushner,

let me help you first.

I’m having trouble determining how/where I should load configuration for my components.

If your components are configured via applicaiton.conf (aka, Typesafe config), which I recommend, then you could do:

trait MyApplicationComponents {
   def configuration: Configuration 
   // your components here
   // can use the `configuration` field above freely 
}

so when you mix ing your MyApplicationComponents in the Application, Lagom will provide the configuration instance.

Would it be possible to extend the relevant Lagom docs with some example code of a custom component along with loading a configuration object for that component?

It’d be great if you could create an issue, thanks!

Ignasi

Thanks for the quick response! Will submit a ticket later this evening. The code sample you gave is roughly how we’re doing things, thankfully. My confusion came from browsing other Lagom projects and seeing varying patterns for config loading. Some preferring to load it in the components definition and pass it directly to the instance like:

trait MyApplicationComponents {
   def configuration: Configuration 
   def myComponent = new MyComponent(configuration)
}

While others prefer to wrap it first in a scoped settings object.

trait MyApplicationComponents {
   def configuration: Configuration 

   def myComponentSettings = MyComponentSettings(configuration)
   def myComponent = new MyComponent(myComponentSettings)
}

This is just a really minor issue over best practices/preferred style so I hesitated to create an issue over it.

1 Like

My confusion came from browsing other Lagom projects and seeing varying patterns for config loading. Some preferring to load it in the components definition and pass it directly to the instance

Ah, I see.

That’s a matter of style but I personally prefer your second alternative, too:

trait MyApplicationComponents {
   def configuration: Configuration 

   def myComponentSettings = MyComponentSettings(configuration)
   def myComponent = new MyComponent(myComponentSettings)
}

That approach is definitely more typed and makes MyComponent easier to test.