petapoco+链接mysql,如果PetaPoco数据库类可以按照要求或需要它每次都创建一次?...

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值