Dependency Injection in .NET Core 2.0

Author: Marino Posadas   Tadit Dash  

Publisher: Packt Publishing‎

Publication year: 2017

E-ISBN: 9781787129573

P-ISBN(Paperback): 9781787121300

Subject: TP312 程序语言、算法语言

Language: ENG

Access to resources Favorite

Disclaimer: Any content in publications that violate the sovereignty, the constitution or regulations of the PRC is not accepted or approved by CNPIEC.

Dependency Injection in .NET Core 2.0

Chapter

Chapter 1: The SOLID Principles of Software Design

In the beginning

.NET and .NET Core

About .NET Core

ASP.NET Core

About the IDE used in this book

Other foundational changes in .NET Core

The SOLID principles

The Single Responsibility Principle (SRP)

The background of the Separation of Concerns (SoC)

Well-known examples of Separation of Concerns

A basic sample of Separation of Concerns

Another sample

The Open/Closed principle

Back to our sample

The Liskov Substitution principle

Back to the code again

Other implementations of LSP in .NET

The Interface Segregation principle

Another sample

The Dependency Inversion principle

The (more or less) canonical example

Other ways to implement Dependency Inversion

Summary

Chapter 2: Dependency Injection and IoC Containers

The Dependency Principle in more detail

Let's look at an example

Aspects of Dependency Injection

Object Composition

Object Lifetime

Interception

Ways to implement DI

Property Injection in action

Method Injection in practice

.NET event architecture as Model Injection

DI containers

Service locators

DI Containers for .NET

Using Unity Container

Using Castle Windsor

Using StructureMap

Using Autofac

Dependency Injection in other frameworks

DI in Angular

Our first demo

Summary

Chapter 3: Introducing Dependency Injection in .NET Core 2.0

The main characteristics of .NET Core

The main advantages of .NET Core

Installation of .NET Core in the IDE

Installation path for .NET Core in Visual Studio 2015

.NET Core in Visual Studio 2017

Types of deployment

Checking other dependencies in .NET Core

Dependency Injection in .NET Core

DI Architecture and Object's Lifetime

The class ActivatorUtilities and other helpers

The Microsoft.Extensions.DependencyInjection container

Checking the object's lifetime

Mapping interfaces to instance classes

The concept of Scope Applied to services

Other extensions with DI functionality

A reflection on the structure of .NET Core

LogLevels Severity

Summary

Chapter 4: Dependency Injection in ASP.NET Core

ASP.NET Core from the command-line tools

Creating the minimum possible application

Changing to ASP .NET Core

Middleware

The Startup class and Dependency Injection

Code explanation

The New ASP.NET servers

Kestrel

WebListener

Dependency Inversion in the architecture - OWIN

Analyzing the default template

Configuration files

The Entry Point

The default Startup class

Dependency Injection in ASP.NET Core

Services provided by ASP.NET Core

The relation of services available at startup

Identifying Services in the Web Application template

Using Dependency Injection in ASP.NET MVC Views

Garbage Collection and Custom Services

Using Custom Services through Dependency Injection

Services and data management

Using Dependency Injection inside Views

Summary

Chapter 5: Object Composition

Understanding object relationships

Object Composition

Types of Object Composition

Composition

Consider an example

Contrasting features of Composition

Why are subclasses inside Composition?

Important notes

Aggregation

Consider an example

Composition versus Aggregation

Advantages and disadvantages

Other important relationships

Association

Example

Explanation of the preceding code

Types of Association

Reflexive Association

Indirect Association

Composition over Inheritance

Inheritance

Example on User class

New User Type

Problem which we come across

The solution to the problem

Role of Object Composition in Dependency Injection

Composition Root

Composing .NET Core 2.0 Console application

Object Composition in ASP.NET Core MVC 2.0

Summary

Chapter 6: Object Lifetime

Manage the object life cycle

Stack versus heap

Managed and unmanaged resources

Generations

Object creation

The Copy constructor

Object destruction

Finalize

The IDisposable interface

Consider an example

Implementing the IDisposable Interface

