swift 依赖请求_如何做基于Swift的依赖注入参数

swift 依赖请求

Dependency Injection feel like a very complicated topic but in fact it is very simple. We as devs all try to follow design principle like SOLID so that our code is properly structured in a modular way and not a spaghetti code. Dependency injection really help with this. It also helps in mocking/testing our code.

依赖注入感觉像一个非常复杂的主题,但实际上它很简单。 我们所有开发人员都尝试遵循SOLID之类的设计原则,以便我们的代码以模块化的方式(而不是意大利面条式代码)正确地进行结构化。 依赖注入确实对此有所帮助。 它还有助于模拟/测试我们的代码。

Dependency injection literally means injecting the dependency. In programming world it means injecting the dependencies our code needs in order to work. There are three flavors of dependency injection in Swift

依赖项注入从字面上意味着注入依赖项。 在编程世界中,这意味着注入代码才能正常工作。 Swift有三种类型的依赖注入

1. Initializer Based

1. 基于初始化

2. Property Based

2. 基于属性

3. Parameter Based

3.基于参数

We already covered Initializer Based Swift Dependency Injection and Parameter Based Swift Dependency Injection . In this blog we will focus on Parameter Based Swift Dependency Injection. Parameter based Dependency Injections is suitable for static classes where our static class method/api can’t have initializers. Our api calls can have some internal dependencies. Let’s take a look at example of that.

我们已经介绍了基于初始化的Swift依赖注入基于参数的Swift依赖注入 。 在此博客中,我们将重点介绍基于参数的Swift依赖注入。 基于参数的依赖注入适用于静态类,其中我们的静态类方法/ api无法具有初始化程序。 我们的api调用可以具有一些内部依赖性。 让我们看一个例子。

If you see above FileUploader’s class the static method/api calls has two internal dependency. To make this api call easily testable we need to be able to pass dependency from outside. Considering this function/api being static we can’t have initializer, this is where parameter based dependency injection comes handy. We can modify the api call/function to take dependency as parameter. Let’s take a look how it looks after the change.

如果您在FileUploader的类上方看到,则静态方法/ api调用具有两个内部依赖项。 为了使此api调用易于测试,我们需要能够从外部传递依赖关系。 考虑到此函数/ api是静态的,我们不能使用初始化程序,这是基于参数的依赖注入方便的地方。 我们可以修改api调用/函数以将依赖项作为参数。 让我们看一下更改后的外观。

Swift Dependency Injection Parameter Based

So by making this minor change we made our upload api call easily testable/mockable. We can go a step forward and make default arguments for the api call.

因此,通过进行此较小的更改,我们使上传api调用易于测试/可模拟。 我们可以前进一步,并为api调用设置默认参数。

Swift Dependency Injection Parameter Based

结论 (Conclusion)

After covering different options for dependency injection in swift, whats the one option that we should always use?. I would say it depends on your use case. There is no one size fits all thing. I would love to hear your comments on dependency injections and your use cases.

在Swift介绍了依赖项注入的不同选项之后,我们应该始终使用的一个选项是什么? 我会说这取决于您的用例。 没有一件适合所有事物的尺码。 我很想听听您对依赖注入和用例的评论。

Originally published at https://www.shashankthakur.dev.

最初发布在 https://www.shashankthakur.dev

翻译自: https://medium.com/@shashank.thakur/how-to-do-swift-dependency-injection-parameter-based-fa19928a6e4e

swift 依赖请求

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值