Download - Udi Dahan Intentions And Interfaces
![Page 1: Udi Dahan Intentions And Interfaces](https://reader036.vdocuments.us/reader036/viewer/2022081717/55494367b4c9050f4d8b5337/html5/thumbnails/1.jpg)
Intentions & InterfacesMaking patterns concrete
Udi Dahan – The Software Simplist
.NET Development Expert & SOA Specialist
www.UdiDahan.com
![Page 2: Udi Dahan Intentions And Interfaces](https://reader036.vdocuments.us/reader036/viewer/2022081717/55494367b4c9050f4d8b5337/html5/thumbnails/2.jpg)
Books, and books, and books, and books…
![Page 3: Udi Dahan Intentions And Interfaces](https://reader036.vdocuments.us/reader036/viewer/2022081717/55494367b4c9050f4d8b5337/html5/thumbnails/3.jpg)
Flexibility brings many benefits
![Page 4: Udi Dahan Intentions And Interfaces](https://reader036.vdocuments.us/reader036/viewer/2022081717/55494367b4c9050f4d8b5337/html5/thumbnails/4.jpg)
Preventing rigidity from creeping in
![Page 5: Udi Dahan Intentions And Interfaces](https://reader036.vdocuments.us/reader036/viewer/2022081717/55494367b4c9050f4d8b5337/html5/thumbnails/5.jpg)
Visitor Pattern
Strategy Pattern
Existing solutions
![Page 6: Udi Dahan Intentions And Interfaces](https://reader036.vdocuments.us/reader036/viewer/2022081717/55494367b4c9050f4d8b5337/html5/thumbnails/6.jpg)
Visitor pattern
Requires a method for
each ConcreteElement
![Page 7: Udi Dahan Intentions And Interfaces](https://reader036.vdocuments.us/reader036/viewer/2022081717/55494367b4c9050f4d8b5337/html5/thumbnails/7.jpg)
Strategy pattern
Requires all classes to
contain a strategy
![Page 8: Udi Dahan Intentions And Interfaces](https://reader036.vdocuments.us/reader036/viewer/2022081717/55494367b4c9050f4d8b5337/html5/thumbnails/8.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081717/55494367b4c9050f4d8b5337/html5/thumbnails/9.jpg)
Doing toomuch can hurt
ou
ren„t
onna
eed
t
![Page 10: Udi Dahan Intentions And Interfaces](https://reader036.vdocuments.us/reader036/viewer/2022081717/55494367b4c9050f4d8b5337/html5/thumbnails/10.jpg)
“Prediction is verydifficult, especially ifit's about the future.”
-- Niels BohrPhysics Nobel prize 1922
![Page 11: Udi Dahan Intentions And Interfaces](https://reader036.vdocuments.us/reader036/viewer/2022081717/55494367b4c9050f4d8b5337/html5/thumbnails/11.jpg)
Bolt on flexibility where you need it
![Page 12: Udi Dahan Intentions And Interfaces](https://reader036.vdocuments.us/reader036/viewer/2022081717/55494367b4c9050f4d8b5337/html5/thumbnails/12.jpg)
Application Code
Infrastructure Code
New
Code
New
Code
Sometimes called “hooks”
![Page 13: Udi Dahan Intentions And Interfaces](https://reader036.vdocuments.us/reader036/viewer/2022081717/55494367b4c9050f4d8b5337/html5/thumbnails/13.jpg)
Flexibility you seek?
Hmm?
Made your roles explicit, have you?
No?
That is why you fail.
![Page 14: Udi Dahan Intentions And Interfaces](https://reader036.vdocuments.us/reader036/viewer/2022081717/55494367b4c9050f4d8b5337/html5/thumbnails/14.jpg)
Make Roles
Explicit
![Page 15: Udi Dahan Intentions And Interfaces](https://reader036.vdocuments.us/reader036/viewer/2022081717/55494367b4c9050f4d8b5337/html5/thumbnails/15.jpg)
Some well known interfaces
IFather
IHusband
IGoToWork
IComeHome
IWashTheDishes
Serializable
ISerializable
Java
![Page 16: Udi Dahan Intentions And Interfaces](https://reader036.vdocuments.us/reader036/viewer/2022081717/55494367b4c9050f4d8b5337/html5/thumbnails/16.jpg)
![Page 17: Udi Dahan Intentions And Interfaces](https://reader036.vdocuments.us/reader036/viewer/2022081717/55494367b4c9050f4d8b5337/html5/thumbnails/17.jpg)
![Page 18: Udi Dahan Intentions And Interfaces](https://reader036.vdocuments.us/reader036/viewer/2022081717/55494367b4c9050f4d8b5337/html5/thumbnails/18.jpg)
Custom entity validation before persistence
![Page 19: Udi Dahan Intentions And Interfaces](https://reader036.vdocuments.us/reader036/viewer/2022081717/55494367b4c9050f4d8b5337/html5/thumbnails/19.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081717/55494367b4c9050f4d8b5337/html5/thumbnails/20.jpg)
It looks like our objects havetoo many roles to play
![Page 21: Udi Dahan Intentions And Interfaces](https://reader036.vdocuments.us/reader036/viewer/2022081717/55494367b4c9050f4d8b5337/html5/thumbnails/21.jpg)
Make roles explicit
![Page 22: Udi Dahan Intentions And Interfaces](https://reader036.vdocuments.us/reader036/viewer/2022081717/55494367b4c9050f4d8b5337/html5/thumbnails/22.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081717/55494367b4c9050f4d8b5337/html5/thumbnails/23.jpg)
IEntity
Customer
Order
The first part is trivial:
![Page 24: Udi Dahan Intentions And Interfaces](https://reader036.vdocuments.us/reader036/viewer/2022081717/55494367b4c9050f4d8b5337/html5/thumbnails/24.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081717/55494367b4c9050f4d8b5337/html5/thumbnails/25.jpg)
Add a dash ofInversion of Control
Service-Locator style
![Page 26: Udi Dahan Intentions And Interfaces](https://reader036.vdocuments.us/reader036/viewer/2022081717/55494367b4c9050f4d8b5337/html5/thumbnails/26.jpg)
The extensible way
.Persist(Customer)Persistence
Service Locator
Customer Validator
new
Validate(Customer)
Get<IValidator<Customer>>
![Page 27: Udi Dahan Intentions And Interfaces](https://reader036.vdocuments.us/reader036/viewer/2022081717/55494367b4c9050f4d8b5337/html5/thumbnails/27.jpg)
But that’s not Object Oriented
Is it?
![Page 28: Udi Dahan Intentions And Interfaces](https://reader036.vdocuments.us/reader036/viewer/2022081717/55494367b4c9050f4d8b5337/html5/thumbnails/28.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081717/55494367b4c9050f4d8b5337/html5/thumbnails/29.jpg)
And application code stays simple
Application Code
Infrastructure Code
.Persist(Customer)
![Page 30: Udi Dahan Intentions And Interfaces](https://reader036.vdocuments.us/reader036/viewer/2022081717/55494367b4c9050f4d8b5337/html5/thumbnails/30.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081717/55494367b4c9050f4d8b5337/html5/thumbnails/31.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081717/55494367b4c9050f4d8b5337/html5/thumbnails/32.jpg)
Loading objects from the DB
Making a customer
“preferred”
Customer
Order
OrderLine
Adding an Order
Customer
![Page 33: Udi Dahan Intentions And Interfaces](https://reader036.vdocuments.us/reader036/viewer/2022081717/55494367b4c9050f4d8b5337/html5/thumbnails/33.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081717/55494367b4c9050f4d8b5337/html5/thumbnails/34.jpg)
Make Roles
Explicit
![Page 35: Udi Dahan Intentions And Interfaces](https://reader036.vdocuments.us/reader036/viewer/2022081717/55494367b4c9050f4d8b5337/html5/thumbnails/35.jpg)
Use interfaces to differentiate roles
Customer
IMakeCustomerPreferred IAddOrdersToCustomer
void MakePreferred(); void AddOrder(Order o);
![Page 36: Udi Dahan Intentions And Interfaces](https://reader036.vdocuments.us/reader036/viewer/2022081717/55494367b4c9050f4d8b5337/html5/thumbnails/36.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081717/55494367b4c9050f4d8b5337/html5/thumbnails/37.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081717/55494367b4c9050f4d8b5337/html5/thumbnails/38.jpg)
The extensible way
.Get<IMakeCustomerPreferred>(id)
Persistence
Service Locator
MakeCustomerPreferredFetchingStrategy
new
Get strategy
Get<IFetchingStrategy<IMakeCustomerPreferred>>
![Page 39: Udi Dahan Intentions And Interfaces](https://reader036.vdocuments.us/reader036/viewer/2022081717/55494367b4c9050f4d8b5337/html5/thumbnails/39.jpg)
IMessage
IMessageHandler<T>void Handle(T message);
where T : IEntity
And the pattern repeats…
![Page 40: Udi Dahan Intentions And Interfaces](https://reader036.vdocuments.us/reader036/viewer/2022081717/55494367b4c9050f4d8b5337/html5/thumbnails/40.jpg)
Once your rolesmade explicit, you have…
Extensibility andflexibility -simple they will be
![Page 41: Udi Dahan Intentions And Interfaces](https://reader036.vdocuments.us/reader036/viewer/2022081717/55494367b4c9050f4d8b5337/html5/thumbnails/41.jpg)
Thank you
Udi Dahan – The Software Simplist
.NET Development Expert & SOA Specialist
www.UdiDahan.com