14147503 intentions interfaces making patterns concrete

41
Intentions & Interfaces Making patterns concrete Udi Dahan The Software Simplist .NET Development Expert & SOA Specialist www.UdiDahan.com [email protected]

Upload: qconlondon2008

Post on 21-May-2015

528 views

Category:

Technology


2 download

TRANSCRIPT

Page 1: 14147503 Intentions Interfaces Making Patterns Concrete

Intentions & InterfacesMaking patterns concrete

Udi Dahan – The Software Simplist

.NET Development Expert & SOA Specialist

www.UdiDahan.com

[email protected]

Page 2: 14147503 Intentions Interfaces Making Patterns Concrete

Books, and books, and books, and books…

Page 3: 14147503 Intentions Interfaces Making Patterns Concrete

Flexibility brings many benefits

Page 4: 14147503 Intentions Interfaces Making Patterns Concrete

Preventing rigidity from creeping in

Page 5: 14147503 Intentions Interfaces Making Patterns Concrete

Visitor Pattern

Strategy Pattern

Existing solutions

Page 6: 14147503 Intentions Interfaces Making Patterns Concrete

Visitor pattern

Requires a method for

each ConcreteElement

Page 7: 14147503 Intentions Interfaces Making Patterns Concrete

Strategy pattern

Requires all classes to

contain a strategy

Page 8: 14147503 Intentions Interfaces Making Patterns Concrete

Application Code

Infrastructure Code

Infrastructure CodeApp

lic

at

ion

Co

de

May cause a collapse of application structure

Page 9: 14147503 Intentions Interfaces Making Patterns Concrete

Doing toomuch can hurt

ou

ren„t

onna

eed

t

Page 10: 14147503 Intentions Interfaces Making Patterns Concrete

“Prediction is verydifficult, especially ifit's about the future.”

-- Niels BohrPhysics Nobel prize 1922

Page 11: 14147503 Intentions Interfaces Making Patterns Concrete

Bolt on flexibility where you need it

Page 12: 14147503 Intentions Interfaces Making Patterns Concrete

Application Code

Infrastructure Code

New

Code

New

Code

Sometimes called “hooks”

Page 13: 14147503 Intentions Interfaces Making Patterns Concrete

Flexibility you seek?

Hmm?

Made your roles explicit, have you?

No?

That is why you fail.

Page 14: 14147503 Intentions Interfaces Making Patterns Concrete

Make Roles

Explicit

Page 15: 14147503 Intentions Interfaces Making Patterns Concrete

Some well known interfaces

IFather

IHusband

IGoToWork

IComeHome

IWashTheDishes

Serializable

ISerializable

Java

Page 16: 14147503 Intentions Interfaces Making Patterns Concrete
Page 17: 14147503 Intentions Interfaces Making Patterns Concrete
Page 18: 14147503 Intentions Interfaces Making Patterns Concrete

Custom entity validation before persistence

Page 19: 14147503 Intentions Interfaces Making Patterns Concrete

The old “object-oriented” way

Customer

.Validate();

Order

.Validate();

* Address

.Validate();

But what if we start here?

bool IsValidating;

Persistence

Page 20: 14147503 Intentions Interfaces Making Patterns Concrete

It looks like our objects havetoo many roles to play

Page 21: 14147503 Intentions Interfaces Making Patterns Concrete

Make roles explicit

Page 22: 14147503 Intentions Interfaces Making Patterns Concrete

IEntity

IValidator<T>ValidationError Validate(T entity);

where T : IEntity

Add a marker interface here,and an interface there….

Page 23: 14147503 Intentions Interfaces Making Patterns Concrete

IEntity

Customer

Order

The first part is trivial:

Page 24: 14147503 Intentions Interfaces Making Patterns Concrete

The second part is more interesting

IValidator<T>ValidationError Validate(T entity);

Customer

CustomerValidator: IValidator<Customer>

ValidationError Validate(Customer entity);

Page 25: 14147503 Intentions Interfaces Making Patterns Concrete

Add a dash ofInversion of Control

Service-Locator style

