New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add shorthand sessionOf and transactionOf #38
Conversation
I think |
Sorry for my late response here. |
@@ -26,6 +26,18 @@ fun sessionOf(dataSource: DataSource, returnGeneratedKey: Boolean = false): Sess | |||
return Session(Connection(dataSource.connection), returnGeneratedKey) | |||
} | |||
|
|||
fun <T> sessionOf(dataSource: DataSource, returnGeneratedKey: Boolean = false, operation: (Session) -> T): T { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using the same name sessionOf
here is very confusing. Is there a better name for this? I think withSession
may be okay but I'm not so confident.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How, about changing it to extension functions:
fun <T> DataSource.session(...
fun <T> DataSource.transaction(...
Usage would be now:
val db: DataSource
db.session {
}
db.transaction {
}
I agree that reusing the name sessionOf
might be confusing. Furthermore, when I used it in my project I noticed that IDE has slight problems with finding proper imports.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about having use
method for AutoCloseable
as with kotlin.io.use
?
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.io/use.html
Or, I haven't checked in detail yet but if implementing both Closeable
and AutoCloseable
is fine, we can just use kotlin.io.use
by implementing Closeable
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That would also be awesome too - maybe even better. Though, I would still like to see a shorthand function like the transactionOf
above.
b5bb9ff
to
e0260fe
Compare
Let me close this PR. Please consider going with your own utility for shorthand. |
Additional shorthand
sessionOf
that doesn't need to be wrapped withusing
.Plus
transactionOf
as often times you need a transaction for the whole session and with this you don't need to nest transaction block in session block.With this I believe it cannot get any better with the amount of control and readability it offers: