如何通过koin中的参数传递界面

发布于 2025-01-20 06:23:39 字数 749 浏览 4 评论 0原文

我在Koin的初学者。

我有一种名为“ mesherepoimpl”的方法,将接口作为参数。

我知道我无法将接口传递到Koin中的方法,因此我创建了一个类并从接口中扩展了该类,然后在Koin模块中添加了该类,因此我将类用作MesherePoimpl的参数。

但是Android Studio给了我这个错误:

由:org.koin.core.error.nobeandeffoundexception:| - 类别没有定义:'com.app.meshe.data.repo.mesherepo'。检查您的定义!

这是我的DI模块:

val mesheModule =
    module {
        single { getInstance(androidContext()) }
        single { MesheLocalDataSource() } //*
        single { MesheRepoImpl(get()) } //**
        factory { TaskViewModelFactory(get()) }
        viewModel { TaskViewModel(get()) }
        viewModel { RewardViewModel(get()) }
        viewModel {MainViewModel()}
    }

1星线是我的类,它从接口延伸,2星线是将接口作为参数的类。

如果我不能使用类,该如何将接口作为参数传递?

I'm so beginner in koin.

I have a method that named "MesheRepoImpl" that get an interface as parameter.

I know that I can't pass an interface to a method in Koin, so I created a class and extends that from the interface then I added that class in koin module, so I use the class as parameter for MesheRepoImpl.

But android studio gives me this error:

Caused by: org.koin.core.error.NoBeanDefFoundException: |- No definition found for class:'com.app.meshe.data.repo.MesheRepo'. Check your definitions!

This is my Di module:

val mesheModule =
    module {
        single { getInstance(androidContext()) }
        single { MesheLocalDataSource() } //*
        single { MesheRepoImpl(get()) } //**
        factory { TaskViewModelFactory(get()) }
        viewModel { TaskViewModel(get()) }
        viewModel { RewardViewModel(get()) }
        viewModel {MainViewModel()}
    }

The 1 star line is my class that extends from the interface and the 2 stars line is the class that get interface as parameter.

How can I pass the interface as parameter, if I can't use a class?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

弱骨蛰伏 2025-01-27 06:23:40

由于仍然没有答案,我建议您考虑继续

interface MesheRepo
class MeshoRepoImpl(): MeshoRepo

您的

interface MesheRepo
class MeshoRepoImpl(val IRepo: MeshoRepo)

所以,只需实现 MeshoRepo 而不是将其作为参数传递给 MeshoRepoImpl

尝试直接回答您的问题,您可以在 Koin 模块中定义接口并传递它们,但您还必须提供它们的实现:

val mesheModule = module {
  single<MeshoRepo> { MeshoRepoImpl() }
  single { MeshoRepoImpl(get()) } // <-- it's like a deadlock, so I still do not see any sense to pass an interface over implementing it
}

并且,请不要忘记接口不是对象。

Since there's still no answer, I'd advise you to consider going with

interface MesheRepo
class MeshoRepoImpl(): MeshoRepo

over your

interface MesheRepo
class MeshoRepoImpl(val IRepo: MeshoRepo)

So, just implement MeshoRepo over passing it as an argument to MeshoRepoImpl.

Trying to answer directly your question, you are able to define interfaces in Koin module and pass them, but you have to provide their implementations, as well:

val mesheModule = module {
  single<MeshoRepo> { MeshoRepoImpl() }
  single { MeshoRepoImpl(get()) } // <-- it's like a deadlock, so I still do not see any sense to pass an interface over implementing it
}

And, please, do not forget that an interface is not an object.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文