Should the PetaPoco database class be created once per request or every time it is needed?

天涯浪子 提交于 2019-12-07 07:03:20

问题


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).

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.

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?


回答1:


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



来源:https://stackoverflow.com/questions/10653695/should-the-petapoco-database-class-be-created-once-per-request-or-every-time-it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!