SlideShare a Scribd company logo
1 of 34
Vodafone Proprietary classified as C2 - Internal
1 November 20192
Vodafone Proprietary classified as C2 - Internal
MVVM With Dependency
Injection
Yahya Saddiq
13 October 2019
Vodafone Proprietary classified as C2 - Internal
1 November 20194
• Now, after working for a few years in this area, cooperating with many excellent
engineers, I realized that the product design doesn’t really make the code so
complex. It’s me who makes it so complicated.
• We have the experience writing spaghetti code which significantly hurts the
performance of our projects. The question is how can we fix it?
A good architecture pattern might help.
Vodafone Proprietary classified as C2 - Internal
1 November 20195
• The term “good architecture” may sound way too abstract. It’s also difficult to know
where to start. Here’s a tip:
Instead of focusing on the definition of the architecture, we can focus on how to
improve the testability of the code
Vodafone Proprietary classified as C2 - Internal
1 November 20196
• We might not be able to master all of different types of architectures. However, we
are still able to keep a simple rule in mind:
no matter what architecture we decide to use, the ultimate goal is to
make test simpler
Vodafone Proprietary classified as C2 - Internal
1 November 20197
• Using “Make test simpler” approach helps us to:
– Start thinking before writing code
– Put emphasis on how to separate responsibility intuitively
– have a clear and reasonable mindset toward the design of the architecture.
– not stuck in trivial details anymore.
Vodafone Proprietary classified as C2 - Internal
1 November 20198
• I’ll talk about:
– The reason we choose the MVVM over the Apple MVC
– How to adapt MVVM to design a clearer architecture
– How to write a simple real-world app based on the MVVM
• I won’t talk about::
– The comparison between MVVM, and other architecture
– A silver bullet that will solve all problems
Vodafone Proprietary classified as C2 - Internal
1 November 20199
• Model
– Responsible for the business logic of the application.
– Reading and writing data
– Persisting application state
– May even include tasks related to data management, such as networking and data validation.
MVC (Model-View-Controller)
Vodafone Proprietary classified as C2 - Internal
1 November 201910
• View
– Should have two important tasks:
– presenting data to the user
– handling user interaction
– A core principle of the MVC pattern is the view layer's ignorance with respect to the model layer.
– Views are dumb objects.
– They only know how to present data to the user.
– They don't know or understand what they are presenting.
MVC (Model-View-Controller)
Vodafone Proprietary classified as C2 - Internal
1 November 201911
• Controller
– The view and model layers are glued together by one viewContoller.
– A controller knows about the view layer as well as the model layer.
– This often results in tight coupling, making controllers the least reusable component of an application based on the
MVC pattern.
MVC (Model-View-Controller)
Vodafone Proprietary classified as C2 - Internal
MVC Advantages
1 November 201912
• Separation of concerns
– In most applications, there is no confusion about what belongs in the view and model layer.
– What goes into controllers is often less clear.
– The result is that controllers are frequently used for everything that doesn't clearly belong in the view or
model layer.
• Reusability
– Whereas controllers are often not reusable, view and model objects are easy to reuse.
Vodafone Proprietary classified as C2 - Internal
MVC Problems
1 November 201913
• A clear separation of concerns is great.
– It makes our life as a developers easier.
– Projects are easier to architect and structure.
But that is only part of the story.
• If the code we write doesn't belong in the view or model layer. No problem. Dump it
in the controller. Problem solved. Right?
Not really.
Vodafone Proprietary classified as C2 - Internal
MVC Problem Example
1 November 201914
• Data formatting is a common task.
– Imagine that you are developing an invoicing application.
– Each invoice has a creation date.
– Depending on the locale of the user, the date of an invoice needs to be formatted differently.
• The creation date of an invoice is stored in the model layer and the view displays
the formatted date. That is obvious. But who is responsible for formatting the date?
– Remember that the view shouldn't need to understand what it is presenting to the user.
– But why should the model be responsible for a task that is related to the user interface?
Vodafone Proprietary classified as C2 - Internal
MVC Problem Example
1 November 201915
• Wait a minute.
– What about our good old controller?
– That’s why we called it a Massive View Controller.
MVVM is here to rescue
Vodafone Proprietary classified as C2 - Internal
1 November 201916
MVVM is proposed by John Gossman in 2005.
The main purpose of the MVVM is to move the
data state from the View to the ViewModel.
Vodafone Proprietary classified as C2 - Internal
MVVM (Model — View — ViewModel)
1 November 201917
• View
– According to the definition, the View consists of only visual elements.
– In the View, we only do things like layout, animation, initializing UI components, etc.
• ViewModel
– There’s a special layer between the View and the Model called the ViewModel
– The ViewModel is the representation of the View.
– ViewModel provides a set of interfaces, each of which represents a UI component in the View.
Vodafone Proprietary classified as C2 - Internal
MVVM (Model — View — ViewModel)
1 November 201918
• Binding
– We use a technique called “binding” to connect UI components to ViewModel interfaces.
– So, in MVVM, we don’t touch the View directly, we deal with business logic in the ViewModel and thus
the View changes itself accordingly.
• We write presentational things such as converting Date to String in the ViewModel
instead of the View
Therefore, it becomes possible to write a simpler test for the presentational logic
without knowing the implementation of the View.
Vodafone Proprietary classified as C2 - Internal
1 November 201919
Let’s take a higher look at the figure below.
– In general, the ViewModel receives the user interaction from the View,
– fetches data from the Model,
– then process the data to a set of ready-to-display properties.
– The View updates itself after observing the change of the ViewModel.
That’s the whole story of the MVVM.
Vodafone Proprietary classified as C2 - Internal
20 1 November 2019
We are going to see a simple app, in which:
– The app fetches popular photos from 500px API and lists
photos in a UITableView.
– Each cell in the table view shows a title, a description and the
created date of a photo.
– Users are not allowed to click photos which are not labeled
for_sale.
A simple gallery app — MVC
Vodafone Proprietary classified as C2 - Internal
1 November 201921
So what’s wrong with the code?
(The 4 problems)
A simple gallery app — MVC
Vodafone Proprietary classified as C2 - Internal
1 November 201922
• If you plan to write tests for the PhotoListViewController, we’ll find
ourselves stuck since it’s too complicated.
• We have to mock the APIService, mock the table view and mock the
cell to test the whole PhotoListViewController.
A simple gallery app — MVC
Vodafone Proprietary classified as C2 - Internal
1 November 201923
Remember that we want to make writing tests easier?
.
.
.
Let’s try MVVM approach!
Vodafone Proprietary classified as C2 - Internal
1 November 201924
• In order to solve the problem, our first priority is to clean up the view controller
• Split the view controller into two parts: the View and the ViewModel.
• To be specific, we are going to:
– Design a set of interfaces for binding.
– Move the presentational logic and controller logic to the ViewModel.
VCs Comparing Time!
Try MVVM
Vodafone Proprietary classified as C2 - Internal
25 1 November 2019
• Let’s take a look at the UI components
in the View:
– Activity Indicator (loading/finish)
– TableView (show/hide)
– Cells (title, description, created date)
• Each UI component has a
corresponding
property in the ViewModel.
• We can say that what we will see in the
View should be the same as what
we see in the ViewModel.
Vodafone Proprietary classified as C2 - Internal
1 November 201926
But how do we do the binding?
(KVO, RxSwift, DIY)
Vodafone Proprietary classified as C2 - Internal
1 November 201927
• Practically, in the ViewModel an interface/property for binding looks like this:
• On the other hand, in the View, we assign a closure to the propChanged as a callback closure for value
updates.
Vodafone Proprietary classified as C2 - Internal
1 November 201928
Interfaces for binding in ViewModel
(cellViewModels, numberOfCells, state)
Vodafone Proprietary classified as C2 - Internal
1 November 201929
Bind the View with the ViewModel
(callback closures in initVM())
Vodafone Proprietary classified as C2 - Internal
30 1 November 2019
Data Flow
1. The PhotoListViewModel starts to
fetch data.
2. After the data fetched, we
create PhotoListCellViewModel obje
cts and update the cellViewModels.
3. The PhotoListViewController is
notified of the update and then
layouts cells using the
updated cellViewModels.
Vodafone Proprietary classified as C2 - Internal
1 November 201931
Dealing with user interaction
(viewModel.userPressed())
Vodafone Proprietary classified as C2 - Internal
1 November 201932
Inject the dependency
The APIService
(filling cellViewModel)
Vodafone Proprietary classified as C2 - Internal
1 November 201933
Yay, we crafted the MVVM
🎉🎉🎉
Vodafone Proprietary classified as C2 - Internal
1 November 201934
Recap
• Made a binding theme using the closure.
• Removed all controller logic from the View.
• Created a testable ViewModel.
“The secret to getting ahead is getting started.” — Mark Twain

