In 2009 I started a major re-write of one of our oldest VB6 apps. I started by writing a WCF service, which is in .NET 3.5, to pull data out of our SQL Server 2005 database and store it back there. I have a similar COM+ component I wrote in VB6 that transferred the data over the network as arrays. In 2009 I thought I would use ADO.NET DataSets instead.
Fast forward 3 years to now. As it so happened, although I started this project in 2009, other pressing projects and issues always pushed it to the back burner. Now, I'm trying to pick up where I left off. Our design is to pull the data out of a backend SQL Server database, and then store it locally in a SQL Express database.
The app, on the user's machine, will run against the local SQL Express database, and only transfer it to the backend server when the user is finished with it. They user may go off site to work, and be gone for quite a long time (months) before returning to upload the data. In this intervening time I'm trying to learn entity framework, to use against the local data store.
It's cool, the whole idea of using LINQ against the entity objects, etc. But, man, I'm struggling with a way to quickly code the transfer of data from the datasets brought back by my old WCF service, to local entity classes. I could delineate each column in each table from the returned datasets/datatables, but WOW, that's a lot of work!
Is there a faster way of doing this?
Try using Automapper for this. Automapper can map from an IDataReader
, so you can do someting like this:
Mapper.CreateMap<IDataReader, YourEntity>();
Mapper.Map<IDataReader, IList<YourEntity>>(dataTable.CreateDataReader());
If your entity fields match the DataTable columns, this should be all that you need to do, but Automapper does allow for more advance mapping configuration if you need it.