Archive for .NET tag
Dictionary<T> look-up or create made simpler
The design of a Dictionary<T> lends itself well to a caching or identification mechanism and as a result you often see code that looks like this:
private static Dictionary<string,Employee> employees;
…
public static Employee GetByName(string name) {
Employee employee;
if (!employees.TryGetValue(name, out employee)) {
employee = new Employee(whatever);
employees.Add(name, employee);
}
return employee;
}
It’s not that it is particularly difficult but it can be a bit error prone and when you’re doing it over and over. What would be nicer is something that let you do:
public static Employee GetByName(string name) {
return employees.GetOrAdd(name, () => new Employee(whatever));
}
Here’s an extension method to drop-in to a static class of your choosing that achieves just that.
public static TActualValue GetOrAdd<TKey, TDictionaryValue, TActualValue>(this IDictionary<TKey, TDictionaryValue> dictionary, TKey key, Func<TActualValue> newValue) where TActualValue : TDictionaryValue
{
TDictionaryValue value;
if (!dictionary.TryGetValue(key, out value)) {
value = newValue.Invoke();
dictionary.Add(key, value);
}
return (TActualValue)value;
}
[)amien
Changing type, the state pattern and LINQ to SQL
A question I see from time-to-time on LINQ to SQL relates to changing an entity’s class.
C# and VB.NET don’t allow a class to change its type at runtime and LINQ to SQL specifically doesn’t provide a mechanism for changing the underlying discriminator for this reason.
Discarding the current object and creating a new one is fraught with issues. What do we do about existing references, unsaved data, established associations and caches?
Start with an example
Consider an abstract Account class with SavingsAccount and CurrentAccount sub-classes. Bank accounts don’t change type once created (in my experience) so that’s good so far.
When we get into processing and validation logic its tempting to create ClosedAccount and OpenAccount classes but what happens during execution when closing an account?
A further consideration is how exactly ClosedAccount and OpenAccount fit into the hierarchy given the single-inheritance limitation of C# and VB.NET.
Enter the State Pattern
The ever-resourceful Gang of Four describe the State Pattern as:
Allow an object to alter its behavior when its internal state changes.
The object will appear to change its class.
Taking the bank accounts example and applying the state pattern gives:

Account no longer needs to change type at run-time yet is still able to have clients call Validate and process Methods that end up in discrete methods as if inheritance had been used.
To achieve this we have:
- Created a State hierarchy that contains the logic we need to change at run-time
- Introduced a private member in Account that points to the current state instance
- Ensured the Account methods call the current state class via the private member
Because the state member is private and only accessed by Account itself we can happily create and dispose it as the conditions that affect the state change as much as we like without worrying about references to it.
This is best illustrated with code. Here rather than just calling the state’s validation logic there is a combination of core Account validation (balance), state validation (closed) and CheckingAccount validation (transaction limits):
public abstract class Account {
private AccountState state;
public virtual Status Validate(ITransaction tx) {
Status result = state.Validate(tx);
if (tx.Amount > Balance)
result.Add(TransactionFailures.InsufficientFunds);
return result;
}
}
public class SavingsAccount : Account {
public override Status Validate(ITransaction tx) {
Status result = base.Validate(tx);
if (Transactions.Count > TransactionLimit)
result.Add(TransactionFailures.TransactionLimitReached);
return result;
}
}
public class ClosedAccountState : AccountState {
public override Status Validate(ITransaction tx) {
return new Status(TransactionFailures.InvalidSourceAccount);
}
}
This is less complex than selectively replacing objects within our application at run-time and can bring additional benefits:
- Single responsibility principle – logic relating to a specific state has its own class to live in
- Favor composition over inheritance – inheritance for OpenSavingsAccount, ClosedCheckingAccount etc. is complex
Like all guidance, patterns and principles do not blindly follow these guidelines or patterns but consider how it affects and fits with your application. For this particular example it not only solves the problem but helps maintainability – at least at this simple stage. Once Validation becomes sufficiently complex it would likely move out entirely into a new set of orchestrated classes just for that.
With LINQ to SQL (and other mappers)
Moving this example into an object-relational mapper requires two – not unexpected – database-mapped properties.
- The inheritance discriminator (Type)
- A state indicator (Active)

