4

I'm faced with the challenge of writing an object persistence mechanism that serializes/deserializes to a SQL database and XML files.

For the sake of illustration, imagine I have a graph of objects that has a single root object. Maybe a "tree", for example, which has all manner of child objects -- leaves, brances, nuts, squirrels, birds and the like.

I need a suggestion for an architecture that seamlessly moves between loading & saving a "tree" from a file and/or database. It needs to be able to load a "tree" from a file and save it to a database, or the other way around.

I'm currently using Entity Framework for my SQL persistence, and I'm happy enough with it. For the XML I'm using XDocument, which I also like a lot, but I'm wondering if there isn't some framework out there that already does all this.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Heather
  • 2,602
  • 1
  • 24
  • 33
  • What's wrong with using an XML column in SQL Server? – John Saunders Sep 14 '10 at 20:00
  • I should have noted that this is the tail end of a project. The original spec was for SQL persistence only, and a ton of work has been done down this road; SQL persistence is working fine. Now a requirement has been added to be able to load/save the same set of entities to a file, while still maintaining the SQL functionality. I'm trying to meet this requirement without a wholesale re-engineering of existing work. – Heather Sep 15 '10 at 20:38
  • Was the xml file format a requirement? Similar to what Fatal mentioned, you could use a private file based Sql Server Compact deployment which is just a local file based database (which doesn't require an install or anything). You would use EF to interact with it so it would have minimal impact on your app. Almost as simple as just using your context with a different connection string. – Kenneth Ito Oct 25 '12 at 01:36

3 Answers3

1

Unless you want to do querying on your objects in Sql Server (or there are other sources that may update/manage relational data), using EF to convert into relation schema is a bit overkill. If all you want is to persist your object graph in different mediums then you should consider runtime serialization or DataContractSerializer. Essentially, you will get binary data or XML that you can dump into any storage medium including Sql Server. This will free you from changing relation schema in sql server when your object structures changes. However, you must consider versioning your objects while going from serialization approach.

VinayC
  • 47,395
  • 5
  • 59
  • 72
0

You can try using the older, yet very nice XmlSerializer.

ps. need to watch out for anything Entity Framework may require from you when loading an object you serialized to a xml file.

eglasius
  • 35,831
  • 5
  • 65
  • 110
  • Agreed, are there specific issues that prevent you from using any of the flavors of automatic XML serialization/deserialization? – Alex Paven Sep 14 '10 at 20:52
  • @Alex from the top of my head, you need to have a parameter less constructor. There are also some scenarios where you need to add extra attribute(s) when you have a member that is any variation of a list that can is holding subclasses in it. For a more complete list I'd search stackoverflow and post a question if it hasn't been asked before. – eglasius Sep 14 '10 at 21:01
  • Ok then, there's DataContractSerializer which is a lot more robust. (Also, XmlSerializer handles lists just fine, but Dictionaries may pose problems). And there are plenty open-source serializers - for example http://code.google.com/p/nserializer/ . – Alex Paven Sep 14 '10 at 21:13
  • @Alex that's a very good point. It would great if you have a link on that. Quick search on stack: http://stackoverflow.com/questions/2505778/datacontractserializer-vs-xmlserializer-pros-and-cons-of-each-serializer, http://stackoverflow.com/questions/883289/which-one-better-handles-versioning-xmlserializer-vs-datacontractserializer --- also got to http://www.danrigsby.com/blog/index.php/2008/03/07/xmlserializer-vs-datacontractserializer-serialization-in-wcf/. – eglasius Sep 14 '10 at 21:36
  • Those links are quite ok. Maybe I'm not the best one to suggest anything in this area though, since I didn't ever need anything other than the puny XmlSerializer. – Alex Paven Sep 15 '10 at 05:43
0

Are there any strict requirements around the entities being saved in XML format? If not, another option could be to use SQLite (http://sqlite.phxsoftware.com/) with the entity framework when you need local/filesystem persistence.

Fatal
  • 3,338
  • 3
  • 19
  • 15