centric corporate templateadiftene/scoala/2018/ip/...angular has its own dependency injection...

66
DEPENDENCY INJECTION May 29, 2018 FLORIN OLARIU

Upload: others

Post on 03-Jun-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Centric corporate templateadiftene/Scoala/2018/IP/...Angular has its own dependency injection framework, and you really can't build an Angular application without it. This page covers

DEPENDENCY INJECTION

May 29, 2018

FLORIN OLARIU

Page 2: Centric corporate templateadiftene/Scoala/2018/IP/...Angular has its own dependency injection framework, and you really can't build an Angular application without it. This page covers

ABOUT ME

• 22+ years experience in software development(mainly as

Technical Lead/Architect)

• Started in Centric 6 years ago

• Current role: .NET Discipline Coordinator/Domain

Architect in Centric

• Professor Collaborator - Alexandru Ioan Cuza University

– Computer Scince Faculty• PhD -> Increasing security for online payments by using

biometrical algorithms

• Introduction to .NET/Software engineering

TITLE PRESENTATION May 29, 2018

Page 3: Centric corporate templateadiftene/Scoala/2018/IP/...Angular has its own dependency injection framework, and you really can't build an Angular application without it. This page covers

AGENDA

• 7 Deadly Developer Sins• Dependency Injection Pattern Overview

• Demo : .NET Core Sample• Demo : Java Spring Sample• Demo : Angular Sample

• Unit testing• Short Story• Sandro Mancuso -> About unit testing story• Why Unit Testing?• What Is A Unit Test?• Unit Test Example• Unit Test Pyramid• Tips For Writing Great Unit Tests• Testing Goals• Demo : .NET Core sample

• Suggestions/Materials• Summary• Questions

May 29, 2018TITLE PRESENTATION

Page 4: Centric corporate templateadiftene/Scoala/2018/IP/...Angular has its own dependency injection framework, and you really can't build an Angular application without it. This page covers

7 DEADLY DEVELOPER SINS

Page 5: Centric corporate templateadiftene/Scoala/2018/IP/...Angular has its own dependency injection framework, and you really can't build an Angular application without it. This page covers

7 DEADLY DEVELOPER SINS

1.Potential bugs

Page 6: Centric corporate templateadiftene/Scoala/2018/IP/...Angular has its own dependency injection framework, and you really can't build an Angular application without it. This page covers

7 DEADLY DEVELOPER SINS

1.Potential bugs

2.Coding standard breach (or lack of)

Page 7: Centric corporate templateadiftene/Scoala/2018/IP/...Angular has its own dependency injection framework, and you really can't build an Angular application without it. This page covers

7 DEADLY DEVELOPER SINS

1.Potential bugs

2.Coding standard breach (or lack of)

3.Duplications

Page 8: Centric corporate templateadiftene/Scoala/2018/IP/...Angular has its own dependency injection framework, and you really can't build an Angular application without it. This page covers

7 DEADLY DEVELOPER SINS

1.Potential bugs

2.Coding standard breach (or lack of)

3.Duplications

4.Lack of unit tests

Page 9: Centric corporate templateadiftene/Scoala/2018/IP/...Angular has its own dependency injection framework, and you really can't build an Angular application without it. This page covers

7 DEADLY DEVELOPER SINS

1.Potential bugs

2.Coding standard breach (or lack of)

3.Duplications

4.Lack of unit tests

5.Bad distribution of complexity

Page 10: Centric corporate templateadiftene/Scoala/2018/IP/...Angular has its own dependency injection framework, and you really can't build an Angular application without it. This page covers

7 DEADLY DEVELOPER SINS

1.Potential bugs

2.Coding standard breach (or lack of)

3.Duplications

4.Lack of unit tests

5.Bad distribution of complexity

6.Spaghetti design

Page 11: Centric corporate templateadiftene/Scoala/2018/IP/...Angular has its own dependency injection framework, and you really can't build an Angular application without it. This page covers

