Wednesday, 27 July 2011

Software Development Stuff, July 2011

VS2010 Integrated SVN

I have recently discovered the AnkhSVN Visual Studio add-in for linking Visual Studio 2010 to your SVN repositories - it's fantastic:

Full Visual Studio 2010 integration - gives you those nice neat “checks” and other icons in the solution explorer, to indicate modifications, new items, etc. It's just like a bought one!

Data Access, Entity Framework 4.1, POCO

Have also recently discovered Entity Framework version 4.1 for awesome POCO (i.e. "code first") support - doesn't quite "just work" off the bat, but it's pretty damn close:

Couple of hints - initially, I kept on getting the following error message:

System.Data.SqlClient.SqlException: Invalid object name 'dbo.Users'.

...obviously I've got a table called [User]. There seems to be some issue (probably not an issue, but just my not reading enough about the framework) with pluralisation. Anyway, the solution is easy – decorate the EF POCO class with the TableAttribute:

 namespace Infostructure.SimpleList.DataModel.Models
[Table("User", Schema = "dbo")]
public class User
{ ...

JSON Serialisation with EF and AutoMapper

In the piece of work I'm doing a the moment (an MVC app that manages a bunch of “simple lists”) I want to be able to serialise to JSON where an incoming GET request is not ASP.NET authenticated and a UID and PW has come through on the query string.

Ran into trouble immediately with serialising my EF POCO objects to JSON, as they have circular references. So, I'm translating to ViewModels. Basically if I don't translate to my matching ViewModels, then I get ye olde "A circular reference was detected while serializing an object of type" exception.

Enter the fantastical AutoMapper library.

I use AutoMapper to handle the mapping from EF POCO objects to my ViewModels. The Mapper needs to be configured in the Global.asax ideally, or alternatively, just anywhere prior to where you intend to do the mapping. Like so:

 AutoMapper.Mapper.CreateMap<DataModel.Models.SimpleList, Models.SimpleListViewModel>();  

Here's some code from an Index method in one of my Controllers:

     public ActionResult Index()
string userName = Request.QueryString["userName"];
string password = Request.QueryString["password"];
if (User.Identity.IsAuthenticated)
_simpleListRepository = new SimpleListRepository();
var simpleLists = _simpleListRepository.GetSimpleLists(User.Identity.Name);
return View("Index", simpleLists);
else if (userName != null && password != null)
_simpleListRepository = new SimpleListRepository();
var simpleLists = _simpleListRepository.GetSimpleLists(userName, password);
var simpleListsDto = AutoMapper.Mapper.Map<IEnumerable<DataModel.Models.SimpleList>, IEnumerable<Models.SimpleListViewModel>>(simpleLists);
return Json(simpleLists, JsonRequestBehavior.AllowGet);
return View("Index");

Here's the link to AutoMapper:

That's all for now.

The code samples I've provided, etc, are a bit rough-and-ready as usual. However, should you happen to have stumbled on this post, and end up following some of it's recommendations, then I hope you enjoy using these features/tools as much as I have!

======= EDIT - 2011-08-01 =======

Unfortuantely AutoMapper uses Reflection.Emit, which is not allowed in a Medium Trust environment (i.e. my shared hosting web host).

There is an alternative out there called ValueInjector, which is not quite as complete-a-tool as AutoMapper as far as I can tell and has a bit of a steeper learning curve. Apparently it can be used in a Medium Trust environment:

I've reverted to a custom mapping implementation for the time being, since what I'm doing right now is not DTO-intensive. AutoMapper is great though and ValueInjector looks interesting.

======= EDIT - 2011-08-01 =======

Tuesday, 12 July 2011

New Zealand Innovation

So, some irony - two articles forwarded to me yesterday, both regarding software innovation in NZ, both at opposite ends of the optimism scale:

(1): "Kiwi Software Skills 'Shallow'" (thanks Ann)

Dr McKendry is a New Zealander who has built a successful career overseas and has basically come home and done a talk dissing the nation that launched him. 

Original link:

(2): "Your Business: Timing Right for Software Success" (thanks Dad)

This is my uncle, talking about the successful SME that he has built over the past 15-to-20 years and his plans for it's future. 

Original link:

My $0.02...

Perhaps the overqualified expat CRM corporate executives of the world (seriously Dr McKendry, please don't tell me you think that Siebel is an innovate product?) should remember where they came from.

NZ'ers split the atom, climbed Mt Everest and invented the electric fence; what's wrong with producing actual solutions to actual business problems in between?

Go uncle Ed!

Go back to Silicon Valley Dr McKendry...

PS: I'm fairly confident that my response is exactly the sort that Dr McKendry was hoping to invoke with his talk!


EDIT - 20131204: 

Since I wrote this post - just about two years ago today - my thinking has changed (perhaps due to the business studies that I have taken over the past two years) and I do now sympathise with Dr McKendry's perspective more.

There is room for improvement in NZ business. The cost of complacency is high and growing.

Migrating (and Open-Sourcing) an Historical Codebase: SVN-to-Git

I have a SVN repo on my local machine that I have been shoving stuff into since before I knew how to use revision control systems properly (...