2

Currently when using Rust Rocket framework it is necessary to get your database connection via the controller. Basically the connection is given to your handler from a pre-configured pool. Now we have to pass this connection down into any struct which needs a database connection.

If I would like to separate the concerns of reading from the data store, or potentially multiple data stores if caching is involved as well, then I will have to pass one, or potentially multiple, different connection structs from my handler into the lower layer.

While I am aware that I can encapsulate all of the connections into a single request guard, I am dissatisfied by lack of abstraction. I feel quite strongly that my handler should know nothing about the database in order to keep the concerns as separate as possible.

How would I proceed in Rust to obtain a connection from some shared pool of connections in an object, without usage of request guards and argument drilling?

Note: Terminology may be incorrect due to rather limited experience with Rocket

TheCoolDrop
  • 796
  • 6
  • 18
  • what ? rocker use a state to do this, why are you using guard ??? – Stargateur Dec 04 '21 at 18:19
  • I acknowledge that I may have used wrong terminology. If my intent was not clear please inform me. I just do not want to get the connection in handler, rather in the struct which is focused on connecting with database and retrieving data. – TheCoolDrop Dec 04 '21 at 18:46
  • well make a wrapper instead of managing the database connection manage your wrapper that have the database connection https://rocket.rs/v0.4/guide/state/ – Stargateur Dec 04 '21 at 18:48
  • I still get that wrapper passed into my handler, and I still have the same problem with argument drilling, but now with different type. – TheCoolDrop Dec 04 '21 at 18:49

0 Answers0