Explore your fundamental understanding of dependency injection and common design patterns in .NET with this engaging quiz. Strengthen your grasp of key principles, terminology, and practical scenarios to enhance your software design skills.
Which statement best describes dependency injection in the context of object-oriented programming?
Explanation: Dependency injection means dependencies are given to a class from the outside, promoting loose coupling. Copying functionality is not injection but duplication. Renaming variables and inheriting private fields are unrelated to the concept. Only the correct answer defines dependency injection accurately.
What is the main purpose of using the Singleton design pattern in a .NET application?
Explanation: The Singleton pattern ensures only one instance of a class exists, controlling global access. Allowing objects to communicate without direct references relates to observer or mediator patterns. Managing unrelated classes is the adapter or facade pattern's purpose. Constructors are still used with Singleton; they are not replaced.
In .NET dependency injection, which option correctly describes a transient service lifetime?
Explanation: Transient lifetime means a new instance is created upon each request, ensuring unique objects. A single shared instance describes a singleton. Once per HTTP request characterizes scoped services, and instantiation at startup does not define transient lifetime.
Why is constructor injection commonly preferred for dependency injection in .NET?
Explanation: Constructor injection lists required dependencies in the constructor, making them explicit and simplifying unit testing. Hiding dependencies complicates readability, not improves it. The use of interfaces is enhanced by constructor injection, and it actually reduces, not increases, tight coupling.
Which scenario best matches when to use the Factory design pattern?
Explanation: The Factory pattern creates objects without revealing the concrete classes to the client, perfect for multiple types. Creating a single instance is a Singleton; adding functionality dynamically is the decorator pattern, and restricting access is more related to the proxy pattern.
Why are interfaces important when implementing dependency injection in .NET components?
Explanation: Interfaces enable swapping different implementations easily, improving flexibility. Interfaces do not deal with private methods or restrict constructor usage. While useful for inheritance, that is not their main role in dependency injection.
What is the main role of the Decorator design pattern in application development?
Explanation: The Decorator pattern allows adding functionality to objects at runtime. Singleton relates to shared instances, not decoration. Database connections or hiding implementations are unrelated to the decorator's purpose.
In relation to dependency injection, what does the Dependency Inversion Principle suggest?
Explanation: The principle promotes reliance on abstractions, enhancing flexibility and testability. Depending only on concrete classes limits reusability. Minimizing method parameters and using global variables are unrelated to dependency inversion.
What happens when a service is registered as 'scoped' in .NET dependency injection?
Explanation: Scoped services get a unique instance per web request, ideal for context-based data. Single instances represent singleton, whereas transient gets new instances every use. Services can always be resolved if properly registered, so the last distractor is incorrect.
What is the main purpose of the Facade design pattern in software design?
Explanation: The Facade pattern gives a single, easy-to-use interface for complex systems. It does not enable multiple inheritance, direct memory access, or directly manage thread-based object creation. Its strength lies in reducing complexity for clients.
Which term best describes the process of informing the dependency injection system about which implementation to provide for an interface?
Explanation: Service registration links interfaces to implementations within the DI system. Method overriding changes behavior in derived classes, inheritance mapping is unrelated, and serialization refers to converting objects to data formats, not DI.
Why is the Liskov Substitution Principle important in the context of design patterns and dependency injection?
Explanation: The Liskov Substitution Principle maintains that derived classes should substitute base classes seamlessly. Single responsibility is a separate principle, not directly related. Choosing abstract classes over interfaces and limiting dependencies are not part of this principle.
What risk arises when a class intended for dependency injection contains overloaded constructors?
Explanation: Having multiple constructors can confuse the DI system about which to call, creating resolution errors. Injecting dependencies usually happens once, not twice. It doesn't prevent instantiation, but might fail if no clear constructor exists. Constructor overloads don't affect interface accessibility.
According to the open/closed principle, how should classes be designed?
Explanation: This principle promotes extending class behavior without altering existing code, aiding maintainability. Direct modification of variables is discouraged. There is no requirement that classes only hold abstract methods, and multiple interface implementations are allowed.
Which benefit is most directly gained by using interfaces in dependency injection?
Explanation: Interfaces make it simple to swap out real dependencies for test versions, facilitating unit testing. They have no bearing on method staticness, mandatory variable initialization, or application size.
What is the main goal of the Builder design pattern when constructing objects?
Explanation: The Builder pattern constructs complex objects incrementally, aiding clarity. Guaranteeing a single object is Singleton's aim. Permission checking and ensuring synchronous processing have nothing to do with Builder's core objective.