Skip to content
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

Closed
wants to merge 1 commit into from

Conversation

Koriit
Copy link

@Koriit Koriit commented Jan 17, 2020

Additional shorthand sessionOf that doesn't need to be wrapped with using.

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:

val ds : DataSource by instance() // kodein

sessionOf(ds) { session ->
  // ...
}

transactionOf(ds) { tx ->
  // ...
}

@Koriit
Copy link
Author

Koriit commented Jan 17, 2020

I think install-jdk.sh doesn't support JDK8 anymore.

@seratch
Copy link
Owner

seratch commented Jun 3, 2020

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 {
Copy link
Owner

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.

Copy link
Author

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.

Copy link
Owner

@seratch seratch Jun 3, 2020

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.

Copy link
Author

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.

@seratch
Copy link
Owner

seratch commented Feb 24, 2022

Let me close this PR. Please consider going with your own utility for shorthand.

@seratch seratch closed this Feb 24, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants