DI/Env/Crud/Tests Flashcards

1
Q

Services

A

‘Service’ is a class that contains business logic such as business calculations, business validations that are specific to the domain of the client’s business.

Service is an abstraction layer (middle layer) between presentation layer (or application layer) and data layer.

It makes the business logic separated from presentation layer and data layer.

It makes the business logic to be unit testable easily.

Will be invoked by controller.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Direct Dependency

A

Controller has a filed of a service and call the method of a service.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Dependency Problem

A

Higher-level modules depend on lower-level modules.

Means, both are tightly-coupled.

The developer of higher-level module SHOULD WAIT until the completion of development of lower-level module.

Requires much code changes in to interchange an alternative lower-level module.

Any changes made in the lower-level module effects changes in the higher-level module.

Difficult to test a single module without effecting / testing the other module.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Dependency Inversion Principle

A

Dependency Inversion Principle (DIP) is a design principle (guideline), which is a solution for the dependency problem.

“The higher-level modules (clients) SHOULD NOT depend on low-level modules (dependencies).

Both should depend on abstractions (interfaces or abstract class).”

“Abstractions should not depend on details (both client and dependency).

Details (both client and dependency) should depend on abstractions.”

The interface is controlled by the client.
Both client and dependency depend on abstraction.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Inversion of Control (IoC)

A

Inversion of Control (IoC) is a design pattern (reusable solution for a common problem), which suggests “IoC container” for implementation of Dependency Inversion Principle (DIP).

It inverses the control by shifting the control to IoC container.

“Don’t call us, we will call you” pattern.

It can be implemented by other design patterns such as events, service locator, dependency injection etc.

Controller creates a filed of a interface.
Method that implements interface gets called.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Adding dependencies

A

All dependencies should be added into the IServiceCollection (acts as IoC container).

builder.Services.Add(
new ServiceDescriptor(
typeof (Interface),
typeof (Service)
ServiceLifetime.LifeTime //Transient, Scoped, Singleton
)
);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Dependency Injection (DI)

A

Dependency injection (DI) is a design pattern, which is a technique for achieving “Inversion of Control (IoC)” between clients and their dependencies.

It allows you to inject (supply) a concrete implementation object of a low-level component into a high-level component.

The client class receives the dependency object as a parameter either in the constructor or in a method.

Service that implements interface is a DI/IoC Container.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Service Lifetime

A

(Transient, Scoped, Singleton)

A service lifetime indicates when a new object of the service has to be created by the IoC / DI container.

Transient: Per injection

Scoped: Per scope (browser request)

Singleton: For entire application lifetime.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Transient

A

Transient lifetime service objects are created each time when they are injected.

Service instances are disposed at the end of the scope (usually, a browser request)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Scoped

A

Scoped lifetime service objects are created once per a scope (usually, a browser request).

Service instances are disposed at the end of the scope (usually, a browser request).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Singleton

A

Singleton lifetime service objects are created for the first time when the are requested.

Service instances are disposed at application shutdown.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Autofac

A

Autofac is another IoC container library for .Net Core.

Means, both are tightly-coupled.
Microsoft.Extensions.DependencyInjection [vs] Autofac
https://autofac.readthedocs.io/en/latest/getting-started/index.html

Alternative to the Microsoft.Extensions

Lifetimes: InstancePerDependency, InstancePerLifetimeScope, SingleInstance, InstancePerOwned, InstancePerMatchingLifetimeScope
Metadata for services: Supported
Decorators: Supported

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Enviroment

A

An environment represents is a system in which the application is deployed and executed.

Development
The environment, where the developer makes changes in the code, commits code to the source control.

Staging
The environment, where the application runs on a server, from which other developers and quality controllers access the application.

Production
The environment, where the real end-users access the application.
Shortly, it’s where the application “live” to the audience.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Gets or sets name of the environment.

A

IWebHostEnvironment
DOTNET_ENVIRONMENT or ASPNETCORE_ENVIRONMENT.

IsDevelopment()
Returns Boolean true, if the current environment name is “Development”.

IsStaging()
Returns Boolean true, if the current environment name is “Staging”.

IsProduction()
Returns Boolean true, if the current environment name is “Production”.

IsEnvironment(string environmentName)
Returns Boolean true, if the current environment name matches with the specified environment.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Access Environment in Controller and other classes

A

using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Hosting;