More Related Content

What's hot

Jetpack Compose.pptx
Jetpack Compose.pptxJetpack Compose.pptx
Jetpack Compose.pptxGDSCVJTI
 
Repository and Unit Of Work Design Patterns
Repository and Unit Of Work Design PatternsRepository and Unit Of Work Design Patterns
Repository and Unit Of Work Design PatternsHatim Hakeel
 
Terraform and Weave GitOps: Build a Fully Automated Application Stack
Terraform and Weave GitOps: Build a Fully Automated Application StackTerraform and Weave GitOps: Build a Fully Automated Application Stack
Terraform and Weave GitOps: Build a Fully Automated Application StackWeaveworks
 
GITS Class #16: CI/CD (Continuous Integration & Continuous Deployment) with G...
GITS Class #16: CI/CD (Continuous Integration & Continuous Deployment) with G...GITS Class #16: CI/CD (Continuous Integration & Continuous Deployment) with G...
GITS Class #16: CI/CD (Continuous Integration & Continuous Deployment) with G...GITS Indonesia
 
Exploring the power of OpenTelemetry on Kubernetes
Exploring the power of OpenTelemetry on KubernetesExploring the power of OpenTelemetry on Kubernetes
Exploring the power of OpenTelemetry on KubernetesRed Hat Developers
 
