How ObboStore Makes B2B Mindblowingly Easy

How does ObboStore help you customize your digital b2b storefront from scratch? Read our latest article to find out more!

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




How to NOT use Dagger 2 when you have Redux

People often remember Redux for its state management. But surprisingly overlooked by many, it also uses injection to map its states to a view. The following is an example about how a React.js TodoList view receives a to-do list.

So a question raises, what if, alongside states (a.k.a. global variables) injection, the dependencies are also injected in the Redux way. Isn’t it a much neater way than to construct an elaborative dependency annotation, using @Provider, @Inject, @Modules, @Components, etc, etc as in Dagger 2.

2. The REST Api dependency.

Two simple weather APIs are integrated to first fetch a city’s woeid , then use the woeid for the city’s current temperature. They can be found in WeatherApi.swift and WeatherApi.kt

3. The Redux Store, State, Reducer and Action

As a simple demo, the WeatherRedux.kt and WeatherRedux.swift contain the actions, a reducer, app states and a store

4. The SideEffect class

Based on Redux, a side effect performs an asynchronized operation to fetch some data, then call store.dispatch to save the data into store.

The side effect uses WeatherApi to perform the async calls.

5. Injection to a view

The newState will be called both on view start and on a state change in the store. Here, the WeatherApi dependency is injected into the SideEffect.

6. Hola!🎉

That’s all you need to add a DI into your code

We spent all the time above to discuss how to implement DI in a mobile app. But why we need the DI in the first place? 😅

The most important reason for DI is to make each component of an app readily available for independent unit testing. DI can decouple a component away from its dependencies. Then in the test time, mocked dependencies can be injected instead, in our case, a FakeWeatherApi in the place of a real WeatherApi.

iOS unit test setup
Android androidTests setup

2. Execute a unit test

Add a comment

Related posts:

Long Payment Terms Squeeze Small Business

One of the biggest headaches for small businesses is being paid late. Both the administrative costs and mental toll associated with chasing up receivables can take valuable time away from pursuing…

How to Make Seed Pots from Newspaper and a Can

I am tired of buying so many jiffy pots every damn year to start my seedlings, so this year I decided that I would make my own. I had a bunch of newspaper saved up (because who doesn’t save…

Be The Odd One Out And Stand Out.

Be The Odd One Out And Stand Out.. “Be The Odd One Out And Stand Out.” is published by Kaz Van Der Vaart.