7 DEADLY DEVELOPER SINS

1.Potential bugs

2.Coding standard breach (or lack of)

3.Duplications

4.Lack of unit tests

5.Bad distribution of complexity

6.Spaghetti design

7.Not enough or too many comments

Page 12: Centric corporate templateadiftene/Scoala/2018/IP/...Angular has its own dependency injection framework, and you really can't build an Angular application without it. This page covers

DEPENDENCY INJECTION PATTERN OVERVIEW

May 29, 2018TITLE PRESENTATION

Page 13: Centric corporate templateadiftene/Scoala/2018/IP/...Angular has its own dependency injection framework, and you really can't build an Angular application without it. This page covers

DEPENDENCY INJECTION PATTERN OVERVIEW

“Dependency injection (DI) is a technique for achieving

loose coupling between objects and their collaborators, or

dependencies. Rather than directly instantiating collaborators, or

using static references, the objects a class needs in order to

perform its actions are provided to the class in some fashion.

Most often, classes will declare their dependencies via their

constructor, allowing them to follow the Explicit Dependencies

Principle. This approach is known as "constructor injection". “

Source - https://docs.microsoft.com/en-

us/aspnet/core/fundamentals/dependency-

injection?view=aspnetcore-2.0

May 29, 2018TITLE PRESENTATION

Page 14: Centric corporate templateadiftene/Scoala/2018/IP/...Angular has its own dependency injection framework, and you really can't build an Angular application without it. This page covers

DEPENDENCY INJECTION PATTERN OVERVIEW

“Every Java-based application has a few objects that

work together to present what the end-user sees as a working

application. When writing a complex Java application,

application classes should be as independent as possible of

other Java classes to increase the possibility to reuse these

classes and to test them independently of other classes while

unit testing. Dependency Injection (or sometime called wiring)

helps in gluing these classes together and at the same time

keeping them independent.”

Source -

https://www.tutorialspoint.com/spring/spring_dependency_injecti

on.htm

May 29, 2018TITLE PRESENTATION

Page 15: Centric corporate templateadiftene/Scoala/2018/IP/...Angular has its own dependency injection framework, and you really can't build an Angular application without it. This page covers

DEPENDENCY INJECTION PATTERN OVERVIEW

“Dependency injection is an important application design

pattern. It's used so widely that almost everyone just calls it DI.

Angular has its own dependency injection framework, and you

really can't build an Angular application without it.

This page covers what DI is and why it's useful”

“Dependency Injection (DI) is a way to create objects

that depend upon other objects. A Dependency Injection system

supplies the dependent objects (called the dependencies) when

it creates an instance of an object.”

Source - https://angular.io/guide/dependency-injection

May 29, 2018TITLE PRESENTATION

Page 16: Centric corporate templateadiftene/Scoala/2018/IP/...Angular has its own dependency injection framework, and you really can't build an Angular application without it. This page covers

DEPENDENCY INJECTION PATTERN OVERVIEW

“In software engineering, dependency injection is a

technique whereby one object (or static method) supplies the

dependencies of another object. A dependency is an object that

can be used (a service)”

Source - https://en.wikipedia.org/wiki/Dependency_injection

May 29, 2018TITLE PRESENTATION

Page 17: Centric corporate templateadiftene/Scoala/2018/IP/...Angular has its own dependency injection framework, and you really can't build an Angular application without it. This page covers

DI - .NET CORE SAMPLE

May 29, 2018TITLE PRESENTATION

Page 18: Centric corporate templateadiftene/Scoala/2018/IP/...Angular has its own dependency injection framework, and you really can't build an Angular application without it. This page covers

DI - JAVA SAMPLE

May 29, 2018TITLE PRESENTATION

Page 19: Centric corporate templateadiftene/Scoala/2018/IP/...Angular has its own dependency injection framework, and you really can't build an Angular application without it. This page covers

DI – ANGULAR SAMPLE

• https://stackblitz.com/angular/poxbnrrxyre?file=src%2Fapp%2

