Lego For Engineers - Dependency Injection for LIDNUG (2011-06-03)
-
Upload
theo-jungeblut -
Category
Technology
-
view
2.158 -
download
0
description
Transcript of Lego For Engineers - Dependency Injection for LIDNUG (2011-06-03)
![Page 1: Lego For Engineers - Dependency Injection for LIDNUG (2011-06-03)](https://reader033.fdocuments.net/reader033/viewer/2022060107/554a0754b4c905507a8b5579/html5/thumbnails/1.jpg)
Lego for Engineers
How to build Reusable and Maintainable
Applications in C#
Theo Jungeblut 06/03/2011 Lego (trademarked in capitals as LEGO)
![Page 2: Lego For Engineers - Dependency Injection for LIDNUG (2011-06-03)](https://reader033.fdocuments.net/reader033/viewer/2022060107/554a0754b4c905507a8b5579/html5/thumbnails/2.jpg)
Theo Jungeblut • Senior Software Developer at
Omnicell Inc. in Mountain View
• Has been designing and implementing .NET based applications , components and frameworks for 8 years
• Previously worked in factory automation with focus on component based software and framework development for 3 ½ years
• Degree in Software Engineering and Network Communications
www.csharp-lightouse.com
![Page 3: Lego For Engineers - Dependency Injection for LIDNUG (2011-06-03)](https://reader033.fdocuments.net/reader033/viewer/2022060107/554a0754b4c905507a8b5579/html5/thumbnails/3.jpg)
Overview
• Why Lego for Software Engineers?
• Design Patterns, Principles and Best Practices
• Dependency Injection Container & more
• The “Must Read”-Book
• Summary
• Q & A
• References
3
![Page 4: Lego For Engineers - Dependency Injection for LIDNUG (2011-06-03)](https://reader033.fdocuments.net/reader033/viewer/2022060107/554a0754b4c905507a8b5579/html5/thumbnails/4.jpg)
Why Lego for Software Engineers?
4
Lego (trademarked in capitals as LEGO) is a line of construction toys manufactured by the Lego Group
http://upload.wikimedia.org/wikipedia/commons/7/75/Lego_technic_gears.jpg
![Page 5: Lego For Engineers - Dependency Injection for LIDNUG (2011-06-03)](https://reader033.fdocuments.net/reader033/viewer/2022060107/554a0754b4c905507a8b5579/html5/thumbnails/5.jpg)
Design Patterns, Principals & Best Practices
5
• Keep it simple stupid (KISS)
• Separation of Concerns (SoC)
• Single Responsibility Principle (SRP)
• Component Oriented Programming (CoP)
• Interface / Contract
• Don’t Repeat Yourself (DRY)
• Dependency Inversion Principal (DIP)
• Inversion of Control (IoC)
• Constructor Injection
• Setter Injection
• Interface Injection • Service Locator
![Page 6: Lego For Engineers - Dependency Injection for LIDNUG (2011-06-03)](https://reader033.fdocuments.net/reader033/viewer/2022060107/554a0754b4c905507a8b5579/html5/thumbnails/6.jpg)
Keep it simple, stupid (KISS)
![Page 7: Lego For Engineers - Dependency Injection for LIDNUG (2011-06-03)](https://reader033.fdocuments.net/reader033/viewer/2022060107/554a0754b4c905507a8b5579/html5/thumbnails/7.jpg)
KISS-Principle – “Keep It Simple Stupid”
7 http://blogs.smarter.com/blogs/Lego%20Brick.jpg
by Kelly Johnson
![Page 8: Lego For Engineers - Dependency Injection for LIDNUG (2011-06-03)](https://reader033.fdocuments.net/reader033/viewer/2022060107/554a0754b4c905507a8b5579/html5/thumbnails/8.jpg)
The Power of Simplicity
8 http://www.geekalerts.com/lego-iphone/
Graphic by Nathan Sawaya courtesy of brickartist.com
Graphic by Nathan Sawaya courtesy of brickartist.com
![Page 9: Lego For Engineers - Dependency Injection for LIDNUG (2011-06-03)](https://reader033.fdocuments.net/reader033/viewer/2022060107/554a0754b4c905507a8b5579/html5/thumbnails/9.jpg)
Graphic by Nathan Sawaya courtesy of brickartist.com
![Page 10: Lego For Engineers - Dependency Injection for LIDNUG (2011-06-03)](https://reader033.fdocuments.net/reader033/viewer/2022060107/554a0754b4c905507a8b5579/html5/thumbnails/10.jpg)
Separation of Concerns (SoC)
Single Responsibility
Principle (SRP)
![Page 11: Lego For Engineers - Dependency Injection for LIDNUG (2011-06-03)](https://reader033.fdocuments.net/reader033/viewer/2022060107/554a0754b4c905507a8b5579/html5/thumbnails/11.jpg)
http://www.technicopedia.com/8865.html
The Product
![Page 12: Lego For Engineers - Dependency Injection for LIDNUG (2011-06-03)](https://reader033.fdocuments.net/reader033/viewer/2022060107/554a0754b4c905507a8b5579/html5/thumbnails/12.jpg)
http://www.technicopedia.com/8865.html
Component / Service
![Page 13: Lego For Engineers - Dependency Injection for LIDNUG (2011-06-03)](https://reader033.fdocuments.net/reader033/viewer/2022060107/554a0754b4c905507a8b5579/html5/thumbnails/13.jpg)
http://technicbricks.blogspot.com/2009/06/tbs-techpoll-12-results-2009-1st.html
Class, Struct, Enum etc.
![Page 14: Lego For Engineers - Dependency Injection for LIDNUG (2011-06-03)](https://reader033.fdocuments.net/reader033/viewer/2022060107/554a0754b4c905507a8b5579/html5/thumbnails/14.jpg)
Separation of Concerns (SoC)
14
• “In computer science, separation of concerns (SoC) is the process of separating a computer program into distinct features that overlap in functionality as little as possible.
•A concern is any piece of interest or focus in a program. Typically, concerns are synonymous with features or behaviors. “
probably by Edsger W. Dijkstra in 1974
http://en.wikipedia.org/wiki/Separation_of_Concerns
![Page 15: Lego For Engineers - Dependency Injection for LIDNUG (2011-06-03)](https://reader033.fdocuments.net/reader033/viewer/2022060107/554a0754b4c905507a8b5579/html5/thumbnails/15.jpg)
Single Responsibility Principle (SRP)
15
“Every object should have a single responsibility, and that responsibility should be entirely encapsulated by the class.”
by Robert C Martin
http://www.ericalbrecht.com
http://en.wikipedia.org/wiki/Single_responsibility_principle
public class Logger : ILogger { public Logger(ILoggingSink loggingSink) {} public void Log(string message) {} }
![Page 16: Lego For Engineers - Dependency Injection for LIDNUG (2011-06-03)](https://reader033.fdocuments.net/reader033/viewer/2022060107/554a0754b4c905507a8b5579/html5/thumbnails/16.jpg)
Component-Oriented Programming
(CoP)
![Page 17: Lego For Engineers - Dependency Injection for LIDNUG (2011-06-03)](https://reader033.fdocuments.net/reader033/viewer/2022060107/554a0754b4c905507a8b5579/html5/thumbnails/17.jpg)
Different Ways of doing Something Similar
17
http://www.ericalbrecht.com
http://www.ericalbrecht.com
http://www.julianaheng.com/transformers-rotf-bumblebee- and-sam-action-figures/
![Page 18: Lego For Engineers - Dependency Injection for LIDNUG (2011-06-03)](https://reader033.fdocuments.net/reader033/viewer/2022060107/554a0754b4c905507a8b5579/html5/thumbnails/18.jpg)
Why Reusable Components Rock
18
http://www.ericalbrecht.com/technic/8020/8020all.jpg
![Page 19: Lego For Engineers - Dependency Injection for LIDNUG (2011-06-03)](https://reader033.fdocuments.net/reader033/viewer/2022060107/554a0754b4c905507a8b5579/html5/thumbnails/19.jpg)
Interfaces / Contracts
19
public interface ILogger { void Log(string message); }
• Decouple Usage and Implementation through introduction of a contract • Allows to replace implementation without changing the consumer
public class Logger : ILogger { public Logger(ILoggingSink loggingSink) {} public void Log(string message) {} }
![Page 20: Lego For Engineers - Dependency Injection for LIDNUG (2011-06-03)](https://reader033.fdocuments.net/reader033/viewer/2022060107/554a0754b4c905507a8b5579/html5/thumbnails/20.jpg)
Don’t repeat yourself (DRY)
![Page 21: Lego For Engineers - Dependency Injection for LIDNUG (2011-06-03)](https://reader033.fdocuments.net/reader033/viewer/2022060107/554a0754b4c905507a8b5579/html5/thumbnails/21.jpg)
Don’t repeat yourself (DRY)
21
by Andy Hunt and Dave Thomas in their book “The Pragmatic Programmer”
// Code Copy and Paste Method public Class Person { public string FirstName { get; set;} public string LastName { get; set;} public Person(Person person) { this.FirstName = string.IsNullOrEmpty(person.FirstName) ? string.Empty : (string) person.FirstName.Clone(); this.LastName = string.IsNullOrEmpty(person.LastName) ? string.Empty : (string) person.LastName.Clone(); } public object Clone() { return new Person(this); } }
// DRY Method public Class Person { public string FirstName { get; set;} public string LastName { get; set;} public Person(Person person) { this.FirstName = person.FirstName.CloneSecured(); this.LastName = person.LastName.CloneSecured(); } public object Clone() { return new Person(this); } }
public static class StringExtension { public static string CloneSecured(this string original) { return string.IsNullOrEmpty(original) ? string.Empty : (string)original.Clone(); } }
![Page 22: Lego For Engineers - Dependency Injection for LIDNUG (2011-06-03)](https://reader033.fdocuments.net/reader033/viewer/2022060107/554a0754b4c905507a8b5579/html5/thumbnails/22.jpg)
Dependency Inversion Principle (DIP)
![Page 23: Lego For Engineers - Dependency Injection for LIDNUG (2011-06-03)](https://reader033.fdocuments.net/reader033/viewer/2022060107/554a0754b4c905507a8b5579/html5/thumbnails/23.jpg)
Dependency Inversion Principle (DIP)
• “High-level modules should not depend on low-level modules. Both should depend on abstractions.
• Abstractions should not depend upon details. Details should depend upon abstractions.”
23
http://en.wikipedia.org/wiki/Dependency_inversion_principle
by Robert C. Martin
![Page 24: Lego For Engineers - Dependency Injection for LIDNUG (2011-06-03)](https://reader033.fdocuments.net/reader033/viewer/2022060107/554a0754b4c905507a8b5579/html5/thumbnails/24.jpg)
Single Responsibility Principle
Open/Closed Principle
Liskov Substitution Principle
Interface Segregation Principle
Dependency Inversion Principle
S O L I D
Robert C Martin: http://butunclebob.com/ArticleS.UncleBob.PrinciplesOfOod
![Page 25: Lego For Engineers - Dependency Injection for LIDNUG (2011-06-03)](https://reader033.fdocuments.net/reader033/viewer/2022060107/554a0754b4c905507a8b5579/html5/thumbnails/25.jpg)
Inversion of Control (IoC)
![Page 26: Lego For Engineers - Dependency Injection for LIDNUG (2011-06-03)](https://reader033.fdocuments.net/reader033/viewer/2022060107/554a0754b4c905507a8b5579/html5/thumbnails/26.jpg)
Inversion of Control (IoC)
26
by Martin Fowler 1994
Logger logger = new Logger();
![Page 27: Lego For Engineers - Dependency Injection for LIDNUG (2011-06-03)](https://reader033.fdocuments.net/reader033/viewer/2022060107/554a0754b4c905507a8b5579/html5/thumbnails/27.jpg)
Inversion of Control (IoC)
27
by Martin Fowler 1994
Logger logger = new Logger();
![Page 28: Lego For Engineers - Dependency Injection for LIDNUG (2011-06-03)](https://reader033.fdocuments.net/reader033/viewer/2022060107/554a0754b4c905507a8b5579/html5/thumbnails/28.jpg)
The “Must Read”-Book
28 http://www.manning.com/seemann/
by Mark Seemann
Dependency Injection is a set of software design principles and patterns that enable us to develop loosely coupled code.
![Page 29: Lego For Engineers - Dependency Injection for LIDNUG (2011-06-03)](https://reader033.fdocuments.net/reader033/viewer/2022060107/554a0754b4c905507a8b5579/html5/thumbnails/29.jpg)
Inversion of Control –
Constructor Injection
29
public class ContactManager : IContactManager { public ContactManager(ILogger logger, IContactPersistence contactPersistence) { this.logger = logger; if (logger == null) { throw new ArgumentNullException("logger"); } … } }
http://www.martinfowler.com/articles/injection.html
![Page 30: Lego For Engineers - Dependency Injection for LIDNUG (2011-06-03)](https://reader033.fdocuments.net/reader033/viewer/2022060107/554a0754b4c905507a8b5579/html5/thumbnails/30.jpg)
Inversion of Control –
Setter (Property) Injection
30
// UNITY Example public class ContactManager : IContactManager { [Dependency] public IContactPersistence ContactPersistence { get { return this.contactPersistence; } set { this.contactPersistence = value; } } }
http://www.martinfowler.com/articles/injection.html
![Page 31: Lego For Engineers - Dependency Injection for LIDNUG (2011-06-03)](https://reader033.fdocuments.net/reader033/viewer/2022060107/554a0754b4c905507a8b5579/html5/thumbnails/31.jpg)
Inversion of Control (IoC) –
Interface Injection
31
http://www.martinfowler.com/articles/injection.html
http://www.codeproject.com/KB/aspnet/IOCDI/InterfacebasedDI.JPG
In this methodology we implement an interface from the IOC framework. IOC framework will use the interface method to inject the object in the main class. You can see in figure ‘Interface based DI’ we have implemented an interface ‘IAddressDI’ which has a ‘setAddress’ method which sets the address object. This interface is then implemented in the customer class. External client / containers can then use the ‘setAddress’ method to inject the address object in the customer object.
http://www.codeproject.com/KB/aspnet/IOCDI.aspx
![Page 32: Lego For Engineers - Dependency Injection for LIDNUG (2011-06-03)](https://reader033.fdocuments.net/reader033/viewer/2022060107/554a0754b4c905507a8b5579/html5/thumbnails/32.jpg)
// UNITY Example internal static class Program { private static UnityContainer unityContainer; private static SingleContactManagerForm singleContactManagerForm; private static void InitializeMainForm() { singleContactManagerForm = unityContainer.Resolve<SingleContactManagerForm>(); } }
Inversion of Control –
Service Locator
32
http://www.martinfowler.com/articles/injection.html
![Page 33: Lego For Engineers - Dependency Injection for LIDNUG (2011-06-03)](https://reader033.fdocuments.net/reader033/viewer/2022060107/554a0754b4c905507a8b5579/html5/thumbnails/33.jpg)
// UNITY Example internal static class Program { private static UnityContainer unityContainer; private static SingleContactManagerForm singleContactManagerForm; private static void InitializeMainForm() { singleContactManagerForm = unityContainer.Resolve<SingleContactManagerForm>(); } }
Inversion of Control –
Service Locator
33
http://www.martinfowler.com/articles/injection.html
![Page 34: Lego For Engineers - Dependency Injection for LIDNUG (2011-06-03)](https://reader033.fdocuments.net/reader033/viewer/2022060107/554a0754b4c905507a8b5579/html5/thumbnails/34.jpg)
Inversion of Control Service Locator vs Dependency Injection
34 http://www.martinfowler.com/articles/injection.html#ServiceLocatorVsDependencyInjection
• Service Locator allows to request explicitly the needed instance/type/service • Every user of a service has a dependency to the Service Locator
• Potential issue if the component need to be provided to 3rd parties. • Favorable for closed platforms as the Service Locator allow more control
• Testing is easier with Dependency Injection than a Service Locator if Service provided is not easily substituted • In general Service Locator is only the less compelling choice if the code is mainly used out of the control of the writer
![Page 35: Lego For Engineers - Dependency Injection for LIDNUG (2011-06-03)](https://reader033.fdocuments.net/reader033/viewer/2022060107/554a0754b4c905507a8b5579/html5/thumbnails/35.jpg)
Dependency Injection Container & more
35
• Typically support all types of Inversion of Control mechanisms • Constructor Injection • Property (Setter) Injection • Interface Injection • Service Locator
•.NET based DI-Container • Unity • Castle Windsor • StructureMap • Spring.NET • Autofac • Puzzle.Nfactory • Ninject • PicoContainer.NET • and more
Related Technology: • Managed Extensibility Framework (MEF) • Windows Communication Foundation (WCF)
![Page 36: Lego For Engineers - Dependency Injection for LIDNUG (2011-06-03)](https://reader033.fdocuments.net/reader033/viewer/2022060107/554a0754b4c905507a8b5579/html5/thumbnails/36.jpg)
The “Must Read”-Book
36 http://www.manning.com/seemann/
by Mark Seemann
![Page 37: Lego For Engineers - Dependency Injection for LIDNUG (2011-06-03)](https://reader033.fdocuments.net/reader033/viewer/2022060107/554a0754b4c905507a8b5579/html5/thumbnails/37.jpg)
Improving Reusability and Maintainability through: • Simplification and Specialization (KISS, SoC, SRP)
•Decoupling (Interfaces, CoP, DIP or SOA) • Avoiding Code Blow (DRY, YAGNI)
• Testability (all of them!)
Summary
37 Lego (trademarked in capitals as LEGO)
![Page 38: Lego For Engineers - Dependency Injection for LIDNUG (2011-06-03)](https://reader033.fdocuments.net/reader033/viewer/2022060107/554a0754b4c905507a8b5579/html5/thumbnails/38.jpg)
Q & A
Graphic by Nathan Sawaya courtesy of brickartist.com
www.csharp-lighthouse.com
Feedback & Comments:
![Page 39: Lego For Engineers - Dependency Injection for LIDNUG (2011-06-03)](https://reader033.fdocuments.net/reader033/viewer/2022060107/554a0754b4c905507a8b5579/html5/thumbnails/39.jpg)
References Part
39
http://www.manning.com/seemann/ http://en.wikipedia.org/wiki/Keep_it_simple_stupid http://picocontainer.org/patterns.html http://en.wikipedia.org/wiki/Separation_of_concerns http://en.wikipedia.org/wiki/Don't_repeat_yourself http://en.wikipedia.org/wiki/You_ain't_gonna_need_it http://en.wikipedia.org/wiki/Component-oriented_programming http://en.wikipedia.org/wiki/Service-oriented_architecture http://www.martinfowler.com/articles/injection.html http://www.codeproject.com/KB/aspnet/IOCDI.aspx http://msdn.microsoft.com/en-us/magazine/cc163739.aspx http://msdn.microsoft.com/en-us/library/ff650320.aspx http://msdn.microsoft.com/en-us/library/aa973811.aspx http://msdn.microsoft.com/en-us/library/ff647976.aspx http://msdn.microsoft.com/en-us/library/cc707845.aspx http://msdn.microsoft.com/en-us/library/bb833022.aspx http://dotnetslackers.com/articles/net/A-First-Look-at-Unity-2-0.aspx http://unity.codeplex.com/ http://www.idesign.net/idesign/DesktopDefault.aspx?tabindex=5&tabid=11