1

I'm looking for some advice, it may be that there is no hard and fast answer but any thoughts are appreciated. The application is very typical. Its a c# code, currently using VS2010. This code is used amongst other things for providing data from a model to end users. The model itself is based on some known data at fixed points (a "cube") various parameters and settings. In standard programming practice the user accesses the data via public "get" functions which in turn rely on private member variables such as the known data and the settings. So far so standard. Now I want to save the class providing this data to the users into an sql database - primarily so all users can share the same data (or more precisely model generated data).

Of course I could just take each member variable of the class and write these into the db using sql database and reinstantiate the class from these. But I dont want to miss out on all the goodies .net & c# has to offer. So what I'm thinking of doing is serializing the object and using linq to sql to squirt this into the db. The linq to sql section is straightforward, but I'm a total newbie when it comes to serialization and I'm a bit concerned/confused about it. It seems the obvious thing is to format the object into xml and write this into the database as a column in the table with sql datatype "xml". But that leaves me with a few questions.

To my understanding the standard XMLserializer will only write the public members of the class into the xml. That looks like a non-starter to me since my class design is predicated on keeping much of the class private (writing classes with all public members is outside of my experience - who does that ?). This has led me to consider the DataContractSerializer in which you can opt-in variables for serialization. However this seems to have some WCF dependencies and I'm wondering what are the potential drawbacks of using it. Additionally there is the SoapFormatter, which seems to be more prevalent for web applications and also JSON. I'm not really considering these, but maybe I should ? Maybe there are better ways of solving the problem ? Perhaps a bit verbose but hopefully all the context can help so people can shoot me some advice.

4

2 回答 2

0

我有与您类似的要求,并且在这方面做了很多研究。我使用 XMLSerialization、JSON、BinraryFormatter 完成了许多概念验证项目,并且不要忘记一些自产的 hack。我几乎决定使用 JSON (JSON.NET),直到我找到protobuf-net!它速度快、体积小、版本独立、支持继承且易于实现,无需对代码进行太多更改。大力推荐。

于 2013-06-30T11:57:18.050 回答
0

如果将对象存储为 XML,则很难从数据库中使用它。例如,如果您将客户对象存储为 XML,您将如何编写以下内容?

select * from Customers where LastName = 'Obama'

这是可以做到的,但这并不容易。

如何将对象映射到数据库是一个有争议的主题。易于上手的框架在应用程序的后期可能会变得过于复杂。由于大多数应用程序在维护上花费的时间比在初始开发上要多,所以我会使用最简单的模型。

普通的 ADO.NET 或Dapper是很好的竞争者。您将编写更多样板代码,但复杂性的降低足以弥补这一点。

于 2013-06-30T12:06:59.747 回答