this post was submitted on 10 Nov 2024
14 points (93.8% liked)
Rust
5999 readers
23 users here now
Welcome to the Rust community! This is a place to discuss about the Rust programming language.
Wormhole
Credits
- The icon is a modified version of the official rust logo (changing the colors to a gradient and black background)
founded 1 year ago
MODERATORS
you are viewing a single comment's thread
view the rest of the comments
view the rest of the comments
So your answer is "you are doing it wrong".
Thank you, very helpful ๐
That's not what I said. Read about the XY problem and then come back and explain what you actually want to achieve, and give some more information like code examples.
Well. I did read up on the "XY" problem and apparently you assume that what I want to do is somehow bad practice.
To elaborate more on the problem: I am writing an axum backend which (like other backends) needs to do stuff in the database. As some endpoints update the database (and sometimes over multiple sql statements) I want to pass around the transaction as this embodies the connection I am using to update the database.
To separate the axum stuff (parameters, urls and whatnot) from the actual database logic, I've first pulled out all the database interactions into separate functions. Because those functions are logically groups (e.g. stuff happening with invoices, others with contacts etc), I thought it was a good idea to create a "dao" struct (and agreed: my OO brain kicked in here which might be debatable). This would group the interactions for each logical domain into a short-lived data access struct.
To prevent passing around the transaction/connection, i wanted to pass that along during construction, so the functions in the DAO could just access the connection when needed.
Non "OO" would be fine to be honest (just having all the DAO as functions in a separate package).
Sorry, no code, not at the computer atm.
(And yes, I am aware that rust is not OO, put away the pitchforks please ๐)
If I understand correctly, each data-access structure represents single workflow, and you may have Transaction in that structure without the need for Arc<Mutex<โฆ>> inside, but maybe you will need to wrap the structure itself with that.