C#:如何在SQL Server中存储任意对象? [英] C#: How would you store arbitrary objects in an SQL Server?

查看:201
本文介绍了C#:如何在SQL Server中存储任意对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设你有任意类型的各种对象,你想要存储在一个键+值类型的表中。 Key可以是一个int,string或guid。价值是什么?字符串,二进制或其他东西?



您将如何存储和加载对象?我会想到一些序列化,但是什么样的?






我有一个解决方案,使用这两种方法:

  public T Get< T>(string key)

public void Set< ; T>(字符串键,T值)

在数据库中我有一个带有字符串列的表和二进制列。然后我使用一个 BinaryFormatter 序列化和反序列化该值,并将Linq2Sql将二进制结果放入数据库表中。但这是一个很好的解决方案吗?目前,我只敢用简单的值(如整数和字符串)来尝试。 BinaryFormatter 和序列化通常与更复杂的类型(如结构体和类)一起工作?特别是如果例如值包含数组或列表的东西。



任何指针?






目前,我将使用它来存储各种最后选择或类型的类型的值。虽然他们可能并不总是必须打字。例如,它可能是从列表中选择值。要点是他们几乎只是为用户提供便利的东西,所以不是非常关键的数据。

解决方案

如果可以限制自己的特定类型,容易映射到SQL类型,我会试图将它们保留在表中的单独的列中,确保只填充其中的一个。这样您可以在数据库中具有人类可读的数据,哪些使得即时查询更容易。这也意味着你不会永远被锁定到.NET中。



有很多不同的序列化选项可用。当然,核心框架是 BinaryFormatter XmlSerializer XML是便携式的,但是以空间为代价。我相信它也不太可定制。



有第三方序列化技术,如 Thrift 协议缓冲区。这些对于他们可以串行化的方式将会有更多的限制),但是更便于携带。 (免责声明:我的20%的项目是协议缓冲区的 C#端口,所以我不是完全不偏不倚的。)



你还应该考虑版本控制 - 如果你改变你要序列化/反序列化的数据结构,你想要发生什么?也许你不需要阅读旧记录,也许你可以这样做。也许你需要旧的代码才能阅读新记录 - 或者也可能没有。



您的技术选择应该由需求驱动。你试图做的越一般,越复杂它就会变得越来越重要 - 所以在尝试提出一个解决方案之前,先弄清楚你真正需要的。


Lets say you have various objects of arbitrary type that you would like to store in a key+value type of table. Key could for example be an int, string or guid. What would the value be? String, Binary or something else?

And how would you store and load the objects? I would think some sort of serialization, but what kind?


I have one solution at the moment where I have a class with these two methods:

public T Get<T>(string key)

public void Set<T>(string key, T value)

In the database I have a table with a string column and a binary column. I then use a BinaryFormatter to serialize and deserialize the value and Linq2Sql to put the binary result in the database table. But is this a good solution? Currently I have only dared trying this with simple values like integers and strings. How do the BinaryFormatter and serialization in general work with more complex types like structs and class? Especially if for example the value contains things like arrays or lists.

Any pointers?


At the moment I will be using it to store various last-selected-or-typed-etc type of values. Although they may not always necessarily be typed. For example it may be choosing values from a list. Main point is that they will pretty much be just convenience stuff for the user, so not very critical data.

解决方案

If you can restrict yourself to specific types which map easily to SQL types, I'd be tempted to keep those in separate columns in the table, making sure you only fill one of them in. That way you have human-readable data in the database, which makes ad-hoc querying easier. It also means you're not forever locked into .NET.

There are loads of different serialization options available. The core framework ones are BinaryFormatter and XmlSerializer of course; XML is much more portable, but at a cost of space. I believe it's also less thoroughly customisable.

There are third party serialization technologies such as Thrift and Protocol Buffers. These will be more restrictive in terms of what they can serialize) but more portable. (Disclaimer: my 20% project is a C# port of Protocol Buffers, so I'm not entirely unbiased here.)

You should also consider versioning - what do you want to happen if you change the data structure you're serializing/deserializing? Maybe you don't need to be able to read "old" records, maybe you do. Maybe you need old code to be able to read "new" records - or maybe not.

Your choice of technology should really be driven by requirements. The more general you try to make it, the more complex it will become - so work out what you really need before you try to come up with a solution.

这篇关于C#:如何在SQL Server中存储任意对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