Anatomy of a Spring Boot App with Clean Architecture - Spring I/O 2023
Anatomy of a Spring Boot App with Clean Architecture - Spring I/O 2023Anatomy of a Spring Boot App with Clean Architecture - Spring I/O 2023
Anatomy of a Spring Boot App with Clean Architecture - Spring I/O 2023Steve Pember
 
Ensuring Kubernetes Cost Efficiency across (many) Clusters - DevOps Gathering...
Ensuring Kubernetes Cost Efficiency across (many) Clusters - DevOps Gathering...Ensuring Kubernetes Cost Efficiency across (many) Clusters - DevOps Gathering...
Ensuring Kubernetes Cost Efficiency across (many) Clusters - DevOps Gathering...Henning Jacobs
 
Introduce Google Kubernetes
Introduce Google KubernetesIntroduce Google Kubernetes
Introduce Google KubernetesYongbok Kim
 
Micro-frontends with Angular 10 (Modern Web 2020)
Micro-frontends with Angular 10 (Modern Web 2020)Micro-frontends with Angular 10 (Modern Web 2020)
Micro-frontends with Angular 10 (Modern Web 2020)Will Huang
 
iOS Development, with Swift and XCode
iOS Development, with Swift and XCodeiOS Development, with Swift and XCode
iOS Development, with Swift and XCodeWan Leung Wong
 
Free GitOps Workshop + Intro to Kubernetes & GitOps
Free GitOps Workshop + Intro to Kubernetes & GitOpsFree GitOps Workshop + Intro to Kubernetes & GitOps
Free GitOps Workshop + Intro to Kubernetes & GitOpsWeaveworks
 
Hybrid Apps with Angular & Ionic Framework
Hybrid Apps with Angular & Ionic FrameworkHybrid Apps with Angular & Ionic Framework
Hybrid Apps with Angular & Ionic FrameworkCihad Horuzoğlu
 
GDSC Flutter Forward Workshop.pptx
GDSC Flutter Forward Workshop.pptxGDSC Flutter Forward Workshop.pptx
GDSC Flutter Forward Workshop.pptxGDSCVJTI
 
Getting Started with Kubernetes
Getting Started with Kubernetes Getting Started with Kubernetes
Getting Started with Kubernetes VMware Tanzu
 
Docker Tutorial For Beginners | What Is Docker And How It Works? | Docker Tut...
Docker Tutorial For Beginners | What Is Docker And How It Works? | Docker Tut...Docker Tutorial For Beginners | What Is Docker And How It Works? | Docker Tut...
Docker Tutorial For Beginners | What Is Docker And How It Works? | Docker Tut...Simplilearn
 

