There is no single "fool proof" defensive strategy.
1) Walls work they simply do not work ALL the time. That does not mean it is worthless to employ the resources to wall off a settlement.
2) Turret towers work they simply do not work all the time. You will have attacks by enemies with heavy weapons that will destroy the turrets on one side of a tower allowing enemies to rush your settlements with little damage and causing lots of harm before they die if this is the only defence you employ.
3) Sirens work the just don't work all by themselves. Getting your settlers to all run towards a siren is a good plan but without support you'll just kill off your settles.
4) Guard post work, having plenty of guards at guard post will help in defence but alone guards will be overwhelmed and die.
A good settlement has multiple layers of defence.
A) You Should wall off your settlements because not ALL spawns of teh enemy happen inside your walls. So forcing the enemy to run to a gate before they can get inside means they do less damage. This investment is worth the effort as it funnels attackers into kill zones as they approach a gate. So do NOT let the simple fact that it isn't fool proof mean you ignore its value.
You should create turrets that target enemies inside your base, they can be placed in turret towers or on roofs but the key is to have them elevated as people have mentioned. This means if any enemy gets inside they are not free from attack which means they must deal with the turret or die. And if they are dealing with a turret it also means they are not attacking vital structures like power, settlers, purifiers ect.
C) Do wall up your generators. Create a building to store your generators when at all possible. There is a roof tile with a missing corner which is designed to allow you to run a power line out or into a building. USE IT. My castle generators are in a building with no door. This means i am not going to have these generators attacked because they are safe in a building. no line of sight means the enemy won't see them and target them.
D) Guard posts and Sirens. Every guard post in my settlement uses a siren. The siren add zero defence so many people over look it but NPCs ESPECIALLY Guards will spot an enemy and immediately run to a nearby siren and turn it on. This gets all the settlers running to the siren ready to support the guard. I place the siren tower so the switch is facing back of the guard post. This way a Guard can quickly reach the siren, turn it on and get back to the guard post and attack the enemy. having guards at every entrance to your settlement means that you have someone watching all access to the settlement which increases the likelihood of an attack being stopped quickly. Do equip your guards with armour and weapons.
E) Use multiple kinds of turrets not just a single type. Some enemies have high energy DR so only using laser turrets means they can last longer and do more damage to you because you are attacking them where their defences are the strongest. The same is true with enemies that have high ballistic DR if you only employ machinegun turrets. Use machine guns and lasers together to make sure you maximize your damage against all enemies. Spotlights should be used for every cluster of turrets so they can spot the enemy at night. Spot lights make seeing enemies at night easier for turrets and settlers alike USE them.
F) Do not use Missile turrets to defend the inside of your base. Missiles are AOE they will damage structures and settlers if they target an enemy inside your base. Use these turrets sparingly and make sure you place them in places where they can't fire into your settlements. not all your turrets have to be on the top level of a turret tower. i usually make concrete walls and one trick i use is setting them in slightly from the settlement boundary. This means that if the wall is two blocks high i can place laser and machine gun turrets on he top. Then place s series of small floor tile on the outside portion of the wall one story bellow the roof with three missile turrets. These turrets can only fire outside my settlement they are still elevated so have good field of view and they can't track an enemy inside and fire so it damages friend and foe alike.
G)make sure each turret draws power from a coupling or power pole independently not from a nearby turret. If you connect turrets via power only via a neighbouring turrets and not a pole you are ASKING for problems. A damage turret doesn't conduct electricity.
P_T1_T2_T3_T4 here is an example of what i mean. Turret T1 is drawing power directly from a power source/tower/coupling. And then you connect the second turret T2 to the first turret. And connect the third turret to the second and so on. They are all powered and everything seems fine. except if the enemy damages the first turret t1 then all he other turrets lose power. So DON'T do this. place a power pole in a central location so you can run a power wire from the pole to each individual turret independently. This way if any single turret is destroyed it does cut power to the other turrets down the line.
H) Do set up supply lines. It helps when settlements share food and water and junk, so if a settlement is damaged in an attack they will have plenty of supplies till you get around to repairing things, It also makes building settlements so much easier.
Hopefully these tips will help and explain why they help as well.