All jobs / Backend developer / Architecture
Backend developer interview question · 0–1 years
Why would a class take its database client as a constructor parameter instead of creating one inside itself?
The strongest answer
The class stops deciding where the dependency comes from, so it can be given a different implementation — a test double, another configuration — and the wiring lives in one visible place instead of every constructor
Why it works
Right — testability is the visible benefit; the real one is that construction and configuration move to one place you can read.
What a good answer covers
- Ask for dependencies; do not create them.
- Wiring lives in one place.
- Swap implementations freely.
- Avoid hidden global instances.
Answers that fall short
- Close, but not the strongest: So the class can be tested without a real database
True, and only half of it: the same property lets you swap the implementation, wrap it, or change its lifetime in one place. - Weak: Because the framework has a container, and dependencies are supposed to be registered there
The container is a convenience. Injection works without one, and the reason is decoupling, not framework convention. - Red flag: I would use a static shared instance, so any class can reach the client without it being passed around
A static instance makes every class depend on a global you cannot substitute, and hides the dependency from the constructor.
Who gets asked this
More Architecture questions
- The interviewer asks: “Should this service be CQRS?” Strongest answer?
- The interviewer asks: “Event-driven or request/response for “order placed → email + warehouse”?” Strongest…
- The interviewer asks: “How do you change an event payload that old consumers still read?” Strongest answer?
- The interviewer asks: “Billing keeps importing Order's entity. What's the design problem?” Strongest answer?
- The interviewer asks: “How do you isolate tenants in a multi-tenant backend?” Strongest answer?
- The interviewer asks: “A bad deploy took all regions with it. What architecture would have contained it?”…
- A team proposes ports and adapters for a service that reads a queue, writes to one database, and will never…
- The interviewer asks: “Two years of events, consumers you don't own. How do you evolve the schema?” Strongest…