views:

447

answers:

1

Hi all,

I have an existing SQL Server database, where I store data from large specific log files (often 100 MB and more), one per database. After some analysis, the database is deleted again.

From the database, I have created both a Entity Framework Model and a DataSet Model via the Visual Studio designers. The DataSet is only for bulk importing data with SqlBulkCopy, after a quite complicated parsing process. All queries are then done using the Entity Framework Model, whose CreateQuery Method is exposed via an interface like this

    public IQueryable<TTarget> GetResults<TTarget>() where TTarget : EntityObject, new()
    {
        return this.Context.CreateQuery<TTarget>(typeof(TTarget).Name);
    }

Now, sometimes my files are very small and in such a case I would like to omit the import into the database, but just have a an in-memory representation of the data, accessible as Entities. The idea is to create the DataSet, but instead of bulk importing, to directly transfer it into an ObjectContext which is accessible via the interface.

Does this make sense?

Now here's what I have done for this conversion so far: I traverse all tables in the DataSet, convert the single rows into entities of the corresponding type and add them to instantiated object of my typed Entity context class, like so

            MyEntities context = new MyEntities(); //create new in-memory context
            ///....
            //get the item in the navigations table
            MyDataSet.NavigationResultRow dataRow = ds.NavigationResult.First(); //here, a foreach would be necessary in a true-world scenario
            NavigationResult entity = new NavigationResult
            {
                Direction = dataRow.Direction,
                ///...
                NavigationResultID = dataRow.NavigationResultID
            }; //convert to entities

            context.AddToNavigationResult(entity); //add to entities
            ///....

A very tedious work, as I would need to create a converter for each of my entity type and iterate over each table in the DataSet I have. Beware, if I ever change my database model....

Also, I have found out, that I can only instantiate MyEntities, if I provide a valid connection string to a SQL Server database. Since I do not want to actually write to my fully fledged database each time, this hinders my intentions. I intend to have only some in-memory proxy database.

Can I do simpler? Is there some automated way of doing such a conversion, like generating an ObjectContext out of a DataSet object?

P.S: I have seen a few questions about unit testing that seem somewhat related, but not quite exact.

+1  A: 

There are tools that map between objects, such as automapper. This is a very good open source tool.

However, these tools sometimes have problems, for example generating duplicate entity keys, or problems when the structure of the objects being mapped are very different.

If you are trying to automate it, I think that there is a greater chance of it working if you use EF 4 and POCO objects.

If you end up writing the mapping code manually, I would move it into a seperate procedure with automated unit tests on it.

The way we do this is to create a static class with "Map" methods":

  • From DTO to EF object
  • From EF to DTO

Then write a test for each method in which we check that the fields were mapped correctly.

Shiraz Bhaiji
Automapper looks great. EF4 is also great, but since this project is already very mature, and based on 3.5 by requirement, this is currently not an option.What do you mean by "separate procedure"?. Do you mean "assembly"? Also do I consider unit tests very handy, but what exactly should I test? Whether the mapping is complete? Or whether the models changed?However, at last, I want to thank you for your answer so far!
Marcel
@Marcel, I have updated my answer
Shiraz Bhaiji
Thanks Shiraz, for answering that question. Since no one else replied, and the Automapper you mention is a good solution for this kind of problem, you receive the bounty of course.
Marcel