Unit control is pretty poor right now, but there are some ways to improve their behavior using specifically designed passegeways with drawbridges/floodgates/doors to cut off certain parts of them. Here are some of the possible methods:
The simplest, perhaps, is "a frontline",
as described by Sethatos, and is best suited for a small (10-20 troops), well-trained, well-equipped military. You make a long passegeway, 5-6 tile wide and position your militias at the very end of it, so that they'd have to fight through the entire invasion before they can get out in the open (which you don't want them to do). You can further improve this tactic by positioning your dwarves in a 5x2 formation using the 'protect burrow' order, behind a floodgate "wall" with a single open door to lure the invaders towards this position: once the enemy is close, pull the lever to open the floodgates and unleash your military.
A bit more intricate, but improvable design is another 5-6 tile corridor, but this time it turns (90 degrees, in any direction where you got space for it) each ~15 tiles, forming sections. At the end of each section you can place a cutting off drawbridge: as the invasion stretches throughout the tunnel, you pull the levers (you need a separate one for each bridge) and divide their forces, so then your militias can deal with each section one by one, getting as much time to rest and recuperate as they need in between. You can improve it for the use of a larger military by adding "formation rooms" (similar to the one you have at the end of the tunnel, for your troops' positioning, but hidden behind a drawbridge instead, as you don't want some troll to mess everything up) at the end of each section in such a way that once the section is cut-off by a bridge and you unleash your main squad(s), you can unleash an additional one (or two) to attack the rear of enemy forces in the same section. If you have ranged troops, you can also expand this corridor upwards and carve fortifications some 1-2 z-levels higher in the end wall of each section: from there, your marksdwarves can ceaslessly fire at the enemy, as long as they got ammo; one important thing to do, though, is to have floodgates/doors/a drawbridge on the same level as fortifications, but outside of them, which can be locked by a lever to stop your markdwarves from firing or being fired at, in case the things go haywire; another important note on markdwarves in general is to make sure they have
no melee access to the enemy whatsoever, lest they decide to go in melee once out of ammo. This tactic works well for any kind of military, depending on the improvements you make.
Another alternative is an "ambush tunnel". A 1 tile wide, snake-like corridor that is separated by turns and drawbridges into 10-12 tile long
main sections. The main sections are parallel to each other and are connected by 3
or 5 tile long
secondary sections. The most important part? The walls of the main sections are two 10x1 drawbridges, with your military squads positioned behind each one in a 10x1 burrow. As the invasion stretchses throughout the tunnel, you lock the section-dividing bridges and then open the wall-bridges. The result is thin lines of 10-12 invaders (sometimes a bit more, due to them crawling occasionally) being attacked from each side by a 10-dwarf squad in full force. Obviously, this tactic works best for a large, melee-oriented military that isn't necessarily well-trainded or well-equipped.