Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Dependency Injection

Daniel
April 02, 2020

Dependency Injection

Talk about dependency injection, not frameworks, just things around dependency injection.

Examples -> https://github.com/la-haus/la-haus-android

Daniel

April 02, 2020
Tweet

More Decks by Daniel

Other Decks in Programming

Transcript

  1. 1

  2. 4

  3. 5

  4. 6

  5. 8 class CallLogger def initialize(logger:) @logger = logger end def

    create(call) @logger.info("Creating call...") end end
  6. 9 class CallLogger def initialize(logger:) @logger = logger end def

    create(call) @logger.info("Creating call...") end end class CallLogger def create(call) Rails.logger.info("Creating call...") end end
  7. 10 class CallLogger attr_reader :logger def initialize(logger:) @logger = logger

    end def create(call) @logger.info("Creating call...") end end class CallLogger def create(call) Rails.logger.info("Creating call...") end def update(call) Rails.logger.info("Update call...") end def delete(call) Rails.logger.info("Delete call...") end def get(call) Rails.logger.info("Get call...") end end
  8. 11 class CallLogger attr_reader :logger def initialize(logger:) @logger = logger

    end def create(call) @logger.info("Creating call...") end end
  9. 12

  10. 13

  11. 14

  12. 15 interface Cache { fun put(key: String, value: String) fun

    get(key: String): String fun clear() } class AppCache: Cache { override fun put(key: String, value: String) { // usamos SharedPreference } override fun get(key: String): String { return // usamos SharedPreference } override fun clear() { // usamos SharedPreference } } class MemoryCache: Cache { val hashMap: HashMap<String, String> = hashMapOf() override fun put(key: String, value: String) { hashMap[key] = value } override fun get(key: String): String { return hashMap[key]!! } override fun clear() { hashMap.clear() } }
  13. 16 interface Cache { fun put(key: String, value: String) fun

    get(key: String): String fun clear() } class LoginView(val context: Context) { val cache: Cache = AndroidCache(context) fun login(username: String) { cache.put("username", username) } }
  14. 17 interface Cache { fun put(key: String, value: String) fun

    get(key: String): String fun clear() } class LoginView(val cache: Cache) { fun login(username: String) { cache.put("username", username) } }
  15. 18 class LoginView(val cache: Cache) { fun login(username: String) {

    cache.put("username", username) } } class LoginView(val context: Context) { val cache: Cache = AndroidCache(context) fun login(username: String) { cache.put("username", username) } }
  16. 19 class LoginView(val cache: Cache) { fun login(username: String) {

    cache.put("username", username) } } val view = LoginView(AppCache()) view.login("[email protected]")
  17. 20 class LoginView(val cache: Cache) { fun login(username: String) {

    cache.put("username", username) } } val cache = MemoryCache() val view = LoginView(cache) view.login("[email protected]") assert(cache.get("username"), "[email protected]")
  18. 21

  19. 22

  20. 23 class LoginView(val cache: Cache) { fun login(username: String) {

    cache.put("username", username) ... } } class SignUpView(val cache: Cache) { fun signup(username: String) { cache.put("username", username) ... } } class HomeView(val cache: Cache) { fun start(username: String) { cache.put("username", username) ... }
  21. 24 class SqlCache: Cache { override fun put(key: String, value:

    String) { // usamos Sql } override fun get(key: String): String { return // usamos Sql } override fun clear() { // usamos Sql } }
  22. 25 class LoginView(val cache: Cache) { fun login(username: String) {

    cache.put("username", username) ... } } class SignUpView(val cache: Cache) { fun signup(username: String) { cache.put("username", username) ... } } class HomeView(val cache: Cache) { fun start(username: String) { cache.put("username", username) ... }
  23. 27

  24. 28 class UploadCallService(val callRepository: CallRepository) { fun handleRingingEvent(adviserId: String, event:

    CallEvent) { callRepository.create(CallUpdate(adviserId, event)) } fun handleHangUpEvent(adviserId: String, event: CallEvent) { callRepository.update(CallUpdate(adviserId, event)) } } class CallRepository { fun create(call: CallUpdate): ApiResponse { // call web service } fun update(call: CallUpdate): ApiResponse { // call web service } }
  25. 29 interface CallRepository { fun create(call: CallUpdate): ApiResponse fun update(call:

    CallUpdate): ApiResponse } class QueueCallRepository() : CallRepository { override fun create(call: CallUpdate) { return sendMessage(call, CallQueueElement.Status.NEW) } override fun update(call: CallUpdate): Flowable<ApiResponse<Any>> { return sendMessage(call, CallQueueElement.Status.UPDATE) } private fun sendMessage(call: CallUpdate, status: CallQueueElement.Status) return sendCallLogMessage(CallQueueElement(call, status)) } catch (exception: Exception) { return getThrowableFailure(exception) } } private fun getThrowableFailure(exception: Exception): ApiResponse { return when (exception) { is AmazonClientException -> BadRequestFailure(exception) is AmazonServiceException -> InternalServerFailure(exception) else -> ThrowableFailure(exception) } } // fun sendCallLogMessage()...
  26. 30 class UploadCallService(val callRepository: CallRepository) { fun handleRingingEvent(adviserId: String, event:

    CallEvent) { callRepository.create(CallUpdate(adviserId, event)) } fun handleHangUpEvent(adviserId: String, event: CallEvent) { callRepository.update(CallUpdate(adviserId, event)) } }
  27. 31