How can it be prevented that NASA would become (too) dependent on one rocket company or vice versa?
By doing exactly what NASA is doing right now.
In 2006 NASA's Commercial Orbital Transportation Services funded several companies to develop alternatives to resupply the International Space Station (ISS). Later that year NASA downselected to two suppliers, SpaceX and Rocketplane Kistler (RpK). NASA later awarded the remaining share of the monies that would have been given to RpK to Orbital Sciences when RpK failed to meet their obligations. NASA could instead have given all of those remaining monies to SpaceX or spend it on other programs. but it didn't do that because competition is good for NASA. NASA continues to use both SpaceX and Orbital Sciences (now a part of Northrop Grumman) to provide supplies to the ISS to avoid relying on one supplier.
NASA continued the concept of multiple commercial suppliers with its Commercial Crew program. Once again, NASA downselected to two competitors, this time SpaceX and Boeing. Competition is good for NASA. It avoids having to rely on one provider.
One solution could be not to go into business with private companies.
That is not an option; it hasn't been since the Gemini program. NASA does not build their own rockets or their own spacecraft. They do however design them and then let contracts to private companies to implement those designs.
That has been problematic as of late. The never-flown Ares launch vehicle project was canceled, only to be replaced by the not-yet flown Space Launch System (SLS). The SLS project alone has cost NASA $19 billion, a good deal more money than the combined monies NASA invested in SpaceX, RpK, Orbital, and Boeing for the Commercial Cargo and Commercial Crew programs.
One problem with the "old space" approach is an entrenched bureaucracy that knows only one way to do things. An even bigger problem is the US Congress, which has dictated which designs and which contractors NASA should use for the SLS. NASA hasn't built rockets for decades. Congress hasn't built a rocket, ever.