克隆分离的对象 [英] Clone detached objects

查看:133
本文介绍了克隆分离的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有在线游戏数据库。并且有下表:

项目类型:
标识
...其他列...
和表
项目:
Id)ItemTypeId
...其他列...
具有1对多的关系。

因为"ItemType"中的数据表格变化很少,我想缓存这些数据:

在第一次请求时我加载所有ItemType,分离它们并存储在一些静态字段中。
在其他请求中我将ItemType项目附加到当前实体。并且它自动附加到任何加载的"项目"。所以我不需要"包括("ItemType")""查询项目时。

但由于它是在线的,我不能为所有用户使用一个分离的对象。我必须附加每个用户请求克隆的分离的ItemType对象。

我找到了通过序列化/反序列化克隆的方法。 (序列化并存储在静态中。比每个请求的反序列化。)但我不认为这是最好的方法。有没有更好的方法或我做错了?

抱歉我的英文。

I have database for online game. And there are next tables:

ItemType:
Id
...other columns...
and table
Item:
Id
ItemTypeId
...other columns...
With 1 to many relationship.

Because data in the "ItemType" table changes very rarely, I want to cache this data:

On first request I load all ItemType, detach them and store in some static field.
On other requests I attach ItemType items to current entity. And its automatically attaches to any loaded "Item". So I don't need to "Include("ItemType")" when querying Items.

But because it is online, I can't use one detached object for all users. I must attach for each user request clone of detached ItemType objects.

I have found way to clone thru the serialization/deserialization. (Serialize and store in static. Than deserialize for each request.) But I don't think it is a best way. Is there better way or I make some wrong?

Sorry for my English.

推荐答案

使用二进制格式的DataContractSerializer应该会给你带来好的结果当然这取决于实体图的大小。

您也可以使用反射来迭代对象的属性并复制它们,但这需要您编写一些图形工作(递归迭代通过实体),这使得序列化/反序列化成为一个更引人注目的解决方案。

如果你有一个大图并且你害怕你会得到糟糕的表现,你总是可以创建一个反序列化的对象池和为每个请求者提供一个实例,并使用应用程序的空闲时间来重新生成池中的对象
Using DataContractSerializer with binary format should give you good result, of course it depends on the size of your entities graph.

You can also use reflection to iterate through the object's properties and copy them, but this will require you to write some graph work (recursive iteration through entities), which makes the serialize/deserialize a more compelling solution.

If you have a large graph and you're affraid you'll get poor performance, you can always create a pool of deserialized objects and give away an instance for each requester, and use idle time of your application to regenerate objects in the pool


这篇关于克隆分离的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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