Page 26: 14147503 Intentions Interfaces Making Patterns Concrete

The extensible way

.Persist(Customer)Persistence

Service Locator

Customer Validator

new

Validate(Customer)

Get<IValidator<Customer>>

Page 27: 14147503 Intentions Interfaces Making Patterns Concrete

But that’s not Object Oriented

Is it?

Page 28: 14147503 Intentions Interfaces Making Patterns Concrete

Extensible and Object Oriented

.Persist(Customer)Persistence

Service Locator

Customer Validator

new Validate(Customer)

Get<IValidator<Customer>>

Customer

.Validate();

Page 29: 14147503 Intentions Interfaces Making Patterns Concrete

And application code stays simple

Application Code

Infrastructure Code

.Persist(Customer)

Page 30: 14147503 Intentions Interfaces Making Patterns Concrete

Loading objects from the DB

public class Customer{public void MakePreferred(){

foreach(Order o in this.UnshippedOrders)foreach(Orderline ol in o.OrderLines)

ol.Discount(10.Percent);}

}

Lazy Loading

Page 31: 14147503 Intentions Interfaces Making Patterns Concrete

Dangers of Lazy Loading

public void MakePreferred(){foreach(Order o in this.UnshippedOrders)

foreach(Orderline ol in o.OrderLines)ol.Discount(10.Percent);

}

DB

Page 32: 14147503 Intentions Interfaces Making Patterns Concrete

Loading objects from the DB

Making a customer

“preferred”

Customer

Order

OrderLine

Adding an Order

Customer

Page 33: 14147503 Intentions Interfaces Making Patterns Concrete

Need Different “Fetching Strategies”

public class ServiceLayer{public void MakePreferred(Id customerId){

Customer c = ORM.Get<Customer>(customerId);c.MakePreferred();

}

public void AddOrder(Id customerId, OrderInfo o){

Customer c = ORM.Get<Customer>(customerId);c.AddOrder(o);

}}

Page 34: 14147503 Intentions Interfaces Making Patterns Concrete

Make Roles

Explicit

Page 35: 14147503 Intentions Interfaces Making Patterns Concrete

Use interfaces to differentiate roles

Customer

IMakeCustomerPreferred IAddOrdersToCustomer

void MakePreferred(); void AddOrder(Order o);

Page 36: 14147503 Intentions Interfaces Making Patterns Concrete

public class ServiceLayer{public void MakePreferred(Id customerId){

IMakeCustomerPreferred c = ORM.Get< IMakeCustomerPreferred>(customerId);

c.MakePreferred();}

public void AddOrder(Id customerId, OrderInfo o){

IAddOrdersToCustomer c = ORM.Get< IAddOrdersToCustomer>(customerId);

c.AddOrder(o);}

}

Application code specifies role

Page 37: 14147503 Intentions Interfaces Making Patterns Concrete

Extend behavior around role

Customer

IMakeCustomerPreferred IAddOrdersToCustomer

void MakePreferred(); void AddOrder(Order o);

MakeCustomerPreferredFetchingStrategy :IFetchingStrategy<IMakeCustomerPreferred>

string Strategy { get {

return “UnshippedOrders, OrderLines”; } }IFetchingStrategy<T>

Inherits

T

Page 38: 14147503 Intentions Interfaces Making Patterns Concrete

The extensible way

.Get<IMakeCustomerPreferred>(id)

Persistence

Service Locator

MakeCustomerPreferredFetchingStrategy

new

Get strategy

Get<IFetchingStrategy<IMakeCustomerPreferred>>

Page 39: 14147503 Intentions Interfaces Making Patterns Concrete

IMessage

IMessageHandler<T>void Handle(T message);

where T : IEntity

And the pattern repeats…

Page 40: 14147503 Intentions Interfaces Making Patterns Concrete

Once your rolesmade explicit, you have…

Extensibility andflexibility -simple they will be

Page 41: 14147503 Intentions Interfaces Making Patterns Concrete

Thank you

Udi Dahan – The Software Simplist

.NET Development Expert & SOA Specialist

www.UdiDahan.com

[email protected]