public class ControllerName : Controller
{
private readonly IWebHostEnvironment _webHost;

public ControllerName(IWebHostEnvironment webHost)
{
_webHost = webHost;
}
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Configuration Settings

A

Configuration (or configuration settings) are the constant key/value pairs that are set at a common location and can be read from anywhere in the same application.

17
Q

Configuration Sources

A

appsettings.json
Environment Variables
File Configuration (JSON, INI or XML files)
In-Memory Configuration
Secret Manager

18
Q

Options Pattern

A

Options pattern uses custom classes to specify what configuration settings are to be loaded into properties.

Examples: Reading the specific connections strings out of many configuration settings.

The option class should be a non-abstract class with a public parameterless constructor.

Public read-write properties are bound.
Fields are not bound.

19
Q

Secrets Manager

A

The ‘secrets manager ‘ stores the user secrets (sensitive configuration data) in a separate location on the developer machine.

20
Q

Http Client

A

HttpClient is a class for sending HTTP requests to a specific HTTP resource (using its URL) and receiving HTTP responses from the same.

Examples: Making a request to a third-party weather API, ChatGPT etc.

21
Q

IHttpClientFactory

A

IHttpClientFactory is an interface that provides a method called **CreateClient() **that creates a new instance of HttpClient class and also automatically disposes the same instance (closes the connection) immediately after usage.

22
Q

HttpClient-methods and Properties

A

Properties:
1. BaseAddress
2. DefaultRequestHeaders

Methods
1. GetAsync()
2. PostAsync()
3. PutAsync()
4. DeleteAsync()

23
Q

EntityFrameworkCore

A

EntityFrameworkCore is light-weight, extensible and cross-platform framework for accessing databases in .NET applications.

It is the most-used database framework for Asp.Net Core Apps.

24
Q

Pros & Cons of EntityFrameworkCore

A
  1. Shorter Code
    The CRUD operations / calling stored procedures are done with shorter amount of code than ADO.NET.
  2. Performance
    EFCore performs slower than ADO.NET.
    So ADO.NET or its alternatives (such as Dapper) are recommended for larger & high-traffic applications.
  3. Strongly-Typed
    The columns as created as properties in model class.
    So the Intellisense offers columns of the table as properties, while writing the code.
    Plus, the developer need not convert data types of values; it’s automatically done by EFCore itself.
25
Q

DbContext and DbSet

A

DbContext
An instance of DbContext is responsible to hold a set of DbSets’ and represent a connection with database.

DbSet
Represents a single database table; each column is represented as a model property.

26
Q

Advantages of Stored Procedure

A

Single database call
You can execute multiple / complex SQL statements with a single database call.

As a result, you’ll get:
Better performance (as you reduce the number of database calls)
Complex database operations such as using temporary tables / cursors becomes easier.

Maintainability
The SQL statements can be changed easily WITHOUT modifying anything in the application source code (as long as inputs and outputs doesn’t change)

27
Q

EF - Async Operations

A

async
The method is awaitable.
Can execute I/O bound code or CPU-bound code

await
Waits for the I/O bound or CPU-bound code execution gets completed.
After completion, it returns the return value.

28
Q

Best Practices of Unit Tests

A

Isolated / Stand-alone
(separated from any other dependencies such as file system or database)

Test single method at-a-time
(should not test more than one method in a single test case)

Unordered
(can be executed in any order)

Fast
(Tests should take little time to run (about few milliseconds))

Repeatable
(Tests can run repeatedly but should give same result, if no changes in the actual source code)

Timely
(Time taken for writing a test case should not take longer time, than then time taken for writing the code that is being tested)

29
Q

Mocking the DbContext

A

Test Double

A “test double” is an object that look and behave like their production equivalent objects.
A “test double” is an object that look and behave like their production equivalent objects.

Fake
An object that providers an alternative (dummy) implementation of an interface

Mock
An object on which you fix specific return value for each individual method or property, without actual / full implementation of it.

30
Q

AutoFixture

A

AutoFixture generates objects of the specified classes and their properties with some fake values based their data types.

31
Q

Fluent Assertions

A

Fluent Assertions are a set of extension methods to make the assertions in unit testing more readable and human-friendly.

32
Q

Repository

A

Repository (or Repository Pattern) is an abstraction between Data Access Layer (EF DbContext) and business logic layer (Service) of the application.

Controller calls service, that calls repository, that calls db;

33
Q

Benefits of Repository Pattern

A

Loosely-coupled business logic (service) & data access.
(You can independently develop them).

Changing data store
(You can create alternative repository implementation for another data store, when needed).

Unit Testing
(Mocking the repository is much easier (and preferred) than mocking DbContext).