Software Engineering Quiz on DI and Unit Testing

0.0(0)
studied byStudied by 1 person
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/73

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

74 Terms

1
New cards

Article 1

2
New cards

What is the root of the intrinsic issues of Unity’s framework?

how Unity makes injecting dependencies upon entities hard

3
New cards

What is a dependency?

an object needed by another object to execute its code

4
New cards

If Object A needs Object B to execute its code, then B is a ______ of A and when its passed in we say B is ______ into A

dependency, injected

5
New cards

What is the Unity code framework designed around?

Bilas’ Data Driven GameObject System

6
New cards

What are the benefits to a component-based entity framework?

  • push users to favor composition over inheritance (build complex behavior by combining simple objects (composition) instead of creating deep class hierarchies (inheritance).

  • keeps classes small, clean, and focused, modular + single responsibility!!

7
New cards

What are the entities in Unity?

GameObjects

8
New cards

What are the components based on in Unity?

MonoBehavior implementations

9
New cards

What does the Entity Component structure rely on?

components define the whole entity’s logic

10
New cards

What are the five principles that must be obeyed in order to use a Gameobjects framework properly?

Monobehaviors must operate within the GameObjects they are attached to as this allows for encapsulation (Encapsulation means hiding the internal details of how something works and only showing what’s necessary to the outside.) Components must handle only the entity they are attached to

Monobehaviors are modular (Modular software engineering means breaking a program into smaller, independent parts (modules) that each do one specific job.) and single responsibility classes that can be reused by other game objects

behavior of a gameobject should be added through new MonoBehaviors instead of adding code to an existing Monobehavior

GameObjects should not be created without a view (Don't create empty GameObjects just to use as managers or containers.)

Monobehaviors can know other components on the same GameObject using GetComponent but shoudln’t know monobehaviors from other gameobjects

11
New cards

Instead of specializing an entity _____ through ______, components allow us to expand behavior ______

vertically, polymorphism, horizontally

12
New cards

Why should GameObjects without a view not be used?

GameObjects all have transforms implying they should be in the world to use that transform

13
New cards

What is the problem with Unity asking to write monobehaviors for all cases?

the logic might not be based on an entity

14
New cards

What’s wrong with Gameobject.Find()

  • slow function

  • somebody could’ve renamed the object

  • can lead to runtime errors not caught in compiletime (really hard to tell which GameObject it can’t find if multiple .Finds)

15
New cards

What’s wrong with Object.FindObjectOfType?

  • bad version of Service Locator pattern as we’re not allowed to abstract the interface (eg. dependency injection)

  • Unity discourages use of interfaces and instead pushes user to use Monobehaviors even when the use of Monobehavior not necessary

16
New cards

what is wrong with the singleton pattern

so many reasons (break encapsulation, hide dependencies, impossible to write proper unit tests, implementation of service instead of abstraction, make refactoring awkward, memory leaks)

code will become a giant spaghetti mess over time

will lead to refactoring to become a huge operation

your design will smell as its your only way of injecting dependencies

singletons rely on public functions which can change states in the singleton itself (very hard to track!)

can’t be designed with inversion of control in mind

forced to write functions such as IsSingletonReady leaving control of internal states to external entites

17
New cards

main reason why communication is injected

to allow objects to communicate with one another

18
New cards

What is the solutions to injecting dependencies?

resolve dependencies through service locator pattern (only to inject actual stateless services), injecting dependencies manually through constructors/setters (most optimal but hard), exploiting reflection through IOC container (Using reflection to automatically discover and create the necessary objects and inject them into your classes at runtime, without writing all the wiring code yourself.), singletons

either that or IOC containers

19
New cards

What does IOC container stand for and why and what is it?

Inversion of Control container bc the design is thought in such a way that the dependencies are never created by the user, but they are lazily created by the container when they are required.

container that creates and contains the dependencies that must be injected inside the application objects.

20
New cards

Article 2

21
New cards

t/f keeping your code maintainable is as important as getting it done

true

22
New cards

What is the most important condition to writing maintainable code?

simple code

23
New cards

What is composition root?

where the IoC container must be initialized before everything else starts using it

24
New cards

T/F. a project can have multiple composition roots

true

25
New cards

What is the cardinal issue of the whole Unity code design?

Unity doesn’t really have a single entry point that allows objects to be initialized

26
New cards

Dependency graph

dependency waterfall

one class dependent on another classs which is dependent on another

27
New cards

t/f if we don’t consider objects that must be created dynamically after the start-up phase, all the dependencies can be solved right at the beginning of the application, within the Composition Root context.

true

28
New cards

What does the Bind method do?

every time a dependency of a known type is found, it must be solved with an instance of the type bound to it

29
New cards

What is the [Inject] keyword for?

sed to mark a property or field where a dependency should be automatically filled in by an IoC (Inversion of Control) container.

30
New cards

Compare IOC container to singleton?

gives same flexibility without risking wildly using static classes everywhere/avoiding memory leaks

dependencies correctly injected only if apart of dependency graph

can be also injected through interfaces, which allows changing implementations without changing the code that uses them.

31
New cards

What is the most powerful benefit behind the IoC container?

the code will not depend on the implementation of the classes anymore, but always on their abstractions (if interfaces are used)

32
New cards

What to do if you want to create objects while the game is running without using new Keyword

use Factories instead

can use the container to inject dependencies into the object that they create

factories are the only classes that can use the Container outside the Composition Root

33
New cards

IoC container and Monobehavior relation

IoC container helps shift code patterns from intensive use of Monobehavior to use of normal classes that implement interfaces

34
New cards

How to make IoC container work with Unity game objects loaded in the scene?

To make IoC work with Unity's scene-loaded objects, here's what the author suggests:

  1. Create a special GameObject, called something like "Context" or "IoCContext".

  2. Attach a script with your IoC container to this GameObject.

  3. In this script’s Awake() method, the container will:

    • Scan all child GameObjects

    • Find all MonoBehaviours

    • Inject their dependencies using [Inject] tags — before any Start() methods are called.

This works because Awake() is called before Start(), so dependencies are available in time.

35
New cards

T/F IoC container great with MVP

T

36
New cards

T/F IOC container library shouldnt support hierarchal containers and why?

false

bad practice to just bind every possible dependency in a single container, as this won’t promote separation of concerns.

they encapsulate the dependencies needed for a specific context, they will also inherit dependencies from parent containers.

37
New cards

Article 3

38
New cards

What is the main problem with dependencies?

the IoC container solution was scarily used too often as an alternative to the Singleton pattern to inject dependencies

TOO MANY DEPENDENCIES

39
New cards

Why is dependency injection not supposed to do and why?

couple object implementations,

interfaces are passed instead of classes. This is necessary to allow changing implementations in the composition root without needing to touch any existing class code (open/closed in SOLID)

40
New cards

Should you pass in many dependencies in the IoC container and why?

you shouldn’t because its badly layered/structured code (j coupling now with interfaces becoming irreleavant)

41
New cards

T/F IoC containers should only be used by experienced teams or else people will just start injecting dependencies everywhere

true

42
New cards

T/F All forms of communications involve dependency injection.

T

43
New cards

What are several ways of letting objects communicate?

interface injection: usually the interface A is injected in B, B is coupled with A [e.g.: Inside a B method A is used, B.something() { A.something());]

Standard events (callbacks): Inside A, B is injected to expose the event, B.onSomething += A.Something

Commands: B and A are uncoupled, B could call a command that calls a method of A. Commands are great to encapsulate business logic that could potentially change often. A Command Factory is usually injected in B.

Mediator: usually B and A do not know each other, but know their mediator. B and A pass themselves into the mediator and the mediator wires the communication between them (i.e.: through events or interfaces). Alternatively, B and A are passed to the mediator in the Composition Root, making A and B dependencies for the Mediator and not the other way around.

Various other patterns like Observers, Event Bus[2], Event Queue[3] where A and B do not know each other but are put in communication through these patterns like it happens with the Mediators.

44
New cards

What are the two worst methods of the ones mentioned above?

they couple interfaces that could change over time.

45
New cards

T/F Dynamic objects can still be dependencies

false

46
New cards

What are services?

units of logic that allow a client to communicate with external devices, service can be executed from inside Commands or from inside GUI Presenters

47
New cards

Where are services usually stored?

service layer in an interface

48
New cards

Why is an IoC container called Inversion of Control container?

IoC containers take away the responsibility to create objects from the user and shift it to the framework. In this sense, they “invert” the control over the creation of dependencies.

49
New cards

Does IoC give control to specialized or abstract classes and what does it give control?

abstract

framework classes should control both the dependency creation (Inversion Of Creation Control) and the logic execution (Inversion of Flow Control) of more specialised objects.

50
New cards

T/F factories are always created and passed as a dependency from the Composition Root.

true

51
New cards

Why is inverting the creation control important?

code becomes independent of constructor implementation (reduces coupling as dependency can be passed as interface instead)

code only dependent on abstraction not implementation

objects injected come already created with its dependencies resolved

flow of code can change according to context

52
New cards

Can Inversion of Creation Control be achieved without using an IoC container?

yes

53
New cards

What is Inversion of Flow control?

Inversion of control (IoC) describes a design in which custom-written portions of a computer program receive the flow of control from a generic, reusable library. A software architecture with this design inverts control as compared to traditional procedural programming: in traditional programming, the custom code that expresses the purpose of the program calls into reusable libraries to take care of generic tasks, but with inversion of control, it is the reusable code that calls into the custom, or task-specific, code

54
New cards

Article 5

55
New cards

How does the Entities Component System design work?

entities are essentially ids

components are units of data. they wrap data that can be shared between systems and do not have logic to manage data

systems are classes where the logic lies

56
New cards

how we can apply the Open/Close principle without ever breaking it

every time we need to introduce a new specific behaviour, we must create a new System that simply uses components as data to apply it.

57
New cards

GameEngine is a _____ while monobehaviors are _____

high-level module, low level module

58
New cards

t/f DIP wants us to find the common behaviours in our implementations, abstract them and package them away in higher-level modules.

t

59
New cards

What is strategy pattern?

behavioral design pattern that lets you define a family of algorithms, put each of them into a separate class, and make their objects interchangeable.

60
New cards

Hollywood principle

specialized code must never call directly methods of more high-level module classes. It is the high-level module mechanism to control the flow of our specialised code.

61
New cards

The author believes that IoC should have a _____ approach

top down

should be doing more abstract stuff and making interfaces for them in lower end modules

62
New cards

What is a utility class?

every class that exposes many public functions ends up being a Utility class, used in several places. Utility classes should be static and stateless. Stateful Utility classes are always a design error.

63
New cards

What is a way to achieve Inversion of Flow Control

events

64
New cards

take home message

The take-home message here is that IoC and DIP are asking us to step away from the Bilas/Unity Gameobject approach that tells us to write behaviours in our specialised components but instead asks us to find the common behaviours of our entities, abstract and pack them in higher-level modules, make our specialised entities implement the high-level interfaces and eventually register them in the high-level managers to be later on processed by the framework.

65
New cards

MVC Pattern Articles Division

66
New cards

Why is it difficult to unit test in Unity?

don’t have direct access to the Composition root

67
New cards

What is a solution to the unit testing problem in Unity?

create a GameObject in an EditMode unit test and then call AddComponent<> to it to initialize an object and then use method injection or setter injection to inject dependencies, but this isn’t the most elegant way.


test these objects in PlayMode tests (essentially integration tests), but these tests run slower and serve a different person than unit testing does.

create composition root and AfterSceneLoad

68
New cards

Why do we use AfterSceneLoad instead of BeforeSceneLoad

we need the game objects to be loaded into the scene

69
New cards

Model-view-controller pattern (MVC)

  • Model: Manages data, business logic, and interacts with databases.

  • View: Represents the user interface and displays data from the model.

  • Controller: Handles user input, interacts with the Model to update data, and updates the View to reflect changes

70
New cards

Dependency inversion principle rules

Recall that the D in SOLID stands for the Dependency Inversion Principle.

A. HIGH LEVEL MODULES SHOULD NOT DEPEND UPON LOW LEVEL MODULES.

BOTH SHOULD DEPEND UPON ABSTRACTIONS.

B. ABSTRACTIONS SHOULD NOT DEPEND UPON DETAILS. DETAILS SHOULD

DEPEND UPON ABSTRACTIONS.

71
New cards

What is an out parameter modifier?

return value is a boolean that lets us know

if the operation succeeded and the out parameter has an actual value or is null.

72
New cards

if you are practicing strict Test Driven Development (TDD) when would you write unit tests

first

73
New cards

T/F Model view controller pattern makes your code more complex but maintainable

true

74
New cards

Model view presenter pattern

Presenter

replaces the Controller and the Presenter is actually responsible for observing changes in the

model and updating the View. The Presenter would listen to UI events (such as clicking a UI

button or typing into a UI text box).