1 / 52

Intentions & Interfaces Making Patterns Concrete

Intentions & Interfaces Making Patterns Concrete. Udi Dahan – The Software Simplist Enterprise Development Expert & SOA Specialist. Session Code: <Session code>. Books, and books, and books, and books…. Flexibility brings many benefits. Agenda. Intentional Interfaces. Data Access.

mitch
Download Presentation

Intentions & Interfaces Making Patterns Concrete

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Intentions & InterfacesMaking Patterns Concrete Udi Dahan – The Software Simplist Enterprise Development Expert & SOA Specialist Session Code: <Session code>

  2. Books, and books, and books, and books…

  3. Flexibility brings many benefits

  4. Agenda Intentional Interfaces Data Access Service Layer Validation

  5. Preventing rigidity from creeping in

  6. Existing solutions Strategy Pattern Visitor Pattern

  7. Visitor pattern Requires a method for each ConcreteElement

  8. Strategy pattern Requires all classes to contain a strategy

  9. Danger Application Code May cause a collapse of application structure Infrastructure Code at Co ion de lic App Infrastructure Code

  10. Doing too muchcan hurt Y ou A ren‘t G onna N eed I t

  11. What needs to flex? “Prediction is very difficult, • especially if it's about the future.” -- Niels Bohr Physics Nobel prize 1922

  12. Bolt on flexibility where you need it

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

  14. Make Roles Explicit

  15. Some well known interfaces ISerializable IFather IHusband Serializable IGoToWork IComeHome IWashTheDishes

  16. Intentional Interfaces Data Access Service Layer Validation

  17. Custom validation before persistence

  18. The old “object-oriented” way • boolIsValidating; .Validate(); Customer .Validate(); Address * .Validate(); Order But what if we start here? Persistence .Persist(c);

  19. Too many roles

  20. Make Roles Explicit

  21. Roles as Marker Interfaces IEntity Customer Order IValidator<T> ValidationError Validate(T entity); where T : IEntity

  22. Validation Implementation IValidator<T> Customer CustomerValidator: IValidator<Customer> ValidationError Validate(T entity); ValidationError Validate(Customer entity);

  23. Extensible validation when persisting .Persist(Customer) Persistence Service Locator Get<IValidator<Customer>> new Validate(Customer) Customer Validator

  24. But that’s not Object OrientedIs it?

  25. Extensible and Object Oriented Customer Validator .Validate(); Customer

  26. Application code stays simple Application Code .Persist(Customer) Infrastructure Code

  27. Intentional Interfaces Data Access Service Layer Validation

  28. Regular Service Layer Code publicvoidMake_Customer_Preferred(Guid id) { using (ISession s = ORM.Open_Session()) using (ITransactiontx = s.Begin_Transaction()) { Customer c = s.Get<Customer>(id); c.Make_Preferred(); tx.Commit(); } }

  29. Loading graphs of objects public class Customer { publicvoidMake_Preferred() { foreach(Order o inthis.UnshippedOrders) foreach(Orderlineolino.OrderLines) ol.Discount(10.Percent); } } Lazy Loading

  30. Dangers of Lazy Loading public class Customer { publicvoidMake_Preferred() { foreach(Order o inthis.UnshippedOrders) foreach(Orderlineolino.OrderLines) ol.Discount(10.Percent); } } DB

  31. Loading graphs of objects Making a customer “preferred” Adding an Order Customer Customer Order OrderLine

  32. Different “Fetching Strategies” for the same customer

  33. Make Roles Explicit

  34. Use interfaces to differentiate roles IMakeCustomerPreferred IAddOrdersToCustomer void MakePreferred(); void AddOrder(Order o); Customer

  35. Service Layer Specifies Role publicvoidMake_Customer_Preferred(Guid id) { using (ISession s = ORM.Open_Session()) using (ITransactiontx = s.Begin_Transaction()) { IMakeCustomerPreferred c = s.Get<IMakeCustomerPreferred>(id); c.Make_Preferred(); tx.Commit(); } }

  36. Infrastructure Roles The Fetching Strategy

  37. Extend behavior around role IMakeCustomerPreferred IAddOrdersToCustomer void MakePreferred(); void AddOrder(Order o); Customer T MakeCustomerPreferredFetchingStrategy : IFetchingStrategy<IMakeCustomerPreferred> Inherits string Strategy { get { return “UnshippedOrders, OrderLines”; } } IFetchingStrategy<T>

  38. The Extensible Way .Get<IMakeCustomerPreferred>(id) Persistence Load graph Service Locator Get<IFetchingStrategy<IMakeCustomerPreferred>> DB new Get strategy Make_Customer_Preferred_Fetching_Strategy

  39. Intentional Interfaces Data Access Service Layer Validation

  40. Regular Service Layer Code Customer Service void Change_Address(Guid id, Address a); void Make_Preferred(Guid id); voidChange_Credit(Credit c);

  41. How developers collaborate…

  42. Applying the pattern again… IMessage IMessageHandler<T> void Handle(T message); where T : IMessage

  43. The messages: Change_Address_Msg Make_Preferred_Msg Change_Credit_Msg IMessage

  44. The message handlers: H1: H2: H3: IMessageHandler<T> void Handle(T message); IMessageHandler<Change_Address_Msg> IMessageHandler<Make_Preferred_Msg> IMessageHandler<Change_Credit_Msg>

  45. Now everybody’s happy…

  46. There can be more than one… H1: H4: Change_Address_Msg IMessageHandler<Change_Address_Msg> IMessageHandler<Change_Address_Msg>

  47. Putting it all together… .Handle(m); Service Locator .Handle(m); GetAll<IMessageHandler<Change_Address_Msg>> new H1 H4

  48. Intentional Interfaces Data Access Service Layer Validation

More Related