1 / 36

Designing High Performance, Persistent Domain Models

Designing High Performance, Persistent Domain Models. Udi Dahan – The Software Simplist Enterprise Development Expert & SOA Specialist. Session Code: <Session code>. What most architecture looks like today. But where do business rules go?. Here?. Here?. Here?. Here?. Here?. Here?.

bran
Download Presentation

Designing High Performance, Persistent Domain Models

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. Designing High Performance,Persistent Domain Models Udi Dahan – The Software Simplist Enterprise Development Expert & SOA Specialist Session Code: <Session code>

  2. What most architecture looks like today

  3. But where do business rules go? Here? Here? Here? Here? Here? Here?

  4. No real reuse between tiers

  5. The book that changed everything ISBN 0321127420

  6. The Domain Model Pattern Methods Properties Events

  7. Independent of all concerns DB UI Communications

  8. Deploy On All Tiers it’s just a bunch of DLLs – or even just one DB SQL CLR

  9. Write Once - Run Everywhere

  10. Domain Models are made of POCOs public class Product { // properties // methods } public class Customer { // properties // methods } public class Order { // properties // methods } public class OrderLine { // properties // methods }

  11. Highly Testable Register for events Test Call a method Check properties & event args

  12. Persisting Domain Models to the DB ? But who’s in charge Object Relational Mapping DB

  13. Service Layer Encapsulates ORM Service Layer Object Relational Mapping Manages connections and transactions DB

  14. Service Layer Code publicvoidMake_Customer_Preferred(Guidid) { using (ISession s = ORM.Open_Session()) using (ITransactiontx = s.Begin_Transaction()) { Customerc = s.Get<Customer>(id); c.Make_Preferred(); tx.Commit(); } } Solves Concurrency

  15. Domain Model Sample Lazy Loading public class Customer { publicvoidMake_Preferred() { foreach(Orderointhis.UnshippedOrders) foreach(Orderlineolino.OrderLines) ol.Discount(10.Percent); } }

  16. Lazy Loading – load what you use Customer Order ? OrderLine Why load Orders and Order Lines when you just want to add a new order

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

  18. Minimize Roundtrips: Eager Fetching • publicvoidMake_Customer_Preferred(Guid id) • { • using (ISession s = ORM.Open_Session()) • using (ITransactiontx = s.Begin_Transaction()) • { • var c = s.Get<Customer>(id); • c.Make_Preferred(); • tx.Commit(); • } • } Need to get all data (Orders, Orderlines, etc) No need to hit DB here

  19. Easier said than done The same entity can be loaded many ways Making a customer “preferred” Adding an Order Customer Customer Order OrderLine

  20. I want it all Pretty please?

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

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

  23. Fetching Strategy Per Role IMakeCustomerPreferred void MakePreferred(); MakeCustomerPreferredFetchingStrategy : IFetchingStrategy<IMakeCustomerPreferred> string Strategy { get { return “UnshippedOrders, OrderLines”; } } IFetchingStrategy<T>

  24. ORM Uses Fetching StrategyIFetchingStrategy<IMakeCustomerPreferred> • publicvoidMake_Customer_Preferred(Guid id) • { • using (ISession s = ORM.Open_Session()) • using (ITransactiontx = s.Begin_Transaction()) • { • var c = s.Get<IMakeCustomerPreferred>(id); • c.Make_Preferred(); • tx.Commit(); • } • }

  25. New Rule for “Add Order” Yes > $100K 10 % Discount Existing Orders No

  26. Eager fetching brings a lot of data Rule Implementation • public class Customer • { • publicvoidAdd_Order(Order o) • { • float sum = Total(this.ShippedOrders); • if (sum > 100.Thousand) • o.Discount(10.Percent); • this.Orders.Add(o); • o.Customer = this; • } • }

  27. Member replaces iteration Σ sumOfShippedOrders Update ORM Q Persistent Update when orders ship + Use when adding orders DB

  28. New Rule Implementation No Fetching Strategy Needed • public class Customer • { • publicvoidAdd_Order(Order o) • { • if (this.sumOfShippedOrders > 100.Thousand) • o.Discount(10.Percent); • this.Orders.Add(o); • o.Customer = this; • } • } this.sumOfShippedOrders

  29. Joins hurt scalability: De-NormalizeVertically partition tables to increase throughput Keep the same interface, change internals ORM DB Update mapping & schema

  30. But some rules have to be in the DBdon’t they? No Duplicate user names @ No Duplicate email addresses Unique Constraints work

  31. Memory of a single server can hold: • 64 million user names = ~ 2 GB • 16 char username @ • 48 million emails = ~ 2 GB • 40 char email Partition data load across servers for much more

  32. Low latency, high throughputand maintainable Keeps the boss happy

  33. So I don’t get stuck at work

  34. Which makes my kids happy

  35. :-)

  36. Thank you Udi Dahan – The Software Simplist Enterprise Development Expert & SOA Specialist email@UdiDahan.com www.UdiDahan.com

More Related