Mid Lane is the lane about Cooldowns. Literally every proper midlaner in the game has the hard engage to screw you or the opponent over if either player screws up in any way. The key to dealing with this is learning how to balance your Cooldowns.
The tl;dr is that you need to pay attention to when you and your opponent's abilities are on or off cool down. Simply put, if your opponent's abilities are on cool down and yours aren't, engage. If you need or have used your own abilities and your opponent still has all of theirs, back off and be careful.
The long explanation starts here:
DPS and pseudo DPS champs like Azir need to mitigate the opponent's ability to negate their DPS as much as possible. They're almost always ready to fight, what with their kits being DPS based, but they tend to crumble under significant burst if the player using them isn't prepared. The general rule here should be to wait for the opponent to either use their mobility based abilities or their hard CC. For example, let's say Ahri fires her Charm at you and she misses. Especially if it's pre 6, this is the perfect time to start wailing away at her as much as you could. Unless your health is low and she can burst you down, Ahri is at an extreme disadvantage in this situation as a DPS champ like Azir will simply out trade her if she doesn't Charm her target. Conversely, in order to face off DPS champs, save CC and mobility. DPS champs scale best with time, meaning the longer they are pinned down (you Stun them, for example), the worse off they are in a given trade. They also tend to only have very limited ways with keeping up with their opponent, meaning a well timed escape can easily get you out of range of their primary DPS, and thus put you back into safety.
Assassins with hard engage like Ahri primarily need to pay attention to the opponent's mobility. There's very little reason for Ahri to try to land her Charm on, for example, a Katarina while Katarina's Shunpo is up, as Katarina's just going to dodge the skillshot and take the opportunity to bully Ahri for burning her CC. While this situation is far more matchup specific (Ahri should tackle a Katarina completely differently from how she would face Azir), these kinds of champions will generally win by taking advantage of the enemy midlaner the moment they misuse their movement based abilities and are out of position for too long. Countering these champs tend to have three parts: Stay mobile, recognize their engage spell, and recognize their strongest spell. I think it's obvious why mobility, or at least any way to mitigate their engage
is strong against these champs, so I won't reiterate. What I do want to explain however is knowing how to recognize their strongest spell compared to their engage spell, as there aren't many mid lane champions who actually share both of these traits as a single spell. Ahri's strongest spell is her Orb of Deception, whereas her engage spell is Charm. Charm's strength is all about amplifying damage, in other words, without the strongest spell in Ahri's arsenal to amplify, her Orb of Deception, Charm is significantly weaker of a threat. This means even if the champion's engage tool is still up, opportunities arise if they use their most powerful spell on, for example, farming, and thus aren't able to follow up on their engage.
Pure burst Assassins with no hard CC like Katarina especially need to be wary of enemy CC. They tend to damage so quickly and so much that they can kill off DPS champs before their DPS ramps up, or they can significantly injure targets even if they dash away as fast as they could, so those tend to be less major concerns so long as the assassin in question is capable of bursting their enemy. What does become a problem is that, once interrupted, they quickly become sitting ducks for their opponents. Interestingly, champions of this kind tend to have tools to negate CC if played correctly. For example, Talon and Katarina both teleport behind their targets, giving them the capability to dodge many skill shots. Fighting against these champions simply comes down to not getting tempted to drop your hard CC every moment you get. Ahri should save her charm to interrupt Katarina's combo, not try to initiate a fight, for example. On the other hand, many Assassins have very little followup or escape tools, so if they aren't able to kill you outright with a single rotation, they tend not to engage. Staying topped off on health and being smart about trades and bullying can keep an Assassin at bay.