Intentions & Interfaces Making patterns concrete Udi Dahan The - - PowerPoint PPT Presentation

intentions interfaces
SMART_READER_LITE
LIVE PREVIEW

Intentions & Interfaces Making patterns concrete Udi Dahan The - - PowerPoint PPT Presentation

Intentions & Interfaces Making patterns concrete Udi Dahan The Software Simplist .NET Development Expert & SOA Specialist www.UdiDahan.com email@UdiDahan.com Books, and books, and books, and books Flexibility brings many benefits


slide-1
SLIDE 1

Intentions & Interfaces

Making patterns concrete

Udi Dahan – The Software Simplist

.NET Development Expert & SOA Specialist

www.UdiDahan.com email@UdiDahan.com

slide-2
SLIDE 2

Books, and books, and books, and books…

slide-3
SLIDE 3

Flexibility brings many benefits

slide-4
SLIDE 4

Preventing rigidity from creeping in

slide-5
SLIDE 5

Visitor Pattern Strategy Pattern

Existing solutions

slide-6
SLIDE 6

Visitor pattern

Requires a method for each ConcreteElement

slide-7
SLIDE 7

Strategy pattern

Requires all classes to contain a strategy

slide-8
SLIDE 8

Applicati lication

  • n Cod
  • de

In Infrastr rastructure ucture Code In Infr fras astructu tructure re Cod

  • de

App lic lic at at io ion Co Co de de May cause a collapse of application structure

slide-9
SLIDE 9

Doing too much can hurt

  • u

ren„t

  • nna

eed t

slide-10
SLIDE 10

“Prediction is very difficult, especially if it's about the future.”

  • - Niels Bohr

Physics Nobel prize 1922

slide-11
SLIDE 11

Bolt on flexibility where you need it

slide-12
SLIDE 12

Applicati lication

  • n Cod
  • de

In Infr frastruc astructure ture Cod

  • de

New w Code Code New Code Code

Sometimes called “hooks”

slide-13
SLIDE 13

Flexibility you seek? Hmm? Made your roles explicit, have you? No? That is why you fail.

slide-14
SLIDE 14

Make Roles Explicit

slide-15
SLIDE 15

Some well known interfaces

IFather IHusband IGoToWork IComeHome IWashTheDishes Serializable ISerializable Java

slide-16
SLIDE 16
slide-17
SLIDE 17
slide-18
SLIDE 18

Custom entity validation before persistence

slide-19
SLIDE 19

The old “object-oriented” way

Cust ustomer

  • mer

.Validate();

Order der

.Validate(); *

Ad Address dress

.Validate();

But what if we start here? bool IsValidating;

Persis sistenc ence

slide-20
SLIDE 20

It looks like our

  • bjects have

too many roles to play

slide-21
SLIDE 21

Make roles explicit

slide-22
SLIDE 22

IEntity IValidator<T>

ValidationError Validate(T entity); where T : IEntity

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

slide-23
SLIDE 23

IEntity Customer Order

The first part is trivial:

slide-24
SLIDE 24

The second part is more interesting

IValidator<T>

ValidationError Validate(T entity);

Customer CustomerValidator: IValidator<Customer>

ValidationError Validate(Customer entity);

slide-25
SLIDE 25

Add a dash of Inversion of Control

Service-Locator style

slide-26
SLIDE 26

The extensible way

.Persist(Customer)

Persis sistenc ence Service vice Lo Loca cator

  • r

Cust stomer

  • mer Val

alid idat ator

  • r

new Validate(Customer) Get<IValidator<Customer>>

slide-27
SLIDE 27

But that’s not Object Oriented Is it?

slide-28
SLIDE 28

Extensible and Object Oriented

.Persist(Customer)

Persis sistenc ence Service vice Lo Loca cator

  • r

Cust stomer

  • mer Val

alid idat ator

  • r

new Validate(Customer) Get<IValidator<Customer>>

Cust stomer

  • mer

.Validate();

slide-29
SLIDE 29

And application code stays simple

Applicati lication

  • n Cod
  • de

In Infr frastruc astructure ture Cod

  • de

.Persist(Customer)

slide-30
SLIDE 30

Loading objects from the DB

public class Customer { public void MakePreferred() { foreach(Order o in this.UnshippedOrders) foreach(Orderline ol in o.OrderLines)

  • l.Discount(10.Percent);

} } Lazy Loading

slide-31
SLIDE 31

Dangers of Lazy Loading

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

  • l.Discount(10.Percent);

}

DB

slide-32
SLIDE 32

Loading objects from the DB

Making a customer “preferred” Customer Order OrderLine Adding an Order Customer

slide-33
SLIDE 33

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); } }

slide-34
SLIDE 34

Make Roles Explicit

slide-35
SLIDE 35

Use interfaces to differentiate roles

Customer IMakeCustomerPreferred IAddOrdersToCustomer void MakePreferred(); void AddOrder(Order o);

slide-36
SLIDE 36

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

slide-37
SLIDE 37

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

slide-38
SLIDE 38

The extensible way

.Get<IMakeCustomerPreferred>(id)

Persis sistenc ence Service vice Lo Loca cator

  • r

MakeCustomerPreferredFetchingStrategy

new Get strategy Get<IFetchingStrategy< IMakeCustomerPreferred>>

slide-39
SLIDE 39

IMessage IMessageHandler<T>

void Handle(T message); where T : IEntity

And the pattern repeats…

slide-40
SLIDE 40

Once your roles made explicit, you have… Extensibility and flexibility - simple they will be

slide-41
SLIDE 41

Thank you

Udi Dahan – The Software Simplist

.NET Development Expert & SOA Specialist

www.UdiDahan.com email@UdiDahan.com