asp.net core dependency injection & unit testing...oct 08, 2016 · refactoringmindset legacy:...
TRANSCRIPT
![Page 1: ASP.NET Core Dependency Injection & Unit Testing...Oct 08, 2016 · RefactoringMindset LEGACY: „Do not touchworking code, unlessneeded.“ Plannedrefactoring NOW: „Leavethecodein](https://reader035.vdocuments.pub/reader035/viewer/2022081612/5f2278ec4f1e8f4e2d6e8011/html5/thumbnails/1.jpg)
Working Effectively withLegacy Code
Robert Hakensoftware architect, HAVIT, [email protected], @RobertHakenMicrosoft MVP: Development, MCT, MCSD
![Page 2: ASP.NET Core Dependency Injection & Unit Testing...Oct 08, 2016 · RefactoringMindset LEGACY: „Do not touchworking code, unlessneeded.“ Plannedrefactoring NOW: „Leavethecodein](https://reader035.vdocuments.pub/reader035/viewer/2022081612/5f2278ec4f1e8f4e2d6e8011/html5/thumbnails/2.jpg)
OCHUTNÁVKA
Legacy Code
![Page 3: ASP.NET Core Dependency Injection & Unit Testing...Oct 08, 2016 · RefactoringMindset LEGACY: „Do not touchworking code, unlessneeded.“ Plannedrefactoring NOW: „Leavethecodein](https://reader035.vdocuments.pub/reader035/viewer/2022081612/5f2278ec4f1e8f4e2d6e8011/html5/thumbnails/3.jpg)
LegacyCode?
„Codewithouttests.“ [Michael Feathers]
„Sourcecode inherited from someone else.“
„Sourcecode inherited from an older version of the software.“
„Veškerý kód, s kterým aktuálně nejste spokojeni (nebo byste alespoň neměli být).“ [Robert Haken]
![Page 4: ASP.NET Core Dependency Injection & Unit Testing...Oct 08, 2016 · RefactoringMindset LEGACY: „Do not touchworking code, unlessneeded.“ Plannedrefactoring NOW: „Leavethecodein](https://reader035.vdocuments.pub/reader035/viewer/2022081612/5f2278ec4f1e8f4e2d6e8011/html5/thumbnails/4.jpg)
LegacyCode
Non-uniformcodingstyle
Nesrozumitelný
Málo/bez testů
BadDesign
CodeSmell
…
![Page 5: ASP.NET Core Dependency Injection & Unit Testing...Oct 08, 2016 · RefactoringMindset LEGACY: „Do not touchworking code, unlessneeded.“ Plannedrefactoring NOW: „Leavethecodein](https://reader035.vdocuments.pub/reader035/viewer/2022081612/5f2278ec4f1e8f4e2d6e8011/html5/thumbnails/5.jpg)
RefactoringMindset
LEGACY:
„Do not touchworkingcode, unlessneeded.“
Plannedrefactoring
NOW:
„Leavethecodein bettercondition thanyoufoundit.“ [TheBoy Scout Rule]
Refactoring as yougo.
![Page 6: ASP.NET Core Dependency Injection & Unit Testing...Oct 08, 2016 · RefactoringMindset LEGACY: „Do not touchworking code, unlessneeded.“ Plannedrefactoring NOW: „Leavethecodein](https://reader035.vdocuments.pub/reader035/viewer/2022081612/5f2278ec4f1e8f4e2d6e8011/html5/thumbnails/6.jpg)
Předpoklady
Sdílené vlastnictví kódu
Source CodeManagement
Continuous Integration builds + runsTests
![Page 7: ASP.NET Core Dependency Injection & Unit Testing...Oct 08, 2016 · RefactoringMindset LEGACY: „Do not touchworking code, unlessneeded.“ Plannedrefactoring NOW: „Leavethecodein](https://reader035.vdocuments.pub/reader035/viewer/2022081612/5f2278ec4f1e8f4e2d6e8011/html5/thumbnails/7.jpg)
RefactoringJustification
Quality
CleanCode
Professionalism
RightThing
Economics
![Page 8: ASP.NET Core Dependency Injection & Unit Testing...Oct 08, 2016 · RefactoringMindset LEGACY: „Do not touchworking code, unlessneeded.“ Plannedrefactoring NOW: „Leavethecodein](https://reader035.vdocuments.pub/reader035/viewer/2022081612/5f2278ec4f1e8f4e2d6e8011/html5/thumbnails/8.jpg)
DEMO
Roslyn CodeAnalysis + baseline
![Page 9: ASP.NET Core Dependency Injection & Unit Testing...Oct 08, 2016 · RefactoringMindset LEGACY: „Do not touchworking code, unlessneeded.“ Plannedrefactoring NOW: „Leavethecodein](https://reader035.vdocuments.pub/reader035/viewer/2022081612/5f2278ec4f1e8f4e2d6e8011/html5/thumbnails/9.jpg)
CodeAnalyzers
Microsoft.AnalyzerPowerPack(RoslynTeam)
System.Runtime.[CSharp.]Analyzers
StyleCopAnalyzers
SonarLint [SonarQube]
VS Perf-tip:
Tools / Options / Text Editor / C# / Advanced / Enable solution wide analysis = OFF
![Page 10: ASP.NET Core Dependency Injection & Unit Testing...Oct 08, 2016 · RefactoringMindset LEGACY: „Do not touchworking code, unlessneeded.“ Plannedrefactoring NOW: „Leavethecodein](https://reader035.vdocuments.pub/reader035/viewer/2022081612/5f2278ec4f1e8f4e2d6e8011/html5/thumbnails/10.jpg)
Refactoring
Continuous, As yougoSmallstepsIDE/ToolingsupportedsafestepsComprehensionRefactoring(Rename, Extract, …)Podpořeno testy
– Pomáhají porozumět kódu– GuardConditions, Contract.Requires– Debug.Assert, InvalidOperationException, …– Unit-Tests– IntegrationTests
![Page 11: ASP.NET Core Dependency Injection & Unit Testing...Oct 08, 2016 · RefactoringMindset LEGACY: „Do not touchworking code, unlessneeded.“ Plannedrefactoring NOW: „Leavethecodein](https://reader035.vdocuments.pub/reader035/viewer/2022081612/5f2278ec4f1e8f4e2d6e8011/html5/thumbnails/11.jpg)
"Good" Unit Test
• automated+ repeatable
• fully isolated
• consistent in itsresults
• runsquickly
• full control of the unit under test(alldependencies)
• relevanttomorrow
• easyto implement
• able to run it at the push of a button
• if fails=> easy to detect what was expected
![Page 12: ASP.NET Core Dependency Injection & Unit Testing...Oct 08, 2016 · RefactoringMindset LEGACY: „Do not touchworking code, unlessneeded.“ Plannedrefactoring NOW: „Leavethecodein](https://reader035.vdocuments.pub/reader035/viewer/2022081612/5f2278ec4f1e8f4e2d6e8011/html5/thumbnails/12.jpg)
DEMO
Poor Man‘s TestabilityExtract dependant call to virtual method
![Page 13: ASP.NET Core Dependency Injection & Unit Testing...Oct 08, 2016 · RefactoringMindset LEGACY: „Do not touchworking code, unlessneeded.“ Plannedrefactoring NOW: „Leavethecodein](https://reader035.vdocuments.pub/reader035/viewer/2022081612/5f2278ec4f1e8f4e2d6e8011/html5/thumbnails/13.jpg)
"Good" Unit Test
• automated+ repeatable
• fully isolated
• consistent in itsresults
• runsquickly
• full control of the unit under test(alldependencies)
• relevanttomorrow
• easyto implement
• able to run it at the push of a button
• if fails=> easy to detect what was expected
![Page 14: ASP.NET Core Dependency Injection & Unit Testing...Oct 08, 2016 · RefactoringMindset LEGACY: „Do not touchworking code, unlessneeded.“ Plannedrefactoring NOW: „Leavethecodein](https://reader035.vdocuments.pub/reader035/viewer/2022081612/5f2278ec4f1e8f4e2d6e8011/html5/thumbnails/14.jpg)
DEMO
Mocking
![Page 15: ASP.NET Core Dependency Injection & Unit Testing...Oct 08, 2016 · RefactoringMindset LEGACY: „Do not touchworking code, unlessneeded.“ Plannedrefactoring NOW: „Leavethecodein](https://reader035.vdocuments.pub/reader035/viewer/2022081612/5f2278ec4f1e8f4e2d6e8011/html5/thumbnails/15.jpg)
"Good" Unit Test
• automated+ repeatable
• fully isolated
• consistent in itsresults
• runsquickly
• full control of the unit under test(alldependencies)
• relevanttomorrow
• easyto implement
• able to run it at the push of a button
• if fails=> easy to detect what was expected
![Page 16: ASP.NET Core Dependency Injection & Unit Testing...Oct 08, 2016 · RefactoringMindset LEGACY: „Do not touchworking code, unlessneeded.“ Plannedrefactoring NOW: „Leavethecodein](https://reader035.vdocuments.pub/reader035/viewer/2022081612/5f2278ec4f1e8f4e2d6e8011/html5/thumbnails/16.jpg)
DEMO
Dependency Injection
![Page 17: ASP.NET Core Dependency Injection & Unit Testing...Oct 08, 2016 · RefactoringMindset LEGACY: „Do not touchworking code, unlessneeded.“ Plannedrefactoring NOW: „Leavethecodein](https://reader035.vdocuments.pub/reader035/viewer/2022081612/5f2278ec4f1e8f4e2d6e8011/html5/thumbnails/17.jpg)
Tips& Tricks
[assembly::InternalsVisibleTo(MyTestAssembly)]
[Obsolete]
TreatWarningsas Errors
Ambient Context(ale ne ServiceLocatornebo public container!)
ServiceFactories
Analyze/ AnalyzeSolutionfor CodeClones
Test / AnalyzeCodeCoverage
![Page 18: ASP.NET Core Dependency Injection & Unit Testing...Oct 08, 2016 · RefactoringMindset LEGACY: „Do not touchworking code, unlessneeded.“ Plannedrefactoring NOW: „Leavethecodein](https://reader035.vdocuments.pub/reader035/viewer/2022081612/5f2278ec4f1e8f4e2d6e8011/html5/thumbnails/18.jpg)
Q & A