Fuser.service.ts

May 29, 2018TITLE PRESENTATION

Page 20: Centric corporate templateadiftene/Scoala/2018/IP/...Angular has its own dependency injection framework, and you really can't build an Angular application without it. This page covers

SHORT STORY

Page 21: Centric corporate templateadiftene/Scoala/2018/IP/...Angular has its own dependency injection framework, and you really can't build an Angular application without it. This page covers

SHORT STORY

• Cameron Purdy, Programmed in assembly, C, C++, Java,

C#, etc.– Answered Jun 12, 2014 · Upvoted by Jeff Nelson, Invented

Chromebook, #Xoogler

Page 22: Centric corporate templateadiftene/Scoala/2018/IP/...Angular has its own dependency injection framework, and you really can't build an Angular application without it. This page covers

SHORT STORY

• “It's simple: Writing unit tests sucks. I personally (and

absolutely) hate writing unit tests for code that I have

written. I would rather go to the dentist than write unit tests.

Maybe even the proctologist.”

Page 23: Centric corporate templateadiftene/Scoala/2018/IP/...Angular has its own dependency injection framework, and you really can't build an Angular application without it. This page covers

SHORT STORY

• “On the other hand:”– “Even though I only started writing unit tests a few years ago,

now I always write unit tests for software components that I am

building.”

Page 24: Centric corporate templateadiftene/Scoala/2018/IP/...Angular has its own dependency injection framework, and you really can't build an Angular application without it. This page covers

SHORT STORY

• “On the other hand:”– “Even though I only started writing unit tests a few years ago,

now I always write unit tests for software components that I am

building.

– I have never regretted the time spent writing unit tests. Having

unit tests is hugely valuable. Not just for finding bugs in the

software, but for insulating against later breaking changes as

well!”

Page 25: Centric corporate templateadiftene/Scoala/2018/IP/...Angular has its own dependency injection framework, and you really can't build an Angular application without it. This page covers

SHORT STORY

• “On the other hand:”– “Even though I only started writing unit tests a few years ago, now

I always write unit tests for software components that I am building.– I have never regretted the time spent writing unit tests. Having unit tests is

hugely valuable. Not just for finding bugs in the software, but for insulating against later breaking changes as well!

– I love finding a few bugs while writing unit tests. Why "a few"? Well, if I never found any bugs in my code with unit tests, I would consider them a waste of time. If I found too many bugs in my code with unit tests, I would be forced to consider a change of careers.”

Page 26: Centric corporate templateadiftene/Scoala/2018/IP/...Angular has its own dependency injection framework, and you really can't build an Angular application without it. This page covers

SHORT STORY

• “On the other hand:”– “Even though I only started writing unit tests a few years ago, now I always write unit tests

for software components that I am building.– I have never regretted the time spent writing unit tests. Having unit tests is hugely valuable.

Not just for finding bugs in the software, but for insulating against later breaking changes as well!

– I love finding a few bugs while writing unit tests. Why "a few"? Well, if I never found any bugs in my code with unit tests, I would consider them a waste of time. If I found too many bugs in my code with unit tests, I would be forced to consider a change of careers.

– When a developer chooses to not write unit tests, they are telling their team-mates and those who depend on their software that this one developer's time is far more valuable than anyone else's time. Why? Because unit tests are an efficient investment, so not writing them means that other people's time is far, far less valuable than one's own time.”

Page 27: Centric corporate templateadiftene/Scoala/2018/IP/...Angular has its own dependency injection framework, and you really can't build an Angular application without it. This page covers

SHORT STORY

• “On the other hand:”– “Even though I only started writing unit tests a few years ago, now I always write unit tests

for software components that I am building.– I have never regretted the time spent writing unit tests. Having unit tests is hugely valuable.

Not just for finding bugs in the software, but for insulating against later breaking changes as well!

