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 worked on my machine!”);
}
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 worked on my machine!”);
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
Request
Failure (i.e. HTTP 500)
Failure Count = 1
Pause 10ms
Request
Failure (i.e. HTTP 500)
Failure Count = 2
State = Open
OperationFailedException
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
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
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
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)
Failure Count = 1
Request
Failure (i.e. HTTP 500)
Failure Count = 2
Failure Count = 0
State = Closed
Response
Response
Request
?
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