匕首线切割图纸下载_匕首击剑简介

匕首线切割图纸下载

Dependency injection (DI), in a nutshell, is a technique whereby one object provides or supplies the dependencies of another instead of creating it by themselves. This will let you have good architecture for your application and better code reusability.

简而言之, 依赖注入(DI)是一种技术,通过该技术,一个对象可以提供或提供另一个对象的依赖关系,而不必自己创建。 这将使您的应用程序具有良好的体系结构,并具有更好的代码可重用性。

In Android, Dagger is the dependency injection library that is recommended by Google for dependency injection. But let’s face it, there are a lot of overheads when setting up Dagger that beginners might shy away from.

在Android中, Dagger是Google推荐用于依赖项注入的依赖项注入库。 但让我们面对现实吧,设置Dagger时会有很多开销,初学者可能会回避。

This is where Dagger Hilt for Android comes in.

这是Android版Dagger Hilt出现的地方。

什么是匕首剑? (What is Dagger Hilt?)

Hilt, built on top of Dagger, is now the new recommended DI library from Google which would help to simplify setting up Dagger in your applications.

Hilt建立在Dagger之上,现在是Google推荐的 DI库,它将有助于简化在应用程序中设置Dagger的过程。

The previous Components that we created in our app, exists now in the predefined components that are being provided to us, out of the box, by Hilt. So we won’t need to define these directly. The built-in components are also automatically integrated into the lifecycle of your app, which is really neat.

我们在应用程序中创建的以前的Components现在存在于Hilt提供给我们的预定义组件中。 因此,我们不需要直接定义它们。 内置组件还可以自动集成到您应用程序的生命周期中,这确实很整洁。

To check out these components, take a look here.

要检查这些组件,请在此处查看

设置 (The Setup)

We first need to add the following to our project level build.gradle

我们首先需要将以下内容添加到我们的项目级别build.gradle

classpath 'com.google.dagger:hilt-android-gradle-plugin:2.28-alpha'

Then in our module level build.gradle

然后在我们的模块级别build.gradle

apply plugin: 'dagger.hilt.android.plugin'

Finally, we can now add the required dependencies for us to use Hilt

最后,我们现在可以添加所需的依赖项以供我们使用Hilt

implementation 'com.google.dagger:hilt-android:2.28-alpha'
kapt 'com.google.dagger:hilt-android-compiler:2.28-alpha'
implementation 'androidx.hilt:hilt-lifecycle-viewmodel:1.0.0-alpha01'
kapt 'androidx.hilt:hilt-compiler:1.0.0-alpha01'

When everything is synced up, we now need to annotate our application class with @HiltAndroidApp, this will generate the different Hilt components along with the base class of our application.

当所有内容同步后,我们现在需要使用@HiltAndroidApp注释应用程序类,这将生成不同的Hilt组件以及应用程序的基类。

For our Activity/Fragment we would annotate it with@AndroidEntryPoint, this will generate the required component that will receive the dependencies that are going to be injected in our Activity/Fragment.

对于我们的活动/片段,我们将使用@AndroidEntryPoint对其进行@AndroidEntryPoint ,这将生成所需的组件,该组件将接收将注入到我们的活动/片段中的依赖项。

In cases that we have a class that has a required constructor argument

如果我们有一个带有必需的构造函数参数的类

class SampleClass (val anotherSampleClass: AnotherSampleClass) {}

We can use Hilt modules to inject the required argument. Take a look at the sample below.

我们可以使用Hilt模块注入所需的参数。 看下面的示例。

@Module
@InstallIn(ActivityComponent::class)
class SampleModule {
@Provides
fun provideA
}

Hilt reuses Dagger’s @Module annotation but in Hilt, instead of specifying which modules the component will use, we specify in each module that we create in which component it will be installed in, @InstallIn(...Component::class) and we would still use @Provides and @Binds annotation as to how we use to before.

Hilt重用了Dagger的@Module批注,但在Hilt中,我们没有指定组件将使用的模块,而是在我们创建的每个模块中指定了将要安装在哪个模块中的@InstallIn(...Component::class)然后仍然会使用@Provides@Binds注释来说明之前的用法。

视图模型 (ViewModel)

Any ViewModel that is annotated with @ViewModelInject constructor() can be provided from the ViewModelFactory of Hilt. This will generate, behind the scenes, the respective factories. The ViewModel can also receive the SavedStateHandle using the annotation @Assisted, without any more configuration. Sweet!

可以从Hilt的ViewModelFactory提供使用@ViewModelInject constructor()注释的任何ViewModel。 这将在后台生成各个工厂。 ViewModel也可以使用注释@Assisted接收@Assisted ,而无需进行任何其他配置。 甜!

class SampleViewModel @ViewModelInject constructor(
val argument: AnotherSampleClass,
@Assisted val savedStateHandle: SavedStateHandle
) : ViewModel() {}

And in our Activity/Fragment, we can get the of the view model by ktx extension function.

在Activity / Fragment中,我们可以通过ktx扩展功能获得视图模型的。

@AndroidEntryPoint
class SampleFragment : Fragment() { val viewModel by viewModels<SampleViewModel>()
}

结论 (Conclusion)

With what we’ve just seen above, we have set up our Android app with DI with the use of Dagger-Hilt. It surely reduced the boilerplate in setting up the whole dependency injection in Android which was quite cumbersome before Hilt came along.

通过上面的介绍,我们使用Dagger-Hilt通过DI设置了Android应用。 它肯定减少了在Android中设置整个依赖项注入的样板,这在Hilt出现之前非常麻烦。

It allowed us to be up and running the DI in no time and with less code. It hides the complexity from us, developers, to let us focus on building other important things.

它使我们能够立即使用更少的代码来启动和运行DI。 它使我们(开发人员)看不到复杂性,让我们专注于构建其他重要事物。

翻译自: https://medium.com/swlh/a-brief-intro-to-dagger-hilt-979dce3c8788

匕首线切割图纸下载

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值