Intentions & Interfaces
Making patterns concrete


Udi Dahan – The Software Simplist
.NET Development Expert & SOA Special...
Books, and books, and books, and books…
Flexibility brings many benefits
Preventing rigidity from creeping in
Existing solutions

                     Strategy Pattern




 Visitor Pattern
Visitor pattern




            Requires a method for
            each ConcreteElement
Strategy pattern




                   Requires all classes to
                   contain a strategy
Application Code
May cause a collapse of
application structure
                               Infrastructure Code




    ...
Doing too
much can hurt

 ou
  ren„t
  onna
  eed
 t
“Prediction is very
 difficult, especially if
 it's about the future.”



-- Niels Bohr
   Physics Nobel prize 1922
Bolt on flexibility where you need it
Sometimes called “hooks”


          Application Code



         Infrastructure Code




                          New
  ...
Flexibility you seek?

Hmm?

Made your roles
explicit, have you?

No?

That is why you fail.
Make
 Roles
  Explicit
Some well known interfaces

        ISerializable          IFather

                             IHusband
 Java   Serializ...
Custom entity validation before persistence
The old “object-oriented” way
                          bool IsValidating;
.Validate();

      Customer         .Validate(...
It looks like our
objects have
too many roles
to play
Make roles explicit
Add a marker interface here,
and an interface there….
  IEntity
               where T : IEntity




                IVali...
The first part is trivial:
                                 IEntity




                      Customer


                 ...
The second part is more interesting
               IValidator<T>
      ValidationError Validate(T entity);




 Customer

...
Add a dash of
Inversion of Control

Service-Locator style
The extensible way
 .Persist(Customer)
                          Persistence

 Service Locator   Get<IValidator<Customer>>...
But that’s not
Object Oriented

Is it?
Extensible and Object Oriented
 .Persist(Customer)
                          Persistence

 Service Locator   Get<IValidato...
And application code stays simple
           Application Code


                     .Persist(Customer)


          Infras...
Loading objects from the DB
 public class Customer
 {
   public void MakePreferred()
   {
     foreach(Order o in this.Uns...
Dangers of Lazy Loading
 public void MakePreferred()
 {
   foreach(Order o in this.UnshippedOrders)
     foreach(Orderline...
Loading objects from the DB

 Making a customer
                        Adding an Order
    “preferred”

                 ...
Need Different “Fetching Strategies”
 public class ServiceLayer
 {
   public void MakePreferred(Id customerId)
   {
     C...
Make
 Roles
  Explicit
Use interfaces to differentiate roles
 IMakeCustomerPreferred              IAddOrdersToCustomer
  void MakePreferred();   ...
Application code specifies role
 public class ServiceLayer
 {
   public void MakePreferred(Id customerId)
   {
     IMakeC...
Extend behavior around role
   IMakeCustomerPreferred                   IAddOrdersToCustomer
     void MakePreferred();   ...
The extensible way
.Get<IMakeCustomerPreferred>(id)
                               Persistence

 Service Locator
         ...
And the pattern repeats…


  IMessage
              where T : IEntity




             IMessageHandler<T>
             voi...
Once your roles
made explicit,
you have…

Extensibility and
flexibility -
simple they will be
Thank you
 Udi Dahan – The Software Simplist
 .NET Development Expert & SOA Specialist

 www.UdiDahan.com
 email@UdiDahan....
14147503 Intentions Interfaces Making Patterns Concrete
14147503 Intentions Interfaces Making Patterns Concrete
Upcoming SlideShare
Loading in...5
×

14147503 Intentions Interfaces Making Patterns Concrete

534

Published on

Published in: Technology, Business
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
534
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
16
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

14147503 Intentions Interfaces Making Patterns Concrete

  1. 1. Intentions & Interfaces Making patterns concrete Udi Dahan – The Software Simplist .NET Development Expert & SOA Specialist www.UdiDahan.com email@UdiDahan.com
  2. 2. Books, and books, and books, and books…
  3. 3. Flexibility brings many benefits
  4. 4. Preventing rigidity from creeping in
  5. 5. Existing solutions Strategy Pattern Visitor Pattern
  6. 6. Visitor pattern Requires a method for each ConcreteElement
  7. 7. Strategy pattern Requires all classes to contain a strategy
  8. 8. Application Code May cause a collapse of application structure Infrastructure Code at Co ion de lic App Infrastructure Code
  9. 9. Doing too much can hurt ou ren„t onna eed t
  10. 10. “Prediction is very difficult, especially if it's about the future.” -- Niels Bohr Physics Nobel prize 1922
  11. 11. Bolt on flexibility where you need it
  12. 12. Sometimes called “hooks” Application Code Infrastructure Code New New Code Code
  13. 13. Flexibility you seek? Hmm? Made your roles explicit, have you? No? That is why you fail.
  14. 14. Make Roles Explicit
  15. 15. Some well known interfaces ISerializable IFather IHusband Java Serializable IGoToWork IComeHome IWashTheDishes
  16. 16. Custom entity validation before persistence
  17. 17. The old “object-oriented” way bool IsValidating; .Validate(); Customer .Validate(); * Address .Validate(); Order But what if we start here? Persistence
  18. 18. It looks like our objects have too many roles to play
  19. 19. Make roles explicit
  20. 20. Add a marker interface here, and an interface there…. IEntity where T : IEntity IValidator<T> ValidationError Validate(T entity);
  21. 21. The first part is trivial: IEntity Customer Order
  22. 22. The second part is more interesting IValidator<T> ValidationError Validate(T entity); Customer CustomerValidator: IValidator<Customer> ValidationError Validate(Customer entity);
  23. 23. Add a dash of Inversion of Control Service-Locator style
  24. 24. The extensible way .Persist(Customer) Persistence Service Locator Get<IValidator<Customer>> new Validate(Customer) Customer Validator
  25. 25. But that’s not Object Oriented Is it?
  26. 26. Extensible and Object Oriented .Persist(Customer) Persistence Service Locator Get<IValidator<Customer>> new Validate(Customer) Customer Validator .Validate(); Customer
  27. 27. And application code stays simple Application Code .Persist(Customer) Infrastructure Code
  28. 28. 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
  29. 29. Dangers of Lazy Loading public void MakePreferred() { foreach(Order o in this.UnshippedOrders) foreach(Orderline ol in o.OrderLines) ol.Discount(10.Percent); } DB
  30. 30. Loading objects from the DB Making a customer Adding an Order “preferred” Customer Customer Order OrderLine
  31. 31. 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); } }
  32. 32. Make Roles Explicit
  33. 33. Use interfaces to differentiate roles IMakeCustomerPreferred IAddOrdersToCustomer void MakePreferred(); void AddOrder(Order o); Customer
  34. 34. Application code specifies role 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); } }
  35. 35. Extend behavior around role IMakeCustomerPreferred IAddOrdersToCustomer void MakePreferred(); void AddOrder(Order o); Customer T MakeCustomerPreferredFetchingStrategy : Inherits IFetchingStrategy<IMakeCustomerPreferred> string Strategy { get { return “UnshippedOrders, OrderLines”; } } IFetchingStrategy<T>
  36. 36. The extensible way .Get<IMakeCustomerPreferred>(id) Persistence Service Locator Get<IFetchingStrategy< new IMakeCustomerPreferred>> Get strategy MakeCustomerPreferredFetchingStrategy
  37. 37. And the pattern repeats… IMessage where T : IEntity IMessageHandler<T> void Handle(T message);
  38. 38. Once your roles made explicit, you have… Extensibility and flexibility - simple they will be
  39. 39. Thank you Udi Dahan – The Software Simplist .NET Development Expert & SOA Specialist www.UdiDahan.com email@UdiDahan.com
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×