– I love finding a few bugs while writing unit tests. Why "a few"? Well, if I never found any bugs in my code with unit tests, I would consider them a waste of time. If I found too many bugs in my code with unit tests, I would be forced to consider a change of careers.

– When a developer chooses to not write unit tests, they are telling their team-mates and those who depend on their software that this one developer's time is far more valuable than anyone else's time. Why? Because unit tests are an efficient investment, so not writing them means that other people's time is far, far less valuable than one's own time.

• All that said, I still hate writing unit tests.”

Page 28: Centric corporate templateadiftene/Scoala/2018/IP/...Angular has its own dependency injection framework, and you really can't build an Angular application without it. This page covers

SANDRO MANCUSO -> ABOUT UNIT TESTING STORY

Page 29: Centric corporate templateadiftene/Scoala/2018/IP/...Angular has its own dependency injection framework, and you really can't build an Angular application without it. This page covers

SANDRO MANCUSO -> ABOUT UNIT TESTING STORY

“At that point I could not keep quiet anymore. “Now that you know what the problem is, why don’t you write some unit tests

around it?” I really wanted to ask why he didn’t write the tests before his changes in the first

place but I thought he probably wouldn’t take that well. “What I’m doing is important and needs to be done quickly,” he said. “I don’t have time to write tests.” Then he deployed the new version of the application into the testing

environment again (note that no one else could use the testing environment while he was doing his tests), played an XML message into the inbound queue, and started looking at the logs again. That went on for another two days until the problem was actually fixed.”

– Mancuso, Sandro. The Software Craftsman: Professionalism, Pragmatism, Pride (Robert C. Martin Series) (p. 82). Pearson Education. Kindle Edition.

Page 30: Centric corporate templateadiftene/Scoala/2018/IP/...Angular has its own dependency injection framework, and you really can't build an Angular application without it. This page covers

WHY UNIT TESTING?

Page 31: Centric corporate templateadiftene/Scoala/2018/IP/...Angular has its own dependency injection framework, and you really can't build an Angular application without it. This page covers

WHY UNIT TESTING?

Page 32: Centric corporate templateadiftene/Scoala/2018/IP/...Angular has its own dependency injection framework, and you really can't build an Angular application without it. This page covers

WHY UNIT TESTING?

Page 33: Centric corporate templateadiftene/Scoala/2018/IP/...Angular has its own dependency injection framework, and you really can't build an Angular application without it. This page covers

WHY UNIT TESTING?

• Benefits

Page 34: Centric corporate templateadiftene/Scoala/2018/IP/...Angular has its own dependency injection framework, and you really can't build an Angular application without it. This page covers

WHY UNIT TESTING?

• Benefits• Find defects early

Page 35: Centric corporate templateadiftene/Scoala/2018/IP/...Angular has its own dependency injection framework, and you really can't build an Angular application without it. This page covers

WHY UNIT TESTING?

• Benefits• Find defects early

• Prevent regressions

Page 36: Centric corporate templateadiftene/Scoala/2018/IP/...Angular has its own dependency injection framework, and you really can't build an Angular application without it. This page covers

WHY UNIT TESTING?

• Benefits• Find defects early

• Prevent regressions

• Provide living documentation (source code)

Page 37: Centric corporate templateadiftene/Scoala/2018/IP/...Angular has its own dependency injection framework, and you really can't build an Angular application without it. This page covers

WHY UNIT TESTING?

• Benefits• Find defects early

• Prevent regressions

• Provide living documentation (source code)

• Automate testing efforts

Page 38: Centric corporate templateadiftene/Scoala/2018/IP/...Angular has its own dependency injection framework, and you really can't build an Angular application without it. This page covers

WHAT IS A UNIT TEST?

Page 39: Centric corporate templateadiftene/Scoala/2018/IP/...Angular has its own dependency injection framework, and you really can't build an Angular application without it. This page covers

WHAT IS A UNIT TEST?

• Definition 1

