Simple reusable DTO factory methods

I have just found a little bug in my app that to me was an issue with code duplication. It was a DTO not getting properly hydrated when is was getting translated from a domain object in the service layer. My DTOs are just objects with auto properties, no business logic methods; just data carriers. I sometimes have the need for a basic DTO with just simple info (ie for Lists) and a more detailed DTO with the objects child collections (as DTOs collections) for more detailed views. The problem I had was when I added a field on to the domain object then had to modify my factory (and test) to ensure the new field was mapped. What I forgot to do was to also do it for my detailed DTO. I wtote the test and realised that I was doing the exact same work in 2 places, which was one of the reason behind the bug. As i prefer not to use anything other than a default constructor for DTOs i was in a bit of a quandry. Other than using JBogard’s AutoMapper i was not sure how to tackle this.
My goodness, i may actually have to engage my brain!
Well the result was incredibly simple.
The detailed DTO (xyzDetailedDto) inherited from the normal DTO (xyzDto) so i just created a private generic factory method in the publicly exposed Extension method class

private static T Create(xyzDomianObject domainObj) where T : xyzDto, new()
{
return new T
{
Id = domainObj.Id,
Name = domainObj.Name,
Details = domainObj.Details,
OtherThing = domainObj.OtherThing
};
}

As xyzDetailedDto inherited from xyzDto I could reuse the creation method and get back the correct type.
This all seems very simple and silly now, but this has drastically cleaned up my translation layer πŸ™‚

Better Know a Framework!

In the vein of the DNR guys:

ImmutableAttribute to continuously check that a class or a structure is immutable.

PureAttribute to continuously check that a method is pure, i.e it doesn’t provoke any side effect by modifying some fields states.

default C# Keyword in Generic Code. Will return null for reference types and zero for numeric value types

These are things that i haven’t used in the past when i should have, but not any more! πŸ™‚

How Did You Get Started in Software Development?

I met a bunch of nice guys at Alt.net Seattle, two of whom Ii got on with really well, Tim and Chris. I think we were kinda of in the same boat, a little overwhelmed by the uber geeks that we were associating with, while still been stoked that we were there and contributing.

Tim Barcz tagged me this week asking how I got started with programming. So here it goes.

How old were you when you started programming?

I first started playing with computers to squeeze memory out of our poky little of a computer so we could play games.. trivial CONFIG.SYS or AUTOEXEC.BAT edits really plus some very simple computer classes at high school. My first real bash at coding was in 1998 in Java 101 at The University of Auckland followed by and Advanced Diploma in Business Systems (Java)

What was your first programming language?

Besides HTML and batch files? Java I suppose (that shows my age or lack of).

What was the first real program you wrote?

My first commercial piece of software was a warranty system for an eCommerce software company. Thrown straight in the deep end using .net 1.1 and vb.net a framework and language I had never used before. prior to that i had attempted to help my brother build my personal training web site (In a past life i was a Sports Scientist/Strength coach, how things have changed!)

If you knew then what you know know would you have started programming?

To be honest, i probably would have never done my Sport Science degree if i knew that i would love programming so much. so to be honest its not all bad because i learnt a lot in the SptSci world that i have related to my work ethic in computing.

If there is one thing you learned along the way that you would tell new developers, what would it be?

Like anything, if you enjoy it you will be more likely to excel in it. Also don’t get caught up in language or API specifics. Good fundamentals will get you much further than a deep (but narrow) API knowledge.

What’s the most fun you’ve ever had … programming?

Being able to travel the world with out worrying about how i will make a buck. Auckland, Perth and London so far.. not bad for under 5 years in the industry.
Also being able to work in an ACDC T-Shirt and jandals is actually pretty cool.

I’d like to hear from:

Stu, Todd, Jerome and CDS… but none of them have blogs because they are dorks :p
so I’ll call out my Bro (who blogs as often as i go to the opera):
Lee “Gumble” Campbell
and a couple of smart buggers that i used to work with in Perth
Bill Poole
and Chris Nurse