The only thing we need to ensure is the Account’s state member always refers to either a ClosedAccountState or OpenedAccountState depending upon the Active flag.
Given that LINQ to SQL code-generates the property for Active we could:
- Make Active private, wrap it in another property and set the state member when it changes and at initialization
- Make the state member a read-only property instead of an instance variable
The second works well here and given that AccountState is itself stateless (perhaps not the best choice of name) we can use a singleton to avoid multiple instances. The state instance variable in the Account class is replaced with:
private AccountState State {
get {
if (Active)
return OpenAccountState.Instance;
else
return ClosedAccountState.Instance;
}
}
The code continues to work and now changing the Active flag results in different behavior.
Best of all we still have the code in separate classes, no switch/case/if statements relating to validation or account types, a clean inheritance hierarchy and no running around trying to invalidate existing references.
Hitting the discriminator directly
There may be times when claims are made that a type has to change – perhaps data was entered incorrectly.
Before delving into the database or providing a tool to flip the underlying discriminator value consider:
- Does the new class interpret the same data in a different manner?
Has a $1,000 credit limit just become a 1,000 transactions per-month limit? - Would the object be valid in the new class?
Did a ProposedCustomer just become ApprovedCustomer without a policy-enforced credit check? - Are associations still honored?
Are 300 unshipped orders for a GameProduct still honored for a BookProduct?
If in doubt don’t do it.
An inconsistent database bleeding through your application isn’t good for anyone and will take a lot longer to sort out than setting up a new entity.
[)amien
Web site vs web application in Visual Studio
Rob Conery got me thinking about web site maintenance and I put forward a brief comment on the two distinct types and how Visual Studio handles them which I have expanded upon here.
Web site
Primarily for working with ad-hoc web sites that have programmed elements. Easily identified by customer-specific content present in aspx files.
No solution or project files are required and the pages and source can reside locally (file system, IIS) or remotely (FTP, WebDev/FrontPage extensions) via the File > Open > Web Site… menu option.
Code-behind and classes are typically stored on the web server which compiles them in-memory on demand. Changes can be made to the files without restarting the application and losing sessions.
| For | Against |
|---|---|
| Quick edit, test, deploy cycle | Syntax errors at runtime |
| No need to compile or restart app | Can’t create an installer |
| Source always available | Source on server useful to hackers |
Web application
Web application projects were introduced as an add-on for Visual Studio 2005, later rolled in to VS 2005 SP1 and made a full first-class citizen with Visual Studio 2008.
Like the name implies these are primarily for web applications, those times when you have written a product or solution that happens to have a web interface.
Web application projects exist on your local drive and are treated like any other VS project type and can be added to existing solutions are subject to full compilation, validation and build steps.
Deployment is typically via MSI installers however you can also utilise the addition Web Deployment Projects add-in which allows you to deployment directly to servers which is useful for deploying to test environments.
| For | Against |
|---|---|
| Controlled build & deploy process | Deployment causes application restart |
| No class files on web server, dll only | Can’t deploy individual classes |
| Syntax errors at compile time |
Hybrid
Sander and I were discussing this article and thought an interesting solution might be to use the Web Application model for local development but to use the Publish option to publish all solution files to an intermediate directory.
Then in the intermediate directory just remove the bin/applicationname.dll file and copy to the target. This should prevent an application restart unless the web.config or global.asax/global.asax.vb files have been modified.
[)amien
The pragmatic .NET developer
Long-time friend, fellow co-host of the GSDF and the coding genius behind the open-source Ogre3D engine Steve Streeting has written an interesting piece on Open source adoption; countering the fear and doubt. I have no doubt that this was fuelled by a lengthy discussion last night in the Ship & Crown pub – a common ritual after our GSDF meetings.
The reasons why I adopted .NET as my primary platform despite being tied to a single-supplier are:
- Ease of deployment & set-up
- Low resistance to adoption
- Great tool integration
- Official & community support
- Love for C# and the CLR
The ALT.NET movement
Many .NET developers are reluctant to look wider afield but this is not exclusively the case and a person focused on the .NET platform but open to selecting beneficial alternatives to the Microsoft prescription is exactly what the ALT.NET moniker was coined to encapsulate.
SourceForge lists over 6,000 open-source C# projects alone and many well-known open source Java & PHP projects have made their way to the .NET platform. NHibernate, NUnit, NCover, Spring.NET & DotNetNuke alongside new .NET developments such as xUnit.NET, SubSonic, Subtext etc.
Best of breed
Where a non-Microsoft option is functionally superior or more cost effective I will consider it whether it is proprietary or open source.
I do not however select a solution simply because it is considered the “best of breed” at that particular moment. Integration, training, availability of support and experienced developers, deployment, cost, barriers to entry and roadmaps must be taken into account.
Given that Microsoft provide the .NET platform anything ‘in-the-box’ scores highly in many of these areas.
Sometimes a non-Microsoft solution comes out on top or there is a compelling reason to adopt it anyway. This is why my toolkit already contains Subversion, TortoiseSVN, AnkhSVN, Reflector and NUnit.
It’s also why I currently run Mac OS X alongside Vista, use WordPress as my blogging platform, prepare my presentations with KeyNote and use OmniFocus to organise my life.
Robust alternative projects
I have concerns about longevity and support on projects from companies and hobbyists regardless of whether they are open source or proprietary.
NDoc, CVS & NullableTypes are three I’ve used which died when an alternative commercial or open source project gained more momentum and SourceForge is seemingly littered with thousands of dead projects.
If a project you rely upon dies you have one of a number of options:
- Migrate to something new (gained little from open source)
- Fix bugs and problems yourself (time spent working outside your business domain)
- Have a support contract with somebody else to work on it (single-supplier scenario?)
Competition is important
Competition is important but I can not, in a professional capacity, recommend to customers something that I believe is less suitable in the interest of keeping the competition healthy.
Confusion about choice
I hit this one first hand developing my final-year degree project which required development of a web site in Java.
The number of choices for Java was incredibly confusing despite knowing the syntax. J2SE or J2EE? JSP, Struts, Spring or another servlet package? What about the database and ORM? Application server? What versions work together? What overlaps? Would I be able to get experienced developers? If not how long to train them up?
.NET has many options too but I can start with the .NET Framework and get right into solving the domain problem. If the going gets tough I may have taken a wrong turn and need a different solution. That could involve choosing an alternative component or framework but now I’ll know what problem I’m trying to solve when I go looking.
Developers on complex projects felt that WebForms wasn’t ideal – it is hard to maintain, the output bloated with leaky implementation (ViewState) but it serves many developers well enough.
Open-source projects such as MonoRail addressed this taking cues from Ruby on Rails. Microsoft acknowledge this and add a similar MVC framework going so far as to support additional engines and components allowing elements of MonoRail to be used. Those guys could drop the glue required to get their engines into the pipeline and just concentrate on engines if they wish.
What works for me
Stay small and focused until you feel friction.
Friction isn’t always technical or immediately obvious. It might be future plans and it’s often people. It might be what isn’t there and will never be.
Time lost on friction is not spent developing your domains features.
If another solution causes less friction, use it but don’t underestimate unknowns.
I guess that’s just being pragmatic.
[)amien
Language Integrated Query: An introduction talk tomorrow
I’m just finishing up the slides, notes and writing code samples for my LINQ presentation at the Guernsey Software Developer Forum tomorrow evening.
Hopefully the broader scope of this presentation will mean a few new faces – the previous talks on Subversion and web application security might have been a little specific for a such a small audience (Guernsey is around 70,000 people on an island 9 miles long).
As always I will put the presentation up here afterwards but will also revise the older ones with the new style and put up both PowerPoint and Keynote versions under an open licence as well as a couple of cheat sheets.
Is there any interest in adding audio narration and transcripts?
This presentation is now available on-line.
[)amien