Page 40: Centric corporate templateadiftene/Scoala/2018/IP/...Angular has its own dependency injection framework, and you really can't build an Angular application without it. This page covers

WHAT IS A UNIT TEST?

• Definition 1

• “A unit test is a piece of code (usually a method) that invoke

another piece of code and checks the correctness of some

assumptions afterwards.”

– Art of Unit testing

Page 41: Centric corporate templateadiftene/Scoala/2018/IP/...Angular has its own dependency injection framework, and you really can't build an Angular application without it. This page covers

WHAT IS A UNIT TEST?

• Definition 2

Page 42: Centric corporate templateadiftene/Scoala/2018/IP/...Angular has its own dependency injection framework, and you really can't build an Angular application without it. This page covers

WHAT IS A UNIT TEST?

• Definition 2

Page 43: Centric corporate templateadiftene/Scoala/2018/IP/...Angular has its own dependency injection framework, and you really can't build an Angular application without it. This page covers

WHAT IS A UNIT TEST?

• Definition 2

• Essentially, a unit test is a method that instantiates a small

portion of our application and verifies its behavior

independently from other parts.

Page 44: Centric corporate templateadiftene/Scoala/2018/IP/...Angular has its own dependency injection framework, and you really can't build an Angular application without it. This page covers

UNIT TEST EXAMPLE

Page 45: Centric corporate templateadiftene/Scoala/2018/IP/...Angular has its own dependency injection framework, and you really can't build an Angular application without it. This page covers

UNIT TEST EXAMPLE

Page 46: Centric corporate templateadiftene/Scoala/2018/IP/...Angular has its own dependency injection framework, and you really can't build an Angular application without it. This page covers

UNIT TESTING PYRAMID

Page 47: Centric corporate templateadiftene/Scoala/2018/IP/...Angular has its own dependency injection framework, and you really can't build an Angular application without it. This page covers

UNIT TESTING PYRAMID

Page 48: Centric corporate templateadiftene/Scoala/2018/IP/...Angular has its own dependency injection framework, and you really can't build an Angular application without it. This page covers

TIPS FOR WRITING GREAT UNIT TESTS

Page 49: Centric corporate templateadiftene/Scoala/2018/IP/...Angular has its own dependency injection framework, and you really can't build an Angular application without it. This page covers

TIPS FOR WRITING GREAT UNIT TESTS

• Make each test independent to all the others

Page 50: Centric corporate templateadiftene/Scoala/2018/IP/...Angular has its own dependency injection framework, and you really can't build an Angular application without it. This page covers

TIPS FOR WRITING GREAT UNIT TESTS

• Make each test independent to all the others

• Any given behavior should be specified in one and only one

test

Page 51: Centric corporate templateadiftene/Scoala/2018/IP/...Angular has its own dependency injection framework, and you really can't build an Angular application without it. This page covers

TIPS FOR WRITING GREAT UNIT TESTS

• Make each test independent to all the others

• Any given behavior should be specified in one and only one

test

• Mock out all external services and state

Page 52: Centric corporate templateadiftene/Scoala/2018/IP/...Angular has its own dependency injection framework, and you really can't build an Angular application without it. This page covers

TIPS FOR WRITING GREAT UNIT TESTS

• Make each test independent to all the others

• Any given behavior should be specified in one and only one

test

• Mock out all external services and state

• “Any unit tests are better than none”

Page 53: Centric corporate templateadiftene/Scoala/2018/IP/...Angular has its own dependency injection framework, and you really can't build an Angular application without it. This page covers

TESTING GOALS

Page 54: Centric corporate templateadiftene/Scoala/2018/IP/...Angular has its own dependency injection framework, and you really can't build an Angular application without it. This page covers

TESTING GOALS

Goal Strongest technique

Page 55: Centric corporate templateadiftene/Scoala/2018/IP/...Angular has its own dependency injection framework, and you really can't build an Angular application without it. This page covers

TESTING GOALS

Goal Strongest technique

Finding bugs (things that don’t work as you want them to)

