This is one of the more simple patterns and is really useful. It is ideal for encapsulating rules and simplifying those if-else or switch statements.
If you have some code that looks at an object and makes a decision based on a property of the object, then you may need to consider the Strategy. You should also consider this if you think that you may, at some point, get asked to add another clause into that switch statement. Maybe the code calculates the exchange rate for US$, and Euro. But what happens when the Business decides they want the British £ also.
For that you may need to add an else clause or extend the Switch.
That's not good really as it breaks the Open-Closed principle. I have a post on this which contains a code example.
This diagram gives a general idea of the pattern.
As an example we could have an object that needs to calculate the exchange rate. So, at construction time we could inject the required calculator and the object calls the relevant method. If we want to add another currency calculator, say UKPound, then we create the new class UKPoundCalculator, it implements the interface IExchangeRateStrategy, and we pass it to the contact class. The beauty is, there are no changes made to the CalculateTheCost context class.
See:
Open-Closed Principle
Thursday, March 15, 2012
Running NUnit from within Visual Studio
It looks like NUnit will run from within Visual Studio now.
http://www.nunit.org/index.php?p=vsTestAdapter&r=2.6
The current release version is 0.9 and only runs in Visual Studio 11, which is in preview release at the moment. That's good news for NUnit as new developers to Visual Studio will probably use NUnit over MSTest now the integration and not needing to use a separate application hurdle has gone.
http://www.nunit.org/index.php?p=vsTestAdapter&r=2.6
The current release version is 0.9 and only runs in Visual Studio 11, which is in preview release at the moment. That's good news for NUnit as new developers to Visual Studio will probably use NUnit over MSTest now the integration and not needing to use a separate application hurdle has gone.
Friday, March 2, 2012
Strategic Design - Responsibility Traps lecture by Eric Evans
This is an Excellent talk by Eric.
Full of common sense and good observation.
The talk is essentially about using the Anti-corruption layer as a way to concentrate on the Core Domain.
Anti-corruption layer? A way of embracing the reality that often times it's better to leave the legacy system, the ball of mud, and concentrate on the new core features.
The example of what can be core domain and sub domain is excellent and simple.
Eric chose the rating feature used by Amazon and by Ebay.
In Amazon the star ratings are very handy but their business model does not depend on the feature. It is unlikely that the visitor to the site will purchase based on this feature. Maybe sometimes, but not significantly.
However in Ebay, the star rating is absolutely crucial in developing the trust between the seller and buyer. This therefore is key and is core domain.
Summary
Eric discusses the need for strategic thinking and how early design decisions have major impact on the organization and the entire development process. He uses the lens of DDD Strategic Design principles (emphasizing "Context Mapping" and "Distilling the Core Domain") to show how to avoid strategic failures and achieve strategic successes. Winning strategy starts with the domain
http://www.infoq.com/presentations/design-strategic-eric-evans
Eric discusses the need for strategic thinking and how early design decisions have major impact on the organization and the entire development process. He uses the lens of DDD Strategic Design principles (emphasizing "Context Mapping" and "Distilling the Core Domain") to show how to avoid strategic failures and achieve strategic successes. Winning strategy starts with the domain
http://www.infoq.com/presentations/design-strategic-eric-evans
Work in the Core Domain - Part 4 in the book!
Wednesday, February 29, 2012
Getting started with DDD when surrounded by Legacy systems, by Eric Evans
Getting started with DDD when surrounded by Legacy systems
STRATEGY 1: BUBBLE CONTEXT(Part 1 of a series by Eric Evans)
- Why we need the bubble
- How we form the bubble
- What becomes of the bubble
We say that effectively applying the tactical techniques of domain-driven design (DDD) requires a clean, bounded context (See September 2010 newsletter). This can be a daunting requirement when your work is dominated by legacy systems. These systems are often tangled, and even when they are orderly they are usually not suited to DDD. In a series of articles over the next few months, I'll describe strategies for getting started with DDD when you have a big commitment to legacy systems.
All of these strategies will be ways of establishing a new bounded context. Attempts to employ the DDD tactics in the context of a legacy system almost always disappoint. One of the fundamentals of DDD is that we choose a model (by which we mean a system of abstractions) well suited to the problem at hand, yet a legacy system already has an established model, albeit implicit, and this model can seldom be changed with a reasonable amount of effort. Even if the legacy model could be changed, the new model might not suit the legacy functionality -- the change could undermine what the legacy system was always good at. On the other hand, simply adding objects that express a distinct model without changing the ones already there will lead to conflicting rules and concepts.
Will DDD work for your team? by Eric Evans
Will DDD work for your team?
Four prerequisites for Domain-driven Design
by Eric Evans
While the Strategic Design techniques of DDD can be applied in many situations, development based on subtle domain models has some demanding prerequisites. If you don't have them, it is a waste of effort to leap into application of DDD at the tactical level.
Instead of grinding your gears trying to express elegant models, you'd do better to direct your efforts toward establishing these conditions for success.
This is real life, and nothing is perfect. However, you really must have some form of the following,
- Access to domain experts
- An iterative process
- A clean, bounded context
- Skills on the team
Wednesday, February 22, 2012
SMTP4DEV
This is a great little tool that will run on your desktop and listen to port 25. Ideal if you are writing code that sends email. That said though. If you have a dependency on smtp and it is preventing you in Test Driven Development then you should probably think about refactoring with dependency injection.
Monday, February 20, 2012
How to use the Yield statement in C#
The Yield is a very interesting statement. You use it with the Return statement to build a collection.
// Step through the list until we get to the '10'
void DoSomethingToTheNumbers()
{
foreach (int num in MakeListofNumbers())
{
if (num == 10) return;
}
}
// makes an IEnumerable list of '3', '4'.. 19
IEnumerable MakeListofNumbers()
{
for(int i = 3; i < 20; i++)
{
yield return i;
}
}
In the method MakeListofNumbers() an IEnumerable of integer values is created. This is built up in a "lazy" manner.
So in the DoSomethingToTheNumbers() method, we can enumerate the MakeListofNumbers() by using a foreach statement. That is we can step through each element in the IEnumerable list.
To see how neat this is it is best to step through it using the debugger. As you step through the foreach statement, the code will magically jump down into the MakeListofNumbers() function and generate the next number in the list.
(To Computer Science nerds this is called a "Continuation")
Why is it called Yield? I think it's because the method yields control to the calling function.
Is it useful?
Well yes but not often. This is really handy if the enumeration generation method is doing something rather computationally expensive. If it is, and if you want to cease the foreach partway through, that is, you want to foreach only the first n elements in the IEnumerable, then you can and you will not have wasted time creating the entire IEnumerable list.
// Step through the list until we get to the '10'
void DoSomethingToTheNumbers()
{
foreach (int num in MakeListofNumbers())
{
if (num == 10) return;
}
}
// makes an IEnumerable list of '3', '4'.. 19
IEnumerable MakeListofNumbers()
{
for(int i = 3; i < 20; i++)
{
yield return i;
}
}
In the method MakeListofNumbers() an IEnumerable of integer values is created. This is built up in a "lazy" manner.
So in the DoSomethingToTheNumbers() method, we can enumerate the MakeListofNumbers() by using a foreach statement. That is we can step through each element in the IEnumerable list.
To see how neat this is it is best to step through it using the debugger. As you step through the foreach statement, the code will magically jump down into the MakeListofNumbers() function and generate the next number in the list.
(To Computer Science nerds this is called a "Continuation")
Why is it called Yield? I think it's because the method yields control to the calling function.
Is it useful?
Well yes but not often. This is really handy if the enumeration generation method is doing something rather computationally expensive. If it is, and if you want to cease the foreach partway through, that is, you want to foreach only the first n elements in the IEnumerable, then you can and you will not have wasted time creating the entire IEnumerable list.
Thursday, February 16, 2012
The Planning Fallacy
Do we all experience the Fallacy on our work:
- Overoptimistic in the time planning therefore underestimating time taken.
- Overestimating the benefits
It is a very real and measurable effect. Using a number of different subject areas from origami to tax returns. The tendency is to plan on the light side. Interestingly this only happens when we estimate our own work. When people uninvolved in the work are asked for estimates, they tend to over estimate.
No one really knows why, but vocalism is thought to be a candidate.
One curious observation is that people still tend to underestimate the task even if they have evidence from previous similar tasks.
Wednesday, February 15, 2012
how to stop Windows rebooting after an auto update
I'm a snob and I am unbearable company because I'm a fully paid up member of the Mac brigade. Every day there is at least one moment of pleasurable reflection when I appreciate something on one of my Mac devices where I'm glad they paid a great deal of attention to the detail.
Windows on the other hand...
No actually I'm not dead against Microsoft or Windows as it's definitely horses for courses and no giant corporation can be right all the time. But I do have my pet hates. Here is one.
You leave your windows machine for a coffee break. The Visual Studio windows are arranged. Source files open at specific points as you struggle your way through more diagnostics.
You come back and Windows has rebooted!
Who in the name of ... thought that would be a neat thing to do.
Yes I'm sure from an OS perspective, a Microsoft perspective, from a Paternalistic systems perspective they're doing it for our own good.
I hear that Windows 8 will require fewer reboots. That makes me smile.
So, how to disable this patronising annoyance:
Step 1
Go to the Start button, or whatever it's called these days, and in the search box type
Step 2
In the tree view, navigate to:
Step 3
In the right hand pane:
Double-click on:
Step 4
Windows on the other hand...
No actually I'm not dead against Microsoft or Windows as it's definitely horses for courses and no giant corporation can be right all the time. But I do have my pet hates. Here is one.
You leave your windows machine for a coffee break. The Visual Studio windows are arranged. Source files open at specific points as you struggle your way through more diagnostics.
You come back and Windows has rebooted!
Who in the name of ... thought that would be a neat thing to do.
Yes I'm sure from an OS perspective, a Microsoft perspective, from a Paternalistic systems perspective they're doing it for our own good.
I hear that Windows 8 will require fewer reboots. That makes me smile.
So, how to disable this patronising annoyance:
Step 1
Go to the Start button, or whatever it's called these days, and in the search box type
gpedit.mscthis opens the Group Policy editor.
Step 2
In the tree view, navigate to:
Computer Configuration -> Administrative Templates -> Windows Components -> Windows Updates
Step 3
In the right hand pane:
Double-click on:
"No auto-restart with logged on users for scheduled automatic updates installations"
Step 4
Set this to Enabled.
Click OK to go on your merry way.
Wednesday, February 8, 2012
Papua New Guinea Fairtrade coffee beans

| Mrs Non-Gorilla | Morning Mrs Gorilla. |
| Mrs Gorilla | Morning Mrs Non-Gorilla. |
| Mrs Non-Gorilla | Have you been shopping? |
| Mrs Gorilla | No...been shopping. |
| Mrs Non-Gorilla | Did you buy anything? |
| Mrs Gorilla | A piston engine. |
| She reveals a six-cylinder car engine on a white tray, on a trolley. | |
| Mrs Non-Gorilla | What d'you buy that for?? |
| Mrs Gorilla | Oooh! It was a bargain. |
Subscribe to:
Posts (Atom)


