udi dahan intentions and interfaces

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

Upload: deimos

Post on 06-May-2015

3.770 views

Category:

Technology


3 download

TRANSCRIPT

Page 1: Udi Dahan Intentions And Interfaces

Intentions & InterfacesMaking patterns concrete

Udi Dahan – The Software Simplist

.NET Development Expert & SOA Specialist

www.UdiDahan.com

[email protected]

Page 2: Udi Dahan Intentions And Interfaces

Books, and books, and books, and books…

Page 3: Udi Dahan Intentions And Interfaces

Flexibility brings many benefits

Page 4: Udi Dahan Intentions And Interfaces

Preventing rigidity from creeping in

Page 5: Udi Dahan Intentions And Interfaces

Visitor Pattern

Strategy Pattern

Existing solutions

Page 6: Udi Dahan Intentions And Interfaces

Visitor pattern

Requires a method for

each ConcreteElement

Page 7: Udi Dahan Intentions And Interfaces

Strategy pattern

Requires all classes to

contain a strategy

Page 8: Udi Dahan Intentions And Interfaces

Application Code

Infrastructure Code

Infrastructure CodeApp

lic

at

ion

Co

de

May cause a collapse of application structure

Page 9: Udi Dahan Intentions And Interfaces

Doing toomuch can hurt

ou

ren„t

onna

eed

t

Page 10: Udi Dahan Intentions And Interfaces

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

-- Niels BohrPhysics Nobel prize 1922

Page 11: Udi Dahan Intentions And Interfaces

Bolt on flexibility where you need it

Page 12: Udi Dahan Intentions And Interfaces

Application Code

Infrastructure Code

New

Code

New

Code

Sometimes called “hooks”

Page 13: Udi Dahan Intentions And Interfaces

Flexibility you seek?

Hmm?

Made your roles explicit, have you?

No?

That is why you fail.

Page 14: Udi Dahan Intentions And Interfaces

Make Roles

Explicit

Page 15: Udi Dahan Intentions And Interfaces

Some well known interfaces

IFather

IHusband

IGoToWork

IComeHome

IWashTheDishes

Serializable

ISerializable

Java

Page 16: Udi Dahan Intentions And Interfaces
Page 17: Udi Dahan Intentions And Interfaces
Page 18: Udi Dahan Intentions And Interfaces

Custom entity validation before persistence

Page 19: Udi Dahan Intentions And Interfaces

The old “object-oriented” way

Customer

.Validate();

Order

.Validate();

* Address

.Validate();

But what if we start here?

bool IsValidating;

Persistence

Page 20: Udi Dahan Intentions And Interfaces

It looks like our objects havetoo many roles to play

Page 21: Udi Dahan Intentions And Interfaces

Make roles explicit

Page 22: Udi Dahan Intentions And Interfaces

IEntity

IValidator<T>ValidationError Validate(T entity);

where T : IEntity

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

Page 23: Udi Dahan Intentions And Interfaces

IEntity

Customer

Order

The first part is trivial:

Page 24: Udi Dahan Intentions And Interfaces

The second part is more interesting

IValidator<T>ValidationError Validate(T entity);

Customer

CustomerValidator: IValidator<Customer>

ValidationError Validate(Customer entity);

Page 25: Udi Dahan Intentions And Interfaces

Add a dash ofInversion of Control

Service-Locator style

Page 26: Udi Dahan Intentions And Interfaces

The extensible way

.Persist(Customer)Persistence

Service Locator

Customer Validator

new

Validate(Customer)

Get<IValidator<Customer>>

Page 27: Udi Dahan Intentions And Interfaces

But that’s not Object Oriented

Is it?

Page 28: Udi Dahan Intentions And Interfaces

Extensible and Object Oriented

.Persist(Customer)Persistence

Service Locator

Customer Validator

new Validate(Customer)

Get<IValidator<Customer>>

Customer

.Validate();

Page 29: Udi Dahan Intentions And Interfaces

And application code stays simple

Application Code

Infrastructure Code

.Persist(Customer)

Page 30: Udi Dahan Intentions And Interfaces

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: Udi Dahan Intentions And Interfaces

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: Udi Dahan Intentions And Interfaces

Loading objects from the DB

Making a customer

“preferred”

Customer

Order

OrderLine

Adding an Order

Customer

Page 33: Udi Dahan Intentions And Interfaces

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: Udi Dahan Intentions And Interfaces

Make Roles

Explicit

Page 35: Udi Dahan Intentions And Interfaces

Use interfaces to differentiate roles

Customer

IMakeCustomerPreferred IAddOrdersToCustomer

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

Page 36: Udi Dahan Intentions And Interfaces

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: Udi Dahan Intentions And Interfaces

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: Udi Dahan Intentions And Interfaces

The extensible way

.Get<IMakeCustomerPreferred>(id)

Persistence

Service Locator

MakeCustomerPreferredFetchingStrategy

new

Get strategy

Get<IFetchingStrategy<IMakeCustomerPreferred>>

Page 39: Udi Dahan Intentions And Interfaces

IMessage

IMessageHandler<T>void Handle(T message);

where T : IEntity

And the pattern repeats…

Page 40: Udi Dahan Intentions And Interfaces

Once your rolesmade explicit, you have…

Extensibility andflexibility -simple they will be

Page 41: Udi Dahan Intentions And Interfaces

Thank you

Udi Dahan – The Software Simplist

.NET Development Expert & SOA Specialist

www.UdiDahan.com

[email protected]