and stuff
tuple
//Initialize customer and invoice
Initialize(customer, invoice);
public void Initialize
(Customer customer, Invoice invoice)
{
customer.Name = “asdf”;
invoice.Date = DateTime.Now;
}
Initialize(customer, invoice);
//did something happen to customer
// and/or invoice?
customer.Name =
InitNameFrom(customer, invoice);
invoice.Date =
InitDateFrom(customer, invoice);
customer.Name =
GetNameFrom(customer, invoice);
invoice.Date =
GetDateFrom(customer, invoice);
var results =
Initialize(customer, invoice);
customer.Name = results.Item1;
invoice.Date = results.Item2;
public tuple<string, DateTime>
Initialize(customer, invoice)
{
return new Tuple<string, DateTime>
(“asdf”, DateTime.Now);
}
tuple
•Avoid side effects
•Avoid out parameters
•multiple values without a specific type
null object
private ILogger _logger;
public MyClass(ILogger logger) {
_logger = logger;
}
…
if (_logger != null) {
_logger.Debug(
“it ...
null checks for
everyone!
forget
one
and…
public class NullLogger : ILogger {
public void Debug(string text) {
//do sweet nothing
}
}
private ILogger _logger = new NullLogger();
public MyClass(ILogger logger) {
_logger = logger;
}
…
_logger.Debug(
“it work...
null object
•Can eliminate null checks
•Simple to implement
Circuit
Breaker
Retry
YourApplication
OutofProcess
Dependency
N times
OutofProcess
Dependency
N times
*
Y clients
=
Denial of
Service Attack
Limit the #
of retries
N * Y
becomes
5 * Y
Y is
still a
problem
Circuit
Breaker
State Machine
On :: Off
On  Off
when not healthy
Off  On
manually
Get to software
before we cut you
Healthy
or
Unhealthy
OutofProcess
Dependency
State is
independent of
requestor
OutofProcess
Dependency
YourApplication
Can throttle
itself
YourApplication Has many
independent
external
dependencies
YourApplication
Has a
wait
threshold
Your
Application
External
Dependency
Circuit
Breaker
Threshold = 2
Pause = 10ms
Timeout = 30s
State = Closed
Request
Reque...
Your
Application
External
Dependency
Circuit
Breaker
Threshold = 2
Pause = 10ms
Timeout = 30s
State = Open
Request
30s has...
Your
Application
External
Dependency
Circuit
Breaker
Threshold = 2
Pause = 10ms
Timeout = 30s
State = ½ Open
Request
Reque...
Your
Application
External
Dependency
Circuit
Breaker
Threshold = 2
Pause = 10ms
Timeout = 30s
State = ½ Open
Request
Reque...
Closed
Open
½ Open
½ Open
is like a
manual reset
Pause
Timeout
Pause
between calls
in the loop
Timeout
before you
can call again
Exceptions
OperationFailed
:
AggregateException
CircuitBreakerOpen
:
ApplicationException
Don’t Loose
Exception Info
Always use
InnerException(s)
Your
Application
External
Dependency
Circuit
Breaker
Threshold = 3
State = Closed
Request
Request
Failure (i.e. HTTP 500)
...
Segregate
Dependencies
circuitBreaker(“database”)
circuitBreaker(“weatherservice”)
Dependency
type,
endpoint svc,
endpoint
Where?
YourApplication
OutofProcess
Dependency
CircuitBreaker
Proxy
Watch for
Inception
YourApplication
WebService
CircuitBreaker
Proxy
Database
Repository
CircuitBreaker
circuit breaker
•retry looping
•slow down attempts
•good neighbour
Design patterns you didn't know about
Design patterns you didn't know about
Design patterns you didn't know about
Upcoming SlideShare
Loading in …5
×

Design patterns you didn't know about

2,754 views

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
2,754
On SlideShare
0
From Embeds
0
Number of Embeds
2,128
Actions
Shares
0
Downloads
0
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Design patterns you didn't know about

  1. 1. and stuff
  2. 2. tuple
  3. 3. //Initialize customer and invoice Initialize(customer, invoice);
  4. 4. public void Initialize (Customer customer, Invoice invoice) { customer.Name = “asdf”; invoice.Date = DateTime.Now; }
  5. 5. Initialize(customer, invoice); //did something happen to customer // and/or invoice?
  6. 6. customer.Name = InitNameFrom(customer, invoice); invoice.Date = InitDateFrom(customer, invoice);
  7. 7. customer.Name = GetNameFrom(customer, invoice); invoice.Date = GetDateFrom(customer, invoice);
  8. 8. var results = Initialize(customer, invoice); customer.Name = results.Item1; invoice.Date = results.Item2;
  9. 9. public tuple<string, DateTime> Initialize(customer, invoice) { return new Tuple<string, DateTime> (“asdf”, DateTime.Now); }
  10. 10. tuple •Avoid side effects •Avoid out parameters •multiple values without a specific type
  11. 11. null object
  12. 12. private ILogger _logger; public MyClass(ILogger logger) { _logger = logger; } … if (_logger != null) { _logger.Debug( “it worked on my machine!”); }
  13. 13. null checks for everyone!
  14. 14. forget one and…
  15. 15. public class NullLogger : ILogger { public void Debug(string text) { //do sweet nothing } }
  16. 16. private ILogger _logger = new NullLogger(); public MyClass(ILogger logger) { _logger = logger; } … _logger.Debug( “it worked on my machine!”);
  17. 17. null object •Can eliminate null checks •Simple to implement
  18. 18. Circuit Breaker
  19. 19. Retry
  20. 20. YourApplication OutofProcess Dependency N times
  21. 21. OutofProcess Dependency N times * Y clients
  22. 22. = Denial of Service Attack
  23. 23. Limit the # of retries
  24. 24. N * Y becomes 5 * Y
  25. 25. Y is still a problem
  26. 26. Circuit Breaker
  27. 27. State Machine On :: Off
  28. 28. On  Off when not healthy
  29. 29. Off  On manually
  30. 30. Get to software before we cut you
  31. 31. Healthy or Unhealthy OutofProcess Dependency
  32. 32. State is independent of requestor OutofProcess Dependency
  33. 33. YourApplication Can throttle itself
  34. 34. YourApplication Has many independent external dependencies
  35. 35. YourApplication Has a wait threshold
  36. 36. Your Application External Dependency Circuit Breaker Threshold = 2 Pause = 10ms Timeout = 30s State = Closed Request Request Failure (i.e. HTTP 500) Failure Count = 1 Pause 10ms Request Failure (i.e. HTTP 500) Failure Count = 2 State = Open OperationFailedException
  37. 37. Your Application External Dependency Circuit Breaker Threshold = 2 Pause = 10ms Timeout = 30s State = Open Request 30s has not passed CircuitBreakerOpenException Request 30s has not passed CircuitBreakerOpenException System can try to become healthy for 30s
  38. 38. Your Application External Dependency Circuit Breaker Threshold = 2 Pause = 10ms Timeout = 30s State = ½ Open Request Request Failure (i.e. HTTP 500) Failure Count = 2 State = Open OperationFailedException 30s has passed
  39. 39. Your Application External Dependency Circuit Breaker Threshold = 2 Pause = 10ms Timeout = 30s State = ½ Open Request Request Failure Count = 0 State = Closed Response 30s has passed Response
  40. 40. Closed Open ½ Open
  41. 41. ½ Open is like a manual reset
  42. 42. Pause Timeout
  43. 43. Pause between calls in the loop
  44. 44. Timeout before you can call again
  45. 45. Exceptions
  46. 46. OperationFailed : AggregateException
  47. 47. CircuitBreakerOpen : ApplicationException
  48. 48. Don’t Loose Exception Info
  49. 49. Always use InnerException(s)
  50. 50. Your Application External Dependency Circuit Breaker Threshold = 3 State = Closed Request Request Failure (i.e. HTTP 500) Failure Count = 1 Request Failure (i.e. HTTP 500) Failure Count = 2 Failure Count = 0 State = Closed Response Response Request ?
  51. 51. Segregate Dependencies
  52. 52. circuitBreaker(“database”) circuitBreaker(“weatherservice”)
  53. 53. Dependency type, endpoint svc, endpoint
  54. 54. Where?
  55. 55. YourApplication OutofProcess Dependency CircuitBreaker Proxy
  56. 56. Watch for Inception
  57. 57. YourApplication WebService CircuitBreaker Proxy Database Repository CircuitBreaker
  58. 58. circuit breaker •retry looping •slow down attempts •good neighbour

×