What's hot (20)

Jetpack Compose.pptx
Jetpack Compose.pptxJetpack Compose.pptx
Jetpack Compose.pptx
 
Repository and Unit Of Work Design Patterns
Repository and Unit Of Work Design PatternsRepository and Unit Of Work Design Patterns
Repository and Unit Of Work Design Patterns
 
Gitlab ci-cd
Gitlab ci-cdGitlab ci-cd
Gitlab ci-cd
 
Terraform and Weave GitOps: Build a Fully Automated Application Stack
Terraform and Weave GitOps: Build a Fully Automated Application StackTerraform and Weave GitOps: Build a Fully Automated Application Stack
Terraform and Weave GitOps: Build a Fully Automated Application Stack
 
GITS Class #16: CI/CD (Continuous Integration & Continuous Deployment) with G...
GITS Class #16: CI/CD (Continuous Integration & Continuous Deployment) with G...GITS Class #16: CI/CD (Continuous Integration & Continuous Deployment) with G...
GITS Class #16: CI/CD (Continuous Integration & Continuous Deployment) with G...
 
TypeScript
TypeScriptTypeScript
TypeScript
 
Exploring the power of OpenTelemetry on Kubernetes
Exploring the power of OpenTelemetry on KubernetesExploring the power of OpenTelemetry on Kubernetes
Exploring the power of OpenTelemetry on Kubernetes
 
Tdd ver.2
Tdd ver.2Tdd ver.2
Tdd ver.2
 
Anatomy of a Spring Boot App with Clean Architecture - Spring I/O 2023
Anatomy of a Spring Boot App with Clean Architecture - Spring I/O 2023Anatomy of a Spring Boot App with Clean Architecture - Spring I/O 2023
Anatomy of a Spring Boot App with Clean Architecture - Spring I/O 2023
 
Ensuring Kubernetes Cost Efficiency across (many) Clusters - DevOps Gathering...
Ensuring Kubernetes Cost Efficiency across (many) Clusters - DevOps Gathering...Ensuring Kubernetes Cost Efficiency across (many) Clusters - DevOps Gathering...
Ensuring Kubernetes Cost Efficiency across (many) Clusters - DevOps Gathering...
 
Introduce Google Kubernetes
Introduce Google KubernetesIntroduce Google Kubernetes
Introduce Google Kubernetes
 
Micro-frontends with Angular 10 (Modern Web 2020)
Micro-frontends with Angular 10 (Modern Web 2020)Micro-frontends with Angular 10 (Modern Web 2020)
Micro-frontends with Angular 10 (Modern Web 2020)
 
iOS Development, with Swift and XCode
iOS Development, with Swift and XCodeiOS Development, with Swift and XCode
iOS Development, with Swift and XCode
 
Free GitOps Workshop + Intro to Kubernetes & GitOps
Free GitOps Workshop + Intro to Kubernetes & GitOpsFree GitOps Workshop + Intro to Kubernetes & GitOps
Free GitOps Workshop + Intro to Kubernetes & GitOps
 
Hybrid Apps with Angular & Ionic Framework
Hybrid Apps with Angular & Ionic FrameworkHybrid Apps with Angular & Ionic Framework
Hybrid Apps with Angular & Ionic Framework
 
GDSC Flutter Forward Workshop.pptx
GDSC Flutter Forward Workshop.pptxGDSC Flutter Forward Workshop.pptx
GDSC Flutter Forward Workshop.pptx
 
TypeScript Presentation
TypeScript PresentationTypeScript Presentation
TypeScript Presentation
 
Getting Started with Kubernetes
Getting Started with Kubernetes Getting Started with Kubernetes
Getting Started with Kubernetes
 
Git & GitLab
Git & GitLabGit & GitLab
Git & GitLab
 
Docker Tutorial For Beginners | What Is Docker And How It Works? | Docker Tut...
Docker Tutorial For Beginners | What Is Docker And How It Works? | Docker Tut...Docker Tutorial For Beginners | What Is Docker And How It Works? | Docker Tut...
Docker Tutorial For Beginners | What Is Docker And How It Works? | Docker Tut...
 