Step1 -  Basic structure of class

Step2 - Defining a Dispose Overload method

Step3 -Modifying the Dispose(bool) for Derived classes

Step 4 - Handling duplicate Dispose calls

Object lifetime management in .NET Core

Object creation

Designing the interfaces

The Concrete class

The Service class

The controller

View

Startup ConfigureServices

Object lifetimes

Singleton

Scoped

Transient

Instance

Object disposal

When to choose what?

Relationship and dependencies among lifetimes

Singleton depending on Scoped and Transient

Scoped depending on Singleton and Transient

Transient depending on Singleton and Scoped

Summary

Chapter 7: Interception

Introducing Interception

The decorator

Patterns and principles for Interception

Benefits of the Decorator approach

Problems of the Decorator approach

Aspect-oriented programming

Cross-cutting concerns

Aspect

Aspect characteristics

Advantages

Aspect attached locations

Types of AOP

Investigating Interception

The Interception process

Castle Windsor

Demonstration for using the Castle Windsor

Creating an Interceptor

Attaching an Interceptor

Intermediate Language (IL) Weaving

IL Weaving process

Creating an aspect

Attaching the aspect

Interception in ASP.NET Core

Filters

Global filter

Attributes

Middleware

Registration

Execution

Ordering

Summary

Chapter 8: Patterns - Dependency Injection

Dependency Inversion Principle

Problems

Solution

Inversion of Control (IoC)

Patterns

Constructor Injection pattern

The problem

The solution

Curveball

Injecting with .NET Core 2.0

Implementation inside ASP.NET Core 2.0

ControllerActivatorProvider.cs

Importance of the Constructor Injection pattern

Advantages of the Constructor Injection pattern

Disadvantages of the Constructor Injection pattern

Property Injection pattern

Curveball

Advantages of the Property Injection Pattern

Disadvantages of the Property Injection Pattern

Method Injection Pattern

Curveball

Injecting with .NET Core 2.0

Implementation inside .NET Core 2.0

MvcServiceCollectionExtensions.cs

Ambient context

Curveball

Implementation inside .NET Core 2.0

The advantages of the Ambient Context

The disadvantages of the Ambient Context

Summary

Chapter 9: Anti-Patterns and Misconceptions on Dependency Injection

When does Dependency Injection become an anti-pattern?

Anti-patterns

Control Freak

Problem

Concrete Factory

Abstract Factory

Static Factory

Solution

Poor Man's DI

The approach

Problem

Solution

Bastard Injection

Problem

Solution

Constrained Construction

Problem

Solution

Service Locator

Design

Advantages

Problem

Code reusability

Solution

Refactoring steps

Summary

Chapter 10: Dependency Injection in Other JavaScript Frameworks

TypeScript

Architectural changes

Modules in TypeScript

External modules

Dependency Injection in TypeScript

Angular

AngularJS

Examples using Visual Studio 2017

Understanding the structure of AngularJS bootstrap

Data access and Dependency Injection

Summing up Dependency Injection features inside AngularJS

Angular 2+

Microsoft's TypeScript is the preferred language in Angular 2+ 

Angular tools

Working with Angular

Editing the initial project

The structure of the main module

DI in Angular 4

The concept of a provider

Wrapping it up

Summary

Chapter 11: Best Practices and Other Related Techniques

Tightly coupled systems

The problem

The solution - refactoring with DI

Interface extraction

Layered architecture

The problem - The concrete class reference again

Misuse of business and Data Access Layer assemblies

Best practice for layered architecture

Managed Extensibility Framework (MEF)

Layers and MEF implementation

Introduction of IUser

The IModuleRegistrar interface

The ModuleRegistrar class

The IModule interface

The ModuleInit class

The ModuleLoader class

Executing the ModuleLoader.LoaderContainer() method from Web App

What have we achieved with MEF?

Layers are separated

All classes inside layers are now internal

No concrete class instantiation inside the UI Layer

More layers can be added to the architecture

Conclusion

Summary

Index

The users who browse this book also browse