Tidbits on software development, technology, and other geeky stuff

AutoMapper With DataTables

When working with data in .NET, I almost always prefer to work with POCO domain objects rather than some type of heavy abstraction layer.  Advantages are many including custom validation, encapsulated business logic and full control and flexibility of what is going on with the entity.  And, using LINQ queries against simple objects is dead easy.

Sometimes I come across an existing system that is using DataTables and DataSets exclusively for handling data and I don’t really have time to convert the data access layer over to using  domain objects.  Fortunately I came across a way to use the nice AutoMapper mapping library to convert a DataTable into a POCO entity very easily.  You just need to:

There you have it.  Now you can go encapsulate some domain specific logic / rules into your domain object (i.e. read-only properties that have domain logic, etc.), run LINQ queries to filter the list, and anything else your heart desires.  It really gives you a lot more power than sticking with a DataTable.

Discuss on Twitter