A General Fredholm Theory and Applications