• 32 Posts
  • 1.46K Comments
Joined 2 years ago
cake
Cake day: April 27th, 2023

help-circle








  • Definitely let go. Rust has some OOP features, but it’s mostly just the OOP idea of interfaces, which Rust models with traits. You can also do dynamic dispatch, which is another OOP feature, but you should almost never use this in Rust unless you absolutely have to. Then there’s encapsulation which is hugely important in Rust too, but yea outside of that kind of thing, I don’t think OOP patterns are too useful. Honestly, if you ask me, many of these “OOP patterns” are really just solving problems that OOP causes in the first place.

    Feel free to ask any other questions.


  • Thanks for explaining further, it’s a lot clearer now what you want to do. And no, I don’t think this DAO thing is idiomatic for Rust and you probably don’t want to do it like that. I’m not familiar with the pattern though, I’m not too much into OOP myself.

    Anyways, I’ve worked a lot with axum and sqlx before so I can tell you what I’d do.

    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.

    This makes sense. You just want a database connection pool (sqlx provides this) in your axum state so your handlers can get connections to 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.

    Again, not sure what this DAO struct actually entails, but what I would do and have done in the past is just do exactly what you said before: “I want to pass around the transaction”. So I would make my functions take the Transaction struct from sqlx (IIRC it has some type parameters and a life time but you can use a type alias to make it less verbose) and then I would just use that transaction in the function to call SQL. If you have a function that needs access to the database but doesn’t need a transaction, you can just use a plain connection instead of a transaction.

    To prevent passing around the transaction/connection, i wanted to pass that along during construction

    I’m not sure what you mean with “pass along during construction” but in any case, why do you want to avoid passing the transaction/connection? I feel like that is exactly what you should do. That is what you need to do anyway. Rust favours explicitness and you need to pass the transaction/connection to functions for them to use it, so just pass it.






  • On the other hand, it has some weirdly opinionated features:

    • Hiding downvoted comments (mob rule)
    • Marking people with many downvotes as “low reputation”. I get it, getting many downvotes is a bad sign but I don’t think the software should try to make a ruling here, I think human moderators should look at the whole picture. It doesn’t make you a bad person that people disagree with you.
    • Communities organized into “topics” - I’m not certain if these groupings are decided by the dev or the admin? Either way I find it a bit problematic.
    • Marking certain communities as “low effort” and not counting “reputation” for those. I don’t feel like the software should be making this kind of value judgement.