Niall McGinn bagged a hat-trick as Aberdeen returned to winning ways in the SPL with a 3-1 win at Dundee.