如果PetaPoco数据库类可以按照要求或需要它每次都创建一次? [英] Should the PetaPoco database class be created once per request or every time it is needed?

查看:572
本文介绍了如果PetaPoco数据库类可以按照要求或需要它每次都创建一次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过PetaPoco,我应该如何处理在创建数据库类的?我的应用程序很可能会利用各种信息库(不太DDD库,更像是一个网关库)的封装查询。

With PetaPoco, how should I handle the creation of the Database class? My application will probably be making use of various repositories (not quite the DDD repository, more like a gateway repository) to encapsulate the queries.

由于我有需要访问同一个数据库连接,我的想法做,在构造函数和调用来创建数据库类的基类的多个库处置在它的析构函数,并简单地调用所有派生类这个对象,所以我想我不会在这种情况下需要使用块(因为上课的时候​​超出范围时,将关闭数据库本身)。

Since I'll have multiple repositories that need to access the same database connection, I was thinking of making a base class that created the Database class in the constructor and calls Dispose in its destructor, and simply call this object in all derived classes, so I think I would not need a using block in this scenario (since when the class goes out of scope it will close the database itself).

我也算是刚刚创建的global.axax的数据库对象,在的Application_BeginRequest 方法,所以它的每个页面上都有,但我不完全知道如何说作品(我想我需要某种形式的的DatabaseManager 类实例化呢?我只看到这种方法的东西像NHibernate和RavenDB使用)这似乎推数据库对象本身的ASPX页面,而不是通过一个资料库/数据层类,这似乎是臭的使用。

I also considered just creating the database object in the global.axax, on the Application_BeginRequest method so it's available on every page, but I'm not entirely sure how that works (I think I'd need some kind of DatabaseManager class that instantiates it? I've only seen this approach used with things like NHibernate and RavenDB) and that seems to push the usage of the database object to the ASPX page itself instead of via a repository/data layer class, which seems smelly.

这些方法,如果有的话,将工作最好有什么建议?我也看到了PetaPoco支持共享连接,这样是事情我想看看还有尽量减少打开连接到数据库?

Any suggestions which of these approaches, if any, will work best? I've also seen that PetaPoco supports a "shared connection" so is that something I want to look at as well to minimize the number of open connections to the database?

推荐答案

我通常创建一个每个请求的数据库。它是一个轻量级的对象。无论是在 ActionFilter 使用控制器基类。

I usually create one Database per request. It is a lightweight object. Either in an ActionFilter use a Controller base class.

这篇关于如果PetaPoco数据库类可以按照要求或需要它每次都创建一次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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