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?
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