Depth Consulting - Calgary .NET User Group - Apr 22 2015 - Dependency Injection

35
http://www.depthconsulting.ca

Transcript of Depth Consulting - Calgary .NET User Group - Apr 22 2015 - Dependency Injection

http://www.depthconsulting.ca

http://www.depthconsulting.ca

DeveloperFundamentals:The Series

Calgary.NET User Group

Apr. 22, 2015

Dependency InjectionWhat You Need to Know

Dave WhiteMicrosoft MVP – Visual Studio ALM

http://www.depthconsulting.ca

Developer Fundamentals Series

Testing – Nov. 26, 2014

SOLID – Feb. 26, 2015

Dependency Injection – April 22, 2015

Source Control – Branching/Merging – June

Putting It All Together – July

http://www.depthconsulting.ca

Dave White Principal Consultant – Depth Consulting

http://linkd.in/giMxuw

@AgileRamblings

• In IT for over 15 years

• Microsoft MVP – Visual Studio ALM (2 years)

• Kanban/Agile Evangelist, Speaker

• PRDC 2011, 2012, LKUK, Local events

• Program Director – KCP Program - LKU

• Professional Scrum Master

• Microsoft Platform Developer (C#, Web, WPF)

http://www.agileramblings.com

http://www.depthconsulting.ca

Simon TimmsSenior .NET Dev – Pacesetter Directional Drilling

http://linkd.in/15pYAWL

@stimms

• In IT for over a decade (+/− few months)

• Microsoft MVP – ASP.NET/IIS (2 years)

• Prolific Speaker and Community Involvement

• PRDC (many), AzureCamp, Local events

• Calgary .NET User Group – President

• Author

• Loves Development

• Polyglot

• Web apps, .NET back ends

• Visualization and Cloud

http://blog.simontimms.com/about/

http://www.depthconsulting.ca

WHY?

http://www.depthconsulting.ca

Why We Don’t Inject• I don’t test design

• No one around me is doing it

• Too hard to learn

• No tools

• My stuff doesn’t need it

• Our code is horrible and very deeply coupled

• No time

http://www.depthconsulting.ca

The truth of the matter…• I makes testingdesigning easier!

• It encourages SOLIDprinciples

• Everyone is doing it

• Easier than ever

• Investment in tools• Private and OSS

• You can start small

• You will be a better developer

• Drives growth down maturity path

• Increasing Complexity• Business and technical

• It makes you faster

http://www.depthconsulting.ca

Let’s start

learning!

By John Castillo (Jack)

(https://creativecommons.org/licenses/by/2.0/deed.en),

via Wikimedia Commons

http://www.depthconsulting.ca

What is Dependency Injection (DI)Short:

Giving an object it’s instance variables

Medium:

Dependency injection is providing the objects that an object needs (its dependencies) instead of constructing them itself

Long:

Dependency injection is a software design pattern in which one or more dependencies (or services) are injected, or passed by reference, into a dependent object (or client) and are made part of the client's state.

http://en.wikipedia.org/wiki/Dependency_injection

James Shore - http://bit.ly/1cUUGaA

http://www.depthconsulting.ca

Two Ways to InjectConstructor

• explicitly required

• “I cannot function without this”

Property (Setter)

• optionally required

• “I can function without this”

public class MyClass{

public void MyClass(IDependentOndependency)

{}

}

public class MyClass{

public void MyClass(){}

public IDependentOn dependency {get; set;}}

http://www.depthconsulting.ca

Two Types of Things to InjectConcrete Classes

• less flexible

• can only inject single implementation

Interfaces

• more flexible

• can inject multiple implementations

public class MyClass{

public void MyClass(DependentOndependency)

{}

}

public class DependentOn {}

public class MyClass{

public void MyClass(IDependentOndependency)

{}

}

public interface IDependentOn {}public class Concrete1: IDependentOn {}public class Concrete2: IDependentOn {}

http://www.depthconsulting.ca

Demo

http://www.depthconsulting.ca

By Deutsche Fotothek [CC BY-SA 3.0 de

(http://creativecommons.org/licenses/by-sa/3.0/de/deed.en)],

via Wikimedia Commons

That doesn’t look so hard but why again?

http://www.depthconsulting.ca

Encourages Modularity• Makes testing designing easier

• S in SOLID is good

• Decomposing functionality into smaller parts is good

• Freeing yourself from some concerns which is good

• Allows implementation flexibility (good)

• Centralized implementation (good)

• Reduces Ctrl-C / Ctrl-V code reuse (good)

http://www.depthconsulting.ca

Remember• You are reading this because you want to

improve

• You’ve learned about testing design

• You’ve learned about SOLID

• These things should be viewed as a holistic improvement

They all support each other

http://www.depthconsulting.ca

By Venpia (Own work) [CC-BY-SA-3.0

(http://creativecommons.org/licenses/by-sa/3.0)],

via Wikimedia Commons

So… there are tools to make this easier?

http://www.depthconsulting.ca

Dependency Injection Containers

http://lmgtfy.com/?q=dependency+injection

Castle Windsor

- Unity

StructureMap

http://www.depthconsulting.ca

What do Containers Provide• Primary Functions

• Registration

• Resolution

• Manage Lifetime

• Secondary Functions• Interception/AOP

• Assembly Loading/Type Discovery

http://www.depthconsulting.ca

Registration• Enables container to know what to create

• Describes construction intent• What concrete classes are associated with requests

• Singleton, always create new instances, instance per scope (request)

• Interception

http://www.depthconsulting.ca

Resolution• Construct the actual instances as required

• Add interceptors (more later)

http://www.depthconsulting.ca

Manage Lifetime• If construction is a primary responsibility, so is destruction

http://www.depthconsulting.ca

Interception• You can do AOP with containers

• Usually require an extension

http://www.depthconsulting.ca

Assembly Loading/Type Discovery• Some containers have helper functions that discover implementation classes that fit a specific criteria• Attributes, Interfaces implemented

http://www.depthconsulting.ca

Demo

http://www.depthconsulting.ca

Hold on a second here Mister!

By Achim Hering (Own work)

[GFDL (http://www.gnu.org/copyleft/fdl.html) or

CC-BY-3.0 (http://creativecommons.org/licenses/by/3.0)],

via Wikimedia Commons

http://www.depthconsulting.ca

The Real Truth - It Can’t Be All Roses• Dependency Inject is often a breaking change• Not a refactoring:

• New application architecture pattern in your application

• Burdened by a MOUNTAIN of technical debt and bad design

• It requires a better understanding of • SOLID and the chosen container’s API

• You’re whole team has to understand it• You may have to seek permission from your teammates

• Not managers

• Dependency graphs aren’t any more explicit

http://www.depthconsulting.ca

The Real Truth – There ARE Roses• Making Unit Tests will be easier

• You will create higher quality product

• You will create better designs

• Most (all?) of the container implementations are OSS (read: free)

http://www.depthconsulting.ca

We can do this!

By Weird Beard – Happy [CC-BY-2.0

(http://creativecommons.org/licenses/by/2.0)],

via Wikimedia Commons

http://www.depthconsulting.ca

Next Steps• Build an example

application

• Understand what you want from a DI Container

• Try multiple containers that fulfill your requirements

• Share your learning with your team

KEEPCALM

and

DI/SOLID/TEST

ON

http://www.depthconsulting.ca

Tools• Visual Studio Community

http://www.visualstudio.com/products/visual-studio-community-vs

• Visual Studio 2015 Previewhttp://www.visualstudio.com/en-us/news/vs2015-preview-vs

• Nuget

• Autofac, Moq• http://www.autofac.org

• https://github.com/Moq/moq4

PM> Install-Package Autofac

PM> Install-Package Moq

http://www.depthconsulting.ca

These are the MeetUps you’re looking for…

Testing – Nov. 26, 2014

SOLID – Feb. 26, 2015

Dependency Injection – April 22, 2015

Source Control – Branching/Merging – June

Putting It All Together – July

http://www.depthconsulting.ca

City of Calgary - Hackathon

http://www.depthconsulting.ca

NOW PRIZES!!

1 ReSharper licence

https://www.jetbrains.com/resharper/

1 Infragistics Ultimate License

http://www.infragistics.com/

INFRAGISTICS ULTIMATE

http://www.depthconsulting.ca

ThanksDave White

@agileramblings

http://www.agileramblings.com

[email protected]

Simon Timms

@stimms

http://blog.simontimms.com

[email protected]