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

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

问题描述

比方说你有,你想在表的键+值类型存储任意类型的各种对象。键例如可以是一个int,字符串或GUID。什么会的价值是什么?字符串,二进制或其他什么东西?

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)

在数据库我有一个字符串列和一个二进制列的表。然后我用一个的BinaryFormatter 序列化和反序列化的价值和LINQ2SQL把二进制结果在数据库表。但是,这是一个很好的解决方案?目前,我有只敢像整数和字符串值的简单尝试此。如何在换届工作中的BinaryFormatter 和系列化更复杂的类型,如结构和类?特别是,如果例如值包含的东西如数组或列表。

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.

任何指针?


目前,我会用它来存储各种上次选择有或类型化等值的类型。虽然他们未必一定输入。例如,它可以从一个列表中选择的值。主要的一点是,他们将几乎成为用户只需方便的东西,所以不是很重要的数据。

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.

推荐答案

如果你能限制自己特定类型这就容易映射到SQL类型,我会很想让那些在单独的列在表中,确保您不仅填补其中的一个。你在数据库中的人类可读的数据通过这种方式,这使得即席查询更加容易。这也意味着你不会永远被锁定在.NET。

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.

有可用不同的序列化选项的负荷。核心框架的是的BinaryFormatter 的XmlSerializer 课程; XML是更加便于携带,但在空间成本。我相信这也不太彻底自定义的。

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.

有第三方的序列化技术,如节俭协议缓冲区。这将是在他们可以序列化什么样的条件更为严格),而且更加便于携带。 (声明:我20%的项目是协议缓冲区的 C#端口的,所以我不完全。这里偏见)

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天全站免登陆