Reputation: 5994
I have to create an import from excelfiles to a database. I've created a little system which mapps my excelfile to some kind of "excel entities". Some of these entites contain information for more than one database table and for other entites I need to calculate the id in the database etc. I am using the entityframework to map the database to objects.
Now my problem is, that I need a clean approach to map between my excelfile structure and my database structure.
Is there any pattern to solve this problem in a clean way? I don't want to check every attribute whether it has changed,... I just need to synchronize the database with my excelfile. But the excefile does not exactly look like my database schema.
Would be very nice if someone would know a pattern or an approach how to solve the problem.
Upvotes: 0
Views: 189
Reputation: 773
Looks to me like you actually have two problems to solve: 1) Get the information out of the excel files into a nice schema; and 2)synchronize the database. Looks like you have #2 well under control if you're considering entity framework. To solve your #1 issue, you more than likely have to create a parser that can fill in the entities that are created in your #2 solution.
TL;DR: Separate the file parsing from the synchronizing. Get all your objects first, then update the database.
Edit: You'll have to create a mapper next. Something that can link two objects by property names. Look at "Reflection", "Extension methods" and a more generic search for mappers. I would create something that would work like this:
object1.Map("Property1",object2,"Property2");
object1 would be your database entity, Property1 the name of the property you want to map to, object2 would be the excel entity and Property2 the name of the property to map from.
PS: If the extension method is done correctly, you can 'chain' the mapping: object1.Map("Property1", object2, "Property2").Map("Property3", object3, "Property4")
and so on. You can even select different objects to map to.
Upvotes: 1