Skip to content
This repository has been archived by the owner on May 8, 2024. It is now read-only.
/ viper-demo-ios Public archive

An app demonstrating Viper-B pattern implementation

Notifications You must be signed in to change notification settings

unwire/viper-demo-ios

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

VIPER-B example application

!!! WARNING !!!
Example may contain **strong** symptoms of over-architecture.
Please proceed with _caution_!
  • Not necessarily an expert view
  • Consciously ties to follow clean architecture approach to the extreme

Inspired by...

Uncle Bob

Nicola Zaghini

!!! Must watch video: [Mastering reuse: A journey into application modularization with VIPER](https://skillsmatter.com/skillscasts/7931-mastering-reuse-a- https://skillsmatter.com/skillscasts/7931-mastering-reuse-a-journey-into-application-modularization-with-viper)

Why Viper-B?

  • Describes components accurately
  • Well defined application layers: view, presentation, business, enterprise, routing
  • Clear responsibilities and boundaries

VIPER-B COMPONENTS OVERVIEW:

fit

VIPER-B LAYERS DIGEST:

1. View & Presentation layer

left fit

View:

  • Usually UIViewController subclass
  • Simplest UIKit code (similar to other patterns, like MVVM)
  • Displays view model delivered by presenter (weak delegate)
  • Delegates all UI events to presenter

Presenter:

  • Delivers ViewModel to the VIEW
  • Handles events delivered from the VIEW
  • An adapter between business logic INTERACTOR and UI (VIEW)
  • Converts Models to something displayable (ViewModel) and delivers it to the delegate (VIEW)
  • May call ROUTER when decides that view events need to be handled externally
  • No UIKit

View model:

  • Data model generated by PRESENTER
  • Describing UI state for the VIEW
  • Simple property types (String, Bool, enum)
  • No UIKit

2. Business layer

left fit

Interactor

  • A PONSO encapsulating business rules for specific use case.
  • Transforms entities into models.
  • Knows what tasks needs to be carried out before returning stuff to the presenter.
  • Does not care where data comes from, uses data manager(s) for this.
  • Does not care about presentation.
  • Can subscribe to events from data managers
  • Api should have very specific methods to meet business requirements.

Model

  • Encapsulates data model of results coming from INTERACTOR.
  • Contains raw properties that are interesting for the use case.
  • Not suitable for display (presenter's job).

3. Enterprise layer

left fit

Data Manager (entity Gateway)

  • Encapsulates an API to the enterprise layer (returns entities).
  • Grouped around domains, e.g. Hero, Image, User
  • The data manager knows where to fetch data from and knows if something should be persisted
  • Does not know the underlaying technologies used for storage or service
  • May have multiple subscribed interactors
  • Longer life-span

Entity

  • Enterprise data model.
  • Not application specific (e.g. coming from external database scheme or web service)
  • Primitives / database-like properties (Int, String)

Service

  • Service to get data from slower resources (network)
  • Operates on entities

Storage

  • Faster, local storage or cache (e.g. NSUserDefaults, CoreData etc)
  • Api should be simple and fast.

4. Builder & Router

left fit

Builder:

  • An ENTRY point to the module.
  • Responsible for creating and wiring up Viper module.
  • Produces and returns UIViewController, retaining the module.
  • Must contain at least one building method.

Router:

  • An EXIT point from the module.
  • Takes care of routing from one module to another.

fit

Pros:

  • Testable
  • Small and specialised classes
  • Clear boundaries between layers and data types
  • Represents data flow steps and transformation very well
  • Promotes composition & protocol oriented design
  • Promotes structured code
  • Facilitates working in teams (not many reasons to change one file)
  • Forces you to take care about naming conventions (so many files and types ;-)
  • Imposes discipline (every time I broke pattern, I had issues with testing)
  • Highlights issues arising from overuse of 3rd party libraries
  • Becomes natural, after a while
  • Fun (if you like LEGO blocks)
  • No singletons!

Cons:

  • Ceremony: lots of files and boiler-plate code
  • Overkill for some projects (prototyping, etc..)
  • Requires well defined requirements up-front.
  • Mentally challenging to grasp whole module and track data flow
  • Lots of data model conversions
  • Difficult to stay clean and reuse the code at the same time
  • Pain to refactor
  • Pain when developing against iOS frameworks (UIKit, Core Data)
  • Interactors turned out to be not that reusable after all
  • Many files
  • Changes you...
  • Sometimes feels like a dog chasing its tail

References

About

An app demonstrating Viper-B pattern implementation

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages