Is there a reason to choose Ktor over Spring?
# ktor
m
Is there a reason to choose Ktor over Spring?
The reason I'm asking such a silly question is that I really like Ktor, but... since Spring has become the standard, I just got curious... I've used both Ktor servers and clients, and I know Ktor is better than Spring... I'm just feeling a little confused right now. Sorry about that.
h
It really depends on your usecase and personal preferences.
h
I think ktor is a lot more lean and less opinionated
m
I feel the same way—there are just too many annotations. That’s why Spring is... Even if it means giving up on getting a job, would it be better to choose Ktor? I’m still in high school.
h
If you are at the point where you are making these kind of decisions while still in high school, I don't think you will have trouble finding a job by the time you graduate college
➕ 5
s
Ktor and Spring have different approaches. Where Spring favors auto-config, and auto-bootstrap through reflection and classpath scanning, and wrapping SDKs, and so on; Ktor favors typesafe DSLs with explicit configuration, and working with 1st party SDKs directly. As a result Ktor is much more lean, and often easier to understand because everything is explicit (depends on experience). We also see it works better in teams that use multiple languages, or for people coming from other ecosystems because Spring is quite unique in its heavy annotation and reflection based bootstrapping. Other languages seem to favor similar approaches like Ktor. Spring also has a couple of more explicit ways of doing things, but afaik they are not so popular.
👍 5
t
I like Spring, but the older I get, the less I prefer all the hidden magic that it provides, it can be difficult to track those features sometimes. For personal projects, I use Ktor, I'm a fan of explicitness.
m
So, I guess I’ll use Ktor for personal projects and Spring for team projects. I’ll stick with Spring for this project, and then switch to Ktor for the next one! It’s too complicated. I really hate Spring’s annotations. In a way, Ktor also uses a DI library called Koin… but I think it’s better because I can control it myself. Thanks, everyone, for your replies! If anyone has a Ktor project in their personal open-source repo, please share a link—I’m curious about the structure.
h
Ktor has a native di library. You don't need to use koin.
m
koin is good..
👎🏾 1
s
I don't use any DI frameworks 🙃 and I have used Ktor in production for 7 years in enterprise systems.

youtube.com/watch?v=JOZFZ__3M7Q▾

👍 3
s
I hate DI frameworks, they turn an app into a spaghetti ball overtime. With KTor, the code is more readable and it is easy to refactor.
m
So, what kind of system do you all use in KTOR and how?
s
I don't use a DI framework, just ol' good composition root pattern.
🙌 2
m
Can I see ur code?
I used to create a separate KOIN and then process it in one place to bring it to Inject.
s
personally, I like ktor because it felt nicer to use in kotlin compared to spring different ways of doing things also I hate annotation magic
I have found though that if smth gets larger then it can be beneficial to use DI though I don't really like koin, because it uses globals which it tries to hide from you. personally, I prefer kodein where you pass around a
DI
object I have found ktor's DI to be lacking whenever you're working outside of ktor & don't have a reference to `Application`/etc. e.g. you have a data model class which wants to call methods in some service which it gets via DI but for the things which are larger where I'd want DI but simple enough where I don't need that, then ktor's DI is nice
m
@solonovamax sorry ping I used to build a lot of my setup around Koin. While I liked that I could keep adding things with
get()
, it was a bit of a hassle because there were no units. Managing everything in one place was also a bit inconvenient. Do you know of anything better? Also, could I take a look at your code? I'm just curious.
r
@solonovamax Koin has option to create an instance object that you can pass around (instead of relying on the default global static). See: insert-koin.io/docs/reference/koin-core/context-isolation