DI/Env/Crud/Tests Flashcards
Services
‘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.
Direct Dependency
Controller has a filed of a service and call the method of a service.
Dependency Problem
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.
Dependency Inversion Principle
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.
Inversion of Control (IoC)
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.
Adding dependencies
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
)
);
Dependency Injection (DI)
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.
Service Lifetime
(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.
Transient
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)
Scoped
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).
Singleton
Singleton lifetime service objects are created for the first time when the are requested.
Service instances are disposed at application shutdown.
Autofac
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
Enviroment
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.
Gets or sets name of the environment.
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.
Access Environment in Controller and other classes
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Hosting;
public class ControllerName : Controller
{
private readonly IWebHostEnvironment _webHost;
public ControllerName(IWebHostEnvironment webHost)
{
_webHost = webHost;
}
}