实体框架与游戏服务器 [英] Entity Framework with a game server

查看:143
本文介绍了实体框架与游戏服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找到使用实体框架在我的C#游戏服务器,使查询变得更加容易。我是类型安全的一个巨大的风扇,和实体框架做了伟大的工作,在自动化大部分的样板code。虽然我不太清楚如何去利用一些组件,即的ObjectContext

I have been looking into using the Entity Framework in my C# game server to make querying easier. I am a huge fan of type safety, and the Entity Framework does a great job at automating most of the boilerplate code. Though I am not quite sure how to go about utilizing some of the components, namely the ObjectContext.

服务器使用相当多线程的,所以线程安全的,值得关注。现在,我只是用一个自定义的池执行查询。没有进入很多细节,每个查询工作在时尚:

The server uses quite a lot of threading, so thread safety is a concern. Right now I just use a custom pool for executing queries. Without going into much detail, each query works in the fashion of:

  1. 的DbConnection
  2. 的DbCommand
  3. 允许查询类来设置参数
  4. 执行的DbCommand
  5. 允许的查询类来处理查询结果,如果有的话
  6. 免费的的DbCommand
  7. 免费的的DbConnection
  1. Grab a DbConnection
  2. Grab a DbCommand
  3. Allow for the query class to set the parameters
  4. Execute the DbCommand
  5. Allow for the query class to handle the query result, if any
  6. Free the DbCommand
  7. Free the DbConnection

这是非常干净,快捷,安全的,但问题是,创建查询是有点麻烦,我必须手动生成和更新容器类如果我想要的类型安全。这就是为什么我转向了实体框架。

It is very clean, fast, and safe, but the problem is that creating queries is a bit of a hassle, and I have to manually generate and update "container classes" if I want the type safety. This is why I have turned to the Entity Framework.

这一切仅仅用了的DbConnection 的DbCommand ,因为没有顾虑而<$ C的伟大工程$ C>的DbConnection /命令为哪个对象或查询任何东西。

This all works great with using just the DbConnection and DbCommand since there is no concerns about which DbConnection/Command performs queries for which object or anything.

不管怎么说,我真的不知道该如何解释更不强加限制。做这样的事情每一次我通常会与执行查询的DbConnection /命令,保存它,和处理的ObjectContext只是增加了太多的开销,当我真的不需要数据库被如此频繁地更新。

Anyways, I don't really know how to explain it much more without imposing restrictions. Doing something like executing a query every time I would normally with the DbConnection/Command, saving it, and disposing the ObjectContext just adds too much overhead when I really don't need the database to be updated so frequently.

你会如何去使用实体框架为不具有高要求的数据库上立即不断地被游戏服务器最多最新的?

How would you go about using the Entity Framework for a game server that doesn't have a high demand on the database being immediately and constantly up-to-date?

推荐答案

,你将最有可能发现性能上的差异与实体框架是在数据的更新(未插入)的地方。这是由于这样的事实,即数据必须首先从数据库中读取,然后改变,则保存回数据库。

The place that you will most probably notice the difference in performance with Entity framework is in the update of data (not insert). This is due to the fact that the data must first be read from the database, then changed, then saved back to the database.

有关我们使用using语句,这样才得到处理马上对象上下文。这将不利于游戏得到一个暂停,而垃圾收集器运行处理上是超出范围的所有对象。

For the object context we use the using statement, so that it gets disposed straight away. It would not be good for a game to get a pause while the garbage collector ran dispose on all objects that were out of scope.

如果你主要是读我建议缓存中的数据,例如使用企业库缓存应用程序块。

If you have mainly read I would recommend caching the data, using for example Enterprise Library Caching application block.

实体框架会给你一个更高效的编程模型,同时缓存会给你更好的表现。

Entity Framework will give you a more productive programming model, while the caching will give you better performance.

这篇关于实体框架与游戏服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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