Why would someone choose to use a namespace rather than a singleton model?

I’ve been trying to find something that will create an sqlite database on android. I finally found something! It’s from Krueger Systems. Looking over the code, they are using a namespace that is included in another script so it can run properly. I’m curious why someone would do that over making a singleton with all the database commands. Does it work better on other platforms? Or is it just a higher level of programming?

It’s difficult to answer as a namespace and a singleton are two totally different things.

Putting things in namespaces is useful when you want to create frameworks that you want to use in different projects, and it allows you to avoid name conflicts.

If you want to gather static members and methods that do not need MonoBehaviour specific stuff, such as Coroutine, Update, etc. then you can create a static class with static members and static methods that will be available at all times.

If you need MonoBehaviour specifics, then you can create a Singleton pattern, which allows you to find/create/reference the unique instance at any given time.

It’s because there is no particular reason to make the database classes into a singleton. In fact, there’s little reason to use singleton pattern in the first place. Singletons may seem to make code easier to use on the surface, but quickly add needless and unavoidable dependency to everything referencing them, creating restrictive and hard-to-test code.

My recommendation is simply pass the database connection / manager resource to the components that need it (in the editor or at runtime). You should be able to assign the database manager field on the dependent behavior component in the Unity inspector editor window. Alternatively, there are numerous ways to have the dependent components locate the database manager in Unity at runtime or in the editor, like calling FindGameObjectsWithTag() andGetComponent() or FindObjectsOfType() (but do this sparingly, like once in Start(), etc.)

In this case, it might make sense or would be fine to have a static instance of the non-component database class in your own source file. A static instance is not a singleton unless it follows the singleton pattern. But I would avoid global state as much as possible.