Similar to MVVM with Dependency Injection

Design Pattern - MVC, MVP and MVVM
Design Pattern - MVC, MVP and MVVMDesign Pattern - MVC, MVP and MVVM
Design Pattern - MVC, MVP and MVVMMudasir Qazi
 
Architectural Design Pattern: Android
Architectural Design Pattern: AndroidArchitectural Design Pattern: Android
Architectural Design Pattern: AndroidJitendra Kumar
 
Task 2 - Educational Article – Model View Controller (MVC)
Task 2 - Educational Article – Model View Controller (MVC)Task 2 - Educational Article – Model View Controller (MVC)
Task 2 - Educational Article – Model View Controller (MVC)Shubham Goenka
 
iOS architecture patterns
iOS architecture patternsiOS architecture patterns
iOS architecture patternsallanh0526
 
Models used in iOS programming, with a focus on MVVM
Models used in iOS programming, with a focus on MVVMModels used in iOS programming, with a focus on MVVM
Models used in iOS programming, with a focus on MVVMAndrei Popa
 
Unit Testing and Why it Matters
Unit Testing and Why it MattersUnit Testing and Why it Matters
Unit Testing and Why it MattersyahyaSadiiq
 
MVC(Model View Controller),Web,Enterprise,Mobile
MVC(Model View Controller),Web,Enterprise,MobileMVC(Model View Controller),Web,Enterprise,Mobile
MVC(Model View Controller),Web,Enterprise,Mobilenaral
 
RESUME BUILDER WEB APPLICATION
RESUME BUILDER WEB APPLICATIONRESUME BUILDER WEB APPLICATION
RESUME BUILDER WEB APPLICATIONIRJET Journal
 
реалии использования Mv в i os разработке
реалии использования Mv в i os разработкереалии использования Mv в i os разработке
реалии использования Mv в i os разработкеProvectus
 
Automating Screenshot Testing Component Library
Automating Screenshot Testing Component LibraryAutomating Screenshot Testing Component Library
Automating Screenshot Testing Component LibraryApplitools
 
Workshop on Sencha Touch - Part 3 - MVC in sencha touch
Workshop on Sencha Touch - Part 3 - MVC in sencha touchWorkshop on Sencha Touch - Part 3 - MVC in sencha touch
Workshop on Sencha Touch - Part 3 - MVC in sencha touchNithya Sivakumar
 
Patterns of evolution from monolith to microservices
Patterns of evolution from monolith to microservicesPatterns of evolution from monolith to microservices
Patterns of evolution from monolith to microservicesKarina Mora
 
MV(C, mvvm) in iOS and ReactiveCocoa
MV(C, mvvm) in iOS and ReactiveCocoaMV(C, mvvm) in iOS and ReactiveCocoa
MV(C, mvvm) in iOS and ReactiveCocoaYi-Shou Chen
 
Code Camp 06 Model View Presenter Architecture
Code Camp 06   Model View Presenter ArchitectureCode Camp 06   Model View Presenter Architecture
Code Camp 06 Model View Presenter Architecturebitburner93
 
1040 ibm worklight delivering agility to mobile cloud deployments
1040 ibm worklight  delivering agility to mobile cloud deployments1040 ibm worklight  delivering agility to mobile cloud deployments
1040 ibm worklight delivering agility to mobile cloud deploymentsTodd Kaplinger
 

Similar to MVVM with Dependency Injection (20)

Android MVVM
Android MVVMAndroid MVVM
Android MVVM
 
Mvvm
MvvmMvvm
Mvvm
 
Design Pattern - MVC, MVP and MVVM
Design Pattern - MVC, MVP and MVVMDesign Pattern - MVC, MVP and MVVM
Design Pattern - MVC, MVP and MVVM
 
Architectural Design Pattern: Android
Architectural Design Pattern: AndroidArchitectural Design Pattern: Android
Architectural Design Pattern: Android
 
Task 2 - Educational Article – Model View Controller (MVC)
Task 2 - Educational Article – Model View Controller (MVC)Task 2 - Educational Article – Model View Controller (MVC)
Task 2 - Educational Article – Model View Controller (MVC)
 
iOS architecture patterns
iOS architecture patternsiOS architecture patterns
iOS architecture patterns
 
