Here’s a little bit about us.
Around half of our national technology team of over 120 are based in Melbourne
High-level policy should not depend on low-level detail, low-level detail should depend on high-level policy
Depend upon abstractions, not concretions.
+ f()
Compiling Code
x : int
+ f()
Running Code
x : int
https://xkcd.com/303/
+ f()
+ f()
+ f()
x : int
x : int
+ f()
x : int
+ show()
public class LivingRoom
{
public LivingRoom()
{
}
public void EnterRoom()
{
//Painting is always there.
}
}
public class LivingRoom
{
public LivingRoom()
{
}
public void EnterRoom()
{
//Video is always there.
}
}
public class LivingRoom
{
public bool ShowVideo {get; set;}
public LivingRoom()
{
}
public void EnterRoom()
{
if(ShowVideo)
{
//Show Video.
}
{
//Show Painting.
}
}
}
public class LivingRoom
{
public bool ShowVideo {get; set;}
public bool ShowPoster {get; set;}
public bool ShowWeddingPicture {get; set;}
public LivingRoom()
{
}
public void EnterRoom()
{
if(ShowVideo)
{
//Show Video.
}
else if(ShowPoster)
{
//Show Poster
}
else if(ShowWeddingPicture)
{
//Show Wedding Picture
}
el
else
{
//Show Painting.
}
}
}
+ show()
public class LivingRoom
{
private IScreen screen;
public LivingRoom(IScreen screen)
{
this.screen = screen;
}
public void EnterRoom()
{
this.screen.Show();
}
}
public class LivingRoom
{
private ILcd lcd;
public LivingRoom(ILcd lcd)
{
this.lcd = lcd;
}
public void EnterRoom()
{
lcd.Show();
}
}
Is Dependency Injection different to Dependency Inversion?
Dependency Injection is used when applying the Dependency Inversion Principle.
+ show()
+ show()
public interface IScreen
{
void Show();
}
public class Painting : IScreen
{
public void Show()
{
//Display Painting
}
}
public interface IScreen
{
void Show();
}
public class Video : IScreen
{
public void Show()
{
//Display Video
}
}
public class House
{
public LivingRoom livingRoom;
public void LivingRoom()
{
var video = new Video();
livingRoom = new LivingRoom(video);
livingRoom.EnterRoom();
}
}
public class House
{
public LivingRoom livingRoom;
public void LivingRoom()
{
var painting = new Painting();
livingRoom = new LivingRoom(painting);
livingRoom.EnterRoom();
}
}
Loosely coupled code
Increased Testability
Independent Developability
Allows for Pluggable architecture
Independent Deployability
You may never see the benefits
Can Increase Complexity
Where should we compose object graphs?
As close as possible to the application's entry point.
A Composition Root is a (preferably) unique location in an application where modules are composed together.
public class House
{
public LivingRoom livingRoom;
public void LivingRoom()
{
var painting = new Painting();
livingRoom = new LivingRoom(painting);
livingRoom.EnterRoom();
}
}
public interface IScreen
{
void Show();
}
public class Painting: IScreen
{
public void Show()
{
//Display Painting
}
}
public class Program
{
public static void Main(string[] args)
{
var painting = new Painting();
var livingRoom = new LivingRoom(painting);
var house = new House(livingRoom);
house.LivingRoom();
}
}
public class House
{
public LivingRoom livingRoom;
public House(LivingRoom livingRoom)
{
this.livingRoom = livingRoom;
}
public void LivingRoom()
{
livingRoom.EnterRoom();
}
}
public class Program
{
public static void Main(string[] args)
{
var painting = new Painting();
var livingRoom = new LivingRoom(painting);
var house = new House(livingRoom);
house.LivingRoom();
}
}
public class House
{
public LivingRoom livingRoom;
public House(LivingRoom livingRoom)
{
this.livingRoom = livingRoom;
}
public void LivingRoom()
{
livingRoom.EnterRoom();
}
}
Presentations on other four Principle
Come have a chat with me.
I love this shit.
Keybase
https://keybase.io/gravypower
Have a read of my blog.
https://blog.gravypower.net
Much ranting, you have been warned
The Clean Coder book
Link to my Blog