CollaboratingPlatypus / PetaPoco

Official PetaPoco, A tiny ORM-ish thing for your POCO's
Other
2.07k stars 600 forks source link

Add support to expose _sharedConnectionDepth as a read-only public property #633

Open BobT2022 opened 2 years ago

BobT2022 commented 2 years ago

Hi all,

It would be very beneficial for diagnostic tracing purposes throughout my application code if I could report on the _sharedConnectionDepth count either as a public read-only property of the PetaPoco IDatabase instance, and/or within the DbConnectionEventArgs arguments of the ConnectionOpened and OnConnectionClosing events. That way I can keep track of any issues arising from explicit shared connections being opened but not subsequently being closed (thus creating an imbalance that may result in the final IDatabase dispose not closing the connection when ordinarily it should).

Thanks, and great job everyone!

asherber commented 2 years ago

My concern with exposing this implementation detail would be that it doesn't always mean what the user might think it does. For example, if you do var db = new Database(myConnection), then _sharedConnectionDepth is automatically set to 2, regardless of the actual connection state. This is because when a connection is passed in, the caller is responsible for opening it and closing it; setting it to 2 prevents PetaPoco from closing it when the Database is disposed. (Yes, there are other ways the library might have implemented this.)

If this is something you need for tracing purposes, then my suggestion would be to access it yourself with reflection.

BobT2022 commented 2 years ago

Ok, however in my case I'm wrapping Database in a UnitOfWork such that it makes passing around the DB instance more abstract to the developer (along with other reasons) - to which the caller only ever supplies a connection string not a connection instance for the very reason that I don't want orphaned connections left in error if a dev doesn't remember to close a connection they explicitly opened. Not ideal using reflection either as that'll be a massive assumption that the internals of PetaPoco never change.

I'll close off this request now as it's not massively important at the moment, plus I can always hook into the ConnectionOpened and OnConnectionClosing events in my UnitOfWork and report that way.

Thanks anyway :)