dev cast dependency injection

23
Dependency Injection And using an Inversion of Control (IoC) container

Upload: dylan-thomas

Post on 06-May-2015

1.167 views

Category:

Technology


0 download

DESCRIPTION

A very quick overview of dependency injection and IoC. For an internal DevCast meeting

TRANSCRIPT

Page 1: Dev Cast Dependency Injection

Dependency Injection

And using an Inversion of Control (IoC) container

Page 2: Dev Cast Dependency Injection

What is Dependency Injection

• Giving an object instance it’s variables

Page 3: Dev Cast Dependency Injection

Dependency Non-Injection

public class Whatever{ private DbStuff _db; This is a variable

public Whatever() { _db = new DbStuff(); And Whatever } depends on it so it creates it public void DoIt() { _db.GetData(); }}

Page 4: Dev Cast Dependency Injection

And this is bad for all kinds of reasons…

Page 5: Dev Cast Dependency Injection

Single Responsibility Principle

• A class should have one, and only one, reason to change.

• We switch from SQL Server to Oracle and our Whatever class needs to change

Page 6: Dev Cast Dependency Injection

Dependency Injection

public class Whatever{ private DbStuff _db;

public Whatever(DbStuff database) { _db = database; The ‘new’ keyword } is outta there

public void DoIt() { _db.GetData(); }}

Page 7: Dev Cast Dependency Injection

Win

• Our Whatever class still depends on DbData but it is not responsible for it

• By adhering to SRP through dependency injection we increase cohesion

Page 8: Dev Cast Dependency Injection

But wait! We still have a problem

Page 9: Dev Cast Dependency Injection

Dependency Inversion Principle

• Depend on abstractions, not on concretions.

• We can improve the situation by depending on an interface or abstract class

Page 10: Dev Cast Dependency Injection

Dependency Injection v 2.0

public class Whatever{ private IDbStuff _db; Better

public Whatever(IDbStuff database) { _db = database; life is good }

public void DoIt() { _db.GetData(); }}

Page 11: Dev Cast Dependency Injection

To summarize, DI is about passing instance variables

Page 12: Dev Cast Dependency Injection

Benefits

• High cohesion because classes are focused on doing one thing really well

• Testability

IDbStuff data = new FakeDbWithBadData();

var test = new Whatever(data);

Assert.Throws(Exception,test.DoIt());The class under

test did not have to change. Magic!

Page 13: Dev Cast Dependency Injection

private readonly SystemConfiguration _configuration;private readonly IWorkorderEntityBuilder _entityBuilder;

public NewEntityWorkorderManager(IWorkorderRepository workorderRepository, IMessageRepository messageRepository, IWorkorderEntityBuilder entityBuilder, SystemConfiguration configuration, ICityworksRepository cityworksRepository) : base(workorderRepository, messageRepository, cityworksRepository, configuration) { _entityBuilder = entityBuilder; _configuration = configuration; }

Page 14: Dev Cast Dependency Injection

Inversion of Control Containers

(IoC is a terrible term but we’re stuck with it. But maybe we’ll use container

instead. At least it’s shorter.)

Page 15: Dev Cast Dependency Injection

Manage Dependencies

• DI makes for cohesive, loosely couple software

• But also more moving parts

• IoC containers exist to make dependency injection easier and more predictable

Page 16: Dev Cast Dependency Injection

ContainersA Short List

• Spring• Spring.NET• Pico• Castle Windsor• StructureMap

• Ninject• Autofac• Unity• Glassfish and any EJB

3.0 app server

Page 17: Dev Cast Dependency Injection

Workflow

Create a registry at application startup

Wire up dependencies in one place• Abstractions• Cascading dependencies• Auto-wiring• Lifestyle/instance

management

Ask the container for an instance

Page 18: Dev Cast Dependency Injection

ForRequestedType<ISpatialQuery>() .TheDefaultIsConcreteType<SpatialQueryGenericClient>();

Page 19: Dev Cast Dependency Injection

ForRequestedType<ISettingRepository>()

.TheDefault.Is.OfConcreteType<ApplicationSettingReposi

tory>()

.CtorDependency<IUnitOfWork>("unitOfWork")

.Is(u => u.TheInstanceNamed(Resources.WCSDatasource));

Page 20: Dev Cast Dependency Injection

var settings = ObjectFactory.GetInstance<ISettingRepository>();

Page 21: Dev Cast Dependency Injection

Autowiring Is A Big Win

Page 22: Dev Cast Dependency Injection

Things depend on things depend on things…

CoreMessageProcess

GisDirectQuery

var processor = ObjectFactory.GetInstance<IMessageProcessor>();

Page 23: Dev Cast Dependency Injection

Features

• Many other features but that’s the core

• Manage lifetime of objects– Singleton, per-thread, per-session

• XML configuration