On Sun, Jul 12, 2020 at 12:47 AM Olle Härstedt <olleharst...@gmail.com> wrote:
> You're misunderstanding. :) Yes, it should be shared, but not with the > same access level as the owner. If a dependent class closes the > connection it will cause spooky-action-at-a-distance somewhere else in > the program. Sorry, it's related to refcount as ownership checking and > not to throwing exception at refcount > 1. If your class doesn't know whether it created its connection or it was passed to it, it's badly designed. Not only would that mix concerns (making DB requests and managing connections), it would also be hard to test and hard to keep in the programmer's head. Use dependency injection to avoid that. I'm not convinced by this example. You can't use ownership as a protection against code doing something stupid. Imagine for a second you've prevented non-owners from closing your connection. Will that also prevent non-owners from dropping your database? What about them messing with your connection object's properties? Language features can only prevent developers from making accidental mistakes. Bad code is not an accidental mistake. -- Best regards, Max Semenik