Page 56: Centric corporate templateadiftene/Scoala/2018/IP/...Angular has its own dependency injection framework, and you really can't build an Angular application without it. This page covers

TESTING GOALS

Goal Strongest technique

Finding bugs (things that don’t work as you want them to)

Manual testing (sometimes also automated integration tests)

Page 57: Centric corporate templateadiftene/Scoala/2018/IP/...Angular has its own dependency injection framework, and you really can't build an Angular application without it. This page covers

TESTING GOALS

Goal Strongest technique

Finding bugs (things that don’t work as you want them to)

Manual testing (sometimes also automated integration tests)

Detecting regressions (things that used to work but have unexpectedly stopped working)

Page 58: Centric corporate templateadiftene/Scoala/2018/IP/...Angular has its own dependency injection framework, and you really can't build an Angular application without it. This page covers

TESTING GOALS

Goal Strongest technique

Finding bugs (things that don’t work as you want them to)

Manual testing (sometimes also automated integration tests)

Detecting regressions (things that used to work but have unexpectedly stopped working)

Automated integration tests (sometimes also manual testing, though time-consuming)

Page 59: Centric corporate templateadiftene/Scoala/2018/IP/...Angular has its own dependency injection framework, and you really can't build an Angular application without it. This page covers

TESTING GOALS

Goal Strongest technique

Finding bugs (things that don’t work as you want them to)

Manual testing (sometimes also automated integration tests)

Detecting regressions (things that used to work but have unexpectedly stopped working)

Automated integration tests (sometimes also manual testing, though time-consuming)

Designing software components robustly

Page 60: Centric corporate templateadiftene/Scoala/2018/IP/...Angular has its own dependency injection framework, and you really can't build an Angular application without it. This page covers

TESTING GOALS

Goal Strongest technique

Finding bugs (things that don’t work as you want them to)

Manual testing (sometimes also automated integration tests)

Detecting regressions (things that used to work but have unexpectedly stopped working)

Automated integration tests (sometimes also manual testing, though time-consuming)

Designing software components robustly Unit testing (within the TDD process)

Page 61: Centric corporate templateadiftene/Scoala/2018/IP/...Angular has its own dependency injection framework, and you really can't build an Angular application without it. This page covers

SUGGESTIONS/MATERIALS

• https://www.martinfowler.com/articles/injection.html

• https://livebook.manning.com/#!/book/the-art-of-unit-testing-

second-edition/about-this-book/1

• https://www.amazon.com/Software-Craftsman-

Professionalism-Pragmatism-Robert/dp/0134052501

May 29, 2018TITLE PRESENTATION

Page 62: Centric corporate templateadiftene/Scoala/2018/IP/...Angular has its own dependency injection framework, and you really can't build an Angular application without it. This page covers

ONE MORE THING…

Page 63: Centric corporate templateadiftene/Scoala/2018/IP/...Angular has its own dependency injection framework, and you really can't build an Angular application without it. This page covers

ONE MORE THING…

“The difference between a bad programmer and a good

programmer is understanding. That is, bad programmers don’t

understand what they are doing and good programmers do. —

Max Kanat-Alexander”

― Steve Fenton, Pro Typescript: Application-Scale

JavaScript Development

Page 64: Centric corporate templateadiftene/Scoala/2018/IP/...Angular has its own dependency injection framework, and you really can't build an Angular application without it. This page covers

ONE MORE THING…

https://www.reddit.com/r/programming/comments/8cwa4o/depen

dency_injection_is_a_25dollar_term_for_a/

Page 65: Centric corporate templateadiftene/Scoala/2018/IP/...Angular has its own dependency injection framework, and you really can't build an Angular application without it. This page covers

QUESTIONS?

Page 66: Centric corporate templateadiftene/Scoala/2018/IP/...Angular has its own dependency injection framework, and you really can't build an Angular application without it. This page covers

THANK YOU!

May 29, 2018

FLORIN OLARIU

[email protected]