Why are most code bases bad? Why is it, that, despite our best intentions, code rots between our fingers? The answer is that most of us still think of code as merely a technical tool to reach a goal: implementing a feature, fixing a bug. While it certainly is that, it’s also a written medium for communicating with (other) people.
36. @ploeh
public class MethodInvoker : ISpecimenBuilder
{
private readonly IMethodQuery query;
public MethodInvoker(IMethodQuery query)
{
if (query == null)
{
throw new ArgumentNullException("query");
}
this.query = query;
}
public IMethodQuery Query
{
get { return this.query; }
}
public object Create(object request, ISpecimenContext context)
{
if (context == null)
{
throw new ArgumentNullException("context");
}
foreach (var ci in this.GetConstructors(request))
{
var paramValues = (from pi in ci.Parameters
select context.Resolve(pi)).ToList();
if (paramValues.All(MethodInvoker.IsValueValid))
{
return ci.Invoke(paramValues.ToArray());
}
}
return new NoSpecimen(request);
}
private IEnumerable<IMethod> GetConstructors(object request)
{
var requestedType = request as Type;
if (requestedType == null)
{
return Enumerable.Empty<IMethod>();
}
return this.query.SelectMethods(requestedType);
}
private static bool IsValueValid(object value)
{
return !(value is NoSpecimen)
&& !(value is OmitSpecimen);
}
}
37. @ploeh
public class MethodInvoker : ISpecimenBuilder
{
private readonly IMethodQuery query;
public MethodInvoker(IMethodQuery query)
{
if (query == null)
{
throw new ArgumentNullException("query");
}
this.query = query;
}
public IMethodQuery Query
{
get { return this.query; }
}
public object Create(object request, ISpecimenContext context)
{
if (context == null)
{
throw new ArgumentNullException("context");
}
foreach (var ci in this.GetConstructors(request))
{
var paramValues = (from pi in ci.Parameters
select context.Resolve(pi)).ToList();
if (paramValues.All(MethodInvoker.IsValueValid))
{
return ci.Invoke(paramValues.ToArray());
}
}
return new NoSpecimen(request);
}
private IEnumerable<IMethod> GetConstructors(object request)
{
var requestedType = request as Type;
if (requestedType == null)
{
return Enumerable.Empty<IMethod>();
}
return this.query.SelectMethods(requestedType);
}
private static bool IsValueValid(object value)
{
return !(value is NoSpecimen)
&& !(value is OmitSpecimen);
}
} @ploeh
Encapsulation
38. @ploeh
“An abstraction is
the amplification
of the essential
and the
elimination of the
irrelevant.”
https://commons.wikimedia.org/wiki/
File:Robert_Cecil_Martin.png @ploeh
54. @ploeh
change the state of the system
private static void Adjust(Customer customer)
{
if (customer.TotalPurchases >= 10000)
customer.Tier = Tier.Gold;
// Many more similar checks go here...
}
Commands
59. @ploehReused Abstractions Principle
Command Query Separation
Liskov Substitution Principle
Interface Segregation Principle
Dependency Inversion Principle
Separation of Concerns
Single Responsibility PrincipleCohesion
You Aren’t Going to Need It
Open Closed Principle Law of Demeter
Principle of Least Surprise
Don’t Repeat YourselfHollywood Principle