hey, is it somehow possible to use a spring servic...
# spring
c
hey, is it somehow possible to use a spring service on file level / top level declaration? to be more clear here is a little example code to explain what i am trying to archive:
Copy code
// lets assume i have some basic crud repository like:
interface MyFunkyRepository : CrudRepository<MyFunkyEntity, String>

@Entity(name = "funky")
@Table(name = "funky")
class MyFunkyEntity(
    @Id
    var id: String = UUID.randomUUID().toString(),

    @Column(name = "title", nullable = false)
    var title: String,

    @Column(name = "description", nullable = false)
    var description: String,
)

// and i have a service that is using the repository
@Service
class MyFunkyService(
    private val myFunkyRepository: MyFunkyRepository,
) {
    fun funkyAmount() = myFunkyRepository.count()
    fun addFunkiness(f: MyFunkyEntity) = myFunkyRepository.save(f)
}

// now i would like to write a little internal DSL to add entries to the repository by using the service
@DslMarker
annotation class MyFunkyDsl

@MyFunkyDsl
fun <T> addFunkyStuff(init: MyFunkyEntity.() -> T): T {
    // TODO: somehow inject MyFunkyService and store fields set by init callback
}

// so i could just use 'addFunkyStuff' lambda anywhere in my code like this:
class UseDslExample {
    fun someFunction() {
        addFunkyStuff {
            title = "xyz"
            description = "abcd"
        }
    }
}
is this possible in any way or what could be an alternative approach to write an DSL for a use-case like that?
1
t
don't think so, toplevel decalrations will always compile into something
static
c
yah that makes sense, i update the question with a code example to be more clear. but i guess you are right
m
What I Have is something like this already done.. https://gist.github.com/marzelwidmer/5f8766efc425d15b07fe08ca99b91bd2 I am not sure if you mean this.
Copy code
// Kotlin DSL
countries(
    country {
        name = "Switzerland"
        capital = "Bern"
        population = 8_603_900
        currency = "CHF"
    },
    country {
        name = "Spain"
        capital = "Madrid"
        population = 47_100_396
        currency = "EUR"
    }
)
c
ok, i see. thx for providing the example. i will give it a try 🙂
👍 1
m
is based on

https://www.youtube.com/watch?v=zYNbsVv9oN0

and

https://www.youtube.com/watch?v=hYXAFO3q3qU