Models used in iOS programming, with a focus on MVVM
Models used in iOS programming, with a focus on MVVMModels used in iOS programming, with a focus on MVVM
Models used in iOS programming, with a focus on MVVM
 
Unit Testing and Why it Matters
Unit Testing and Why it MattersUnit Testing and Why it Matters
Unit Testing and Why it Matters
 
MVC(Model View Controller),Web,Enterprise,Mobile
MVC(Model View Controller),Web,Enterprise,MobileMVC(Model View Controller),Web,Enterprise,Mobile
MVC(Model View Controller),Web,Enterprise,Mobile
 
RESUME BUILDER WEB APPLICATION
RESUME BUILDER WEB APPLICATIONRESUME BUILDER WEB APPLICATION
RESUME BUILDER WEB APPLICATION
 
реалии использования Mv в i os разработке
реалии использования Mv в i os разработкереалии использования Mv в i os разработке
реалии использования Mv в i os разработке
 
Automating Screenshot Testing Component Library
Automating Screenshot Testing Component LibraryAutomating Screenshot Testing Component Library
Automating Screenshot Testing Component Library
 
Knockout js
Knockout jsKnockout js
Knockout js
 
Workshop on Sencha Touch - Part 3 - MVC in sencha touch
Workshop on Sencha Touch - Part 3 - MVC in sencha touchWorkshop on Sencha Touch - Part 3 - MVC in sencha touch
Workshop on Sencha Touch - Part 3 - MVC in sencha touch
 
Adopting MVVM
Adopting MVVMAdopting MVVM
Adopting MVVM
 
Patterns of evolution from monolith to microservices
Patterns of evolution from monolith to microservicesPatterns of evolution from monolith to microservices
Patterns of evolution from monolith to microservices
 
MV(C, mvvm) in iOS and ReactiveCocoa
MV(C, mvvm) in iOS and ReactiveCocoaMV(C, mvvm) in iOS and ReactiveCocoa
MV(C, mvvm) in iOS and ReactiveCocoa
 
Code Camp 06 Model View Presenter Architecture
Code Camp 06   Model View Presenter ArchitectureCode Camp 06   Model View Presenter Architecture
Code Camp 06 Model View Presenter Architecture
 
Why MVC?
Why MVC?Why MVC?
Why MVC?
 
1040 ibm worklight delivering agility to mobile cloud deployments
1040 ibm worklight  delivering agility to mobile cloud deployments1040 ibm worklight  delivering agility to mobile cloud deployments
1040 ibm worklight delivering agility to mobile cloud deployments
 

Recently uploaded

Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software DevelopersVinodh Ram
 
Folding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesFolding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesPhilip Schwarz
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...gurkirankumar98700
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...stazi3110
 
Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureDinusha Kumarasiri
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEOrtus Solutions, Corp
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEEVICTOR MAESTRE RAMIREZ
 
The Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdfThe Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdfPower Karaoke
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...OnePlan Solutions
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - InfographicHr365.us smith
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio, Inc.
 
MYjobs Presentation Django-based project
MYjobs Presentation Django-based projectMYjobs Presentation Django-based project
MYjobs Presentation Django-based projectAnoyGreter
 
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样umasea
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataBradBedford3
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackVICTOR MAESTRE RAMIREZ
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...soniya singh
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Andreas Granig
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...MyIntelliSource, Inc.
 

Recently uploaded (20)

Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software Developers
 
Folding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesFolding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a series
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
 
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
 
Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with Azure
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEE
 
The Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdfThe Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdf
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...
 
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort ServiceHot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - Infographic
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
 
MYjobs Presentation Django-based project
MYjobs Presentation Django-based projectMYjobs Presentation Django-based project
MYjobs Presentation Django-based project
 
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStack
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
 

MVVM with Dependency Injection

  • 1.
  • 2. Vodafone Proprietary classified as C2 - Internal 1 November 20192
  • 3. Vodafone Proprietary classified as C2 - Internal MVVM With Dependency Injection Yahya Saddiq 13 October 2019
  • 4. Vodafone Proprietary classified as C2 - Internal 1 November 20194 • Now, after working for a few years in this area, cooperating with many excellent engineers, I realized that the product design doesn’t really make the code so complex. It’s me who makes it so complicated. • We have the experience writing spaghetti code which significantly hurts the performance of our projects. The question is how can we fix it? A good architecture pattern might help.
  • 5. Vodafone Proprietary classified as C2 - Internal 1 November 20195 • The term “good architecture” may sound way too abstract. It’s also difficult to know where to start. Here’s a tip: Instead of focusing on the definition of the architecture, we can focus on how to improve the testability of the code
  • 6. Vodafone Proprietary classified as C2 - Internal 1 November 20196 • We might not be able to master all of different types of architectures. However, we are still able to keep a simple rule in mind: no matter what architecture we decide to use, the ultimate goal is to make test simpler
  • 7. Vodafone Proprietary classified as C2 - Internal 1 November 20197 • Using “Make test simpler” approach helps us to: – Start thinking before writing code – Put emphasis on how to separate responsibility intuitively – have a clear and reasonable mindset toward the design of the architecture. – not stuck in trivial details anymore.
  • 8. Vodafone Proprietary classified as C2 - Internal 1 November 20198 • I’ll talk about: – The reason we choose the MVVM over the Apple MVC – How to adapt MVVM to design a clearer architecture – How to write a simple real-world app based on the MVVM • I won’t talk about:: – The comparison between MVVM, and other architecture – A silver bullet that will solve all problems
  • 9. Vodafone Proprietary classified as C2 - Internal 1 November 20199 • Model – Responsible for the business logic of the application. – Reading and writing data – Persisting application state – May even include tasks related to data management, such as networking and data validation. MVC (Model-View-Controller)
  • 10. Vodafone Proprietary classified as C2 - Internal 1 November 201910 • View – Should have two important tasks: – presenting data to the user – handling user interaction – A core principle of the MVC pattern is the view layer's ignorance with respect to the model layer. – Views are dumb objects. – They only know how to present data to the user. – They don't know or understand what they are presenting. MVC (Model-View-Controller)
  • 11. Vodafone Proprietary classified as C2 - Internal 1 November 201911 • Controller – The view and model layers are glued together by one viewContoller. – A controller knows about the view layer as well as the model layer. – This often results in tight coupling, making controllers the least reusable component of an application based on the MVC pattern. MVC (Model-View-Controller)
  • 12. Vodafone Proprietary classified as C2 - Internal MVC Advantages 1 November 201912 • Separation of concerns – In most applications, there is no confusion about what belongs in the view and model layer. – What goes into controllers is often less clear. – The result is that controllers are frequently used for everything that doesn't clearly belong in the view or model layer. • Reusability – Whereas controllers are often not reusable, view and model objects are easy to reuse.
  • 13. Vodafone Proprietary classified as C2 - Internal MVC Problems 1 November 201913 • A clear separation of concerns is great. – It makes our life as a developers easier. – Projects are easier to architect and structure. But that is only part of the story. • If the code we write doesn't belong in the view or model layer. No problem. Dump it in the controller. Problem solved. Right? Not really.
  • 14. Vodafone Proprietary classified as C2 - Internal MVC Problem Example 1 November 201914 • Data formatting is a common task. – Imagine that you are developing an invoicing application. – Each invoice has a creation date. – Depending on the locale of the user, the date of an invoice needs to be formatted differently. • The creation date of an invoice is stored in the model layer and the view displays the formatted date. That is obvious. But who is responsible for formatting the date? – Remember that the view shouldn't need to understand what it is presenting to the user. – But why should the model be responsible for a task that is related to the user interface?
  • 15. Vodafone Proprietary classified as C2 - Internal MVC Problem Example 1 November 201915 • Wait a minute. – What about our good old controller? – That’s why we called it a Massive View Controller. MVVM is here to rescue
  • 16. Vodafone Proprietary classified as C2 - Internal 1 November 201916 MVVM is proposed by John Gossman in 2005. The main purpose of the MVVM is to move the data state from the View to the ViewModel.
  • 17. Vodafone Proprietary classified as C2 - Internal MVVM (Model — View — ViewModel) 1 November 201917 • View – According to the definition, the View consists of only visual elements. – In the View, we only do things like layout, animation, initializing UI components, etc. • ViewModel – There’s a special layer between the View and the Model called the ViewModel – The ViewModel is the representation of the View. – ViewModel provides a set of interfaces, each of which represents a UI component in the View.
  • 18. Vodafone Proprietary classified as C2 - Internal MVVM (Model — View — ViewModel) 1 November 201918 • Binding – We use a technique called “binding” to connect UI components to ViewModel interfaces. – So, in MVVM, we don’t touch the View directly, we deal with business logic in the ViewModel and thus the View changes itself accordingly. • We write presentational things such as converting Date to String in the ViewModel instead of the View Therefore, it becomes possible to write a simpler test for the presentational logic without knowing the implementation of the View.
  • 19. Vodafone Proprietary classified as C2 - Internal 1 November 201919 Let’s take a higher look at the figure below. – In general, the ViewModel receives the user interaction from the View, – fetches data from the Model, – then process the data to a set of ready-to-display properties. – The View updates itself after observing the change of the ViewModel. That’s the whole story of the MVVM.
  • 20. Vodafone Proprietary classified as C2 - Internal 20 1 November 2019 We are going to see a simple app, in which: – The app fetches popular photos from 500px API and lists photos in a UITableView. – Each cell in the table view shows a title, a description and the created date of a photo. – Users are not allowed to click photos which are not labeled for_sale. A simple gallery app — MVC
  • 21. Vodafone Proprietary classified as C2 - Internal 1 November 201921 So what’s wrong with the code? (The 4 problems) A simple gallery app — MVC
  • 22. Vodafone Proprietary classified as C2 - Internal 1 November 201922 • If you plan to write tests for the PhotoListViewController, we’ll find ourselves stuck since it’s too complicated. • We have to mock the APIService, mock the table view and mock the cell to test the whole PhotoListViewController. A simple gallery app — MVC
  • 23. Vodafone Proprietary classified as C2 - Internal 1 November 201923 Remember that we want to make writing tests easier? . . . Let’s try MVVM approach!
  • 24. Vodafone Proprietary classified as C2 - Internal 1 November 201924 • In order to solve the problem, our first priority is to clean up the view controller • Split the view controller into two parts: the View and the ViewModel. • To be specific, we are going to: – Design a set of interfaces for binding. – Move the presentational logic and controller logic to the ViewModel. VCs Comparing Time! Try MVVM
  • 25. Vodafone Proprietary classified as C2 - Internal 25 1 November 2019 • Let’s take a look at the UI components in the View: – Activity Indicator (loading/finish) – TableView (show/hide) – Cells (title, description, created date) • Each UI component has a corresponding property in the ViewModel. • We can say that what we will see in the View should be the same as what we see in the ViewModel.
  • 26. Vodafone Proprietary classified as C2 - Internal 1 November 201926 But how do we do the binding? (KVO, RxSwift, DIY)
  • 27. Vodafone Proprietary classified as C2 - Internal 1 November 201927 • Practically, in the ViewModel an interface/property for binding looks like this: • On the other hand, in the View, we assign a closure to the propChanged as a callback closure for value updates.
  • 28. Vodafone Proprietary classified as C2 - Internal 1 November 201928 Interfaces for binding in ViewModel (cellViewModels, numberOfCells, state)
  • 29. Vodafone Proprietary classified as C2 - Internal 1 November 201929 Bind the View with the ViewModel (callback closures in initVM())
  • 30. Vodafone Proprietary classified as C2 - Internal 30 1 November 2019 Data Flow 1. The PhotoListViewModel starts to fetch data. 2. After the data fetched, we create PhotoListCellViewModel obje cts and update the cellViewModels. 3. The PhotoListViewController is notified of the update and then layouts cells using the updated cellViewModels.
  • 31. Vodafone Proprietary classified as C2 - Internal 1 November 201931 Dealing with user interaction (viewModel.userPressed())
  • 32. Vodafone Proprietary classified as C2 - Internal 1 November 201932 Inject the dependency The APIService (filling cellViewModel)
  • 33. Vodafone Proprietary classified as C2 - Internal 1 November 201933 Yay, we crafted the MVVM 🎉🎉🎉
  • 34. Vodafone Proprietary classified as C2 - Internal 1 November 201934 Recap • Made a binding theme using the closure. • Removed all controller logic from the View. • Created a testable ViewModel. “The secret to getting ahead is getting started.” — Mark Twain