London Escorts sunderland escorts 1v1.lol unblocked yohoho 76 https://www.symbaloo.com/mix/yohoho?lang=EN yohoho https://www.symbaloo.com/mix/agariounblockedpvp https://yohoho-io.app/ https://www.symbaloo.com/mix/agariounblockedschool1?lang=EN
-0.4 C
New York
Monday, February 3, 2025

Mastering the VIPER structure – The.Swift.Dev.


After writing my finest practices article about VIPER, I’ve made a number of modifications to the codebase. I used to be enjoying with these concepts in my thoughts already, however by no means had sufficient time to implement them correctly. Let’s me present you the modifications…

VIPER protocols

My generic subject was that I wished to have a frequent interface for each single module element. That is why I created easy protocols for the next:

  • View
  • Interactor
  • Presenter
  • Entity
  • Router
  • Module

This manner for instance my router objects are implementing the Router protocol, so if I make an extension on it, each single one may have that exact performance. It is a fairly small, however very nice addition that makes my modules far more highly effective than they had been earlier than. Actually talking I ought to have had this from the very starting, however anyway any more it is gona be like this. 😬

This transfer implied to arrange my VIPER protocols right into a customized framework, so I made one, with these parts. You could find it on GitHub, it is a actually primary one, be at liberty to make use of it, you simply should import VIPER in your venture.

Module protocols

Since I used to be utilizing VIPER it had this nice urge to implement a customized module for presenting system default alert messages on iOS. UIAlertController is the one I am speaking about. Really Robi (my true steel good friend) prompt a surprisingly good normal answer for the issue. His concept gave the impression of this:

Why do not we create a protocol for the router, so we may implement this on each different router, additionally we may merely name present(alert:) on them?

I beloved this method, so we have constructed it. Turned out, it is freakin superior. So we launched a brand new protocol for the module router, carried out a default protocol extension and voilà routers are actually able to presenting error messages.

Observe that you need to use the identical sample for many different (related) issues as effectively. The fundamental implementation appears to be like like this one beneath, I hope you get the thought. 💡

import VIPER

class AlertModule: Module {
    
}

protocol AlertModuleRouter: class {

    func present(alert: AlertEntity)
}

extension AlertModuleRouter the place Self: Router {

    func present(alert: AlertEntity) {
        
    }
}



protocol MyModuleRouter: Router, AlertModuleRouter {

    
}

In fact this system can work for different VIPER parts as effectively, it is fairly straightforward to implment and the protocol oriented method provides us an enormous win. 🏆

Presenter to presenter interactions

I additionally modified my thoughts concerning the place of the delegate implementations collaborating within the module communication circulation. In my final article I instructed you that I am storing the delegate on the router, however in a while I spotted that delegation is usually associated to enterprise logic, so I merely moved them to the presenter layer. Sorry about this. 🤷‍♂️

import VIPER

protocol AModulePresenterDelegate {
    func didDoSomething()
}

class AModule: Module {

    func construct(with delegate: AModulePresenterDelegate? = nil) -> UIViewController {
        

        presenter.delegate = delegate

        

        return view
    }
}

class AModulePresenter: Presenter {

    func someAction() {
        self.delegate?.didDoSomething()
        self.router?.dismiss()
    }
}



class BModulePresenter: Presenter, AModulePresenterDelegate {

    func didDoSomething() {
        print("Hey from module A!")
    }
}

This manner you may skip the whole router layer, plus all of the enterprise associated logic will likely be carried out within the presenter layer, which ought to be the one method to go. 🤪

Entities are right here to remain

Other than the service layer generally it is fairly helpful to have an entity wrapper with some further metadata for the mannequin objects. That is why I additionally made an Entity protocol, and began to make use of it in my modules. For instance an online view module that may open a hyperlink can have a WebViewEntity with a title and a content material URL property. 😅

import VIPER

struct AlertEntity: Entity {
    let title: String
    let message: String
}

The pattern alert module from above can use an AlertEntity with some properties that may outline the title, message or the buttons. This manner you do not actually have to consider the place to place these objects, as a result of these are the actual VIPER entities.

IO protocols

This can be a WIP (work-in-progress) concept that I would prefer to check out, however the primary idea is considerably like that I need to separate enter and output protocols for VIPER module layers. Additionally this IO differentiation could be mirrored on the service layers too (perhaps the entire object “mess” from the service layer goes for use as IO entities sooner or later), by mess I imply that there could be approach too many objects within the Service/Objects listing, so which means that these may very well be additionally grouped by modules (aka. entities).

Anyway, I am considering of one thing like RequestEntity, ResponseEntity for service communication, and for the VIPER layer communication I may think about two separate protocols, e.g. PresenterInput, PresenterOutput. We’ll see, however at first sight, it is looks like fairly an over-engineered factor (hahaha, says the VIPER advocate 😂).

VIPER vs [put your architecture name here]

No! Please do not suppose that x is best than y. Architectures and design patterns are easy instruments that may be utilized to make your life easier. In case you do not like x, you must strive y, however you shouldn’t blame x, simply because that is your private opinion.

My present favourite structure is VIPER, so what? Possibly in a yr or two I am going to go loopy in love with reactive programming. Does it actually issues? I do not suppose so. I’ve realized and tried so many issues in the course of the previous, that I am unable to even bear in mind. 🧠

I am additionally consistently attempting to determine new issues, as you may see this complete collection of articles about VIPER is the results of my studying progress & experiences. In case you actually need to grasp one thing, you must observe, analysis and take a look at lots, and most significantly be pleased with your successes and keep humble on the similar time. 🙏

That is it concerning the VIPER structure for some time. I hope you loved studying the entire collection. When you’ve got any questions, be at liberty to ask me via Twitter. 💭

Related Articles

Social Media Auto Publish Powered By : XYZScripts.com