I am building a 3rd-party library that requires a database access. To be more specific, it’s an access control library. Currently the biggest problem I am facing is that how do I make it compatible with most frameworks and CMS’s?
I could issue queries using a database instance that the developer specifies. This has three problems:
- I need an access to a database instance.
- The library needs to be able to deal with different database instances (PDO, MySQLi, mysql-resource, postgresql-resource, etc.)
- There would be no caching, because the queries do not go through the application (assuming the app uses some).
Or I could just have methods that return the SQL, but is that any better?
The Adapter Design Pattern springs to mind
For your concrete problem this means you create a class for your framework that exposes an API with all the method required by your other classes. This is the only interface you will interact it. Then you create adapters for each database you want to support. So your adapter will become the meditator between your framework and the DAL.
Example:
The above is your main class. You will only interact with this class from your remaining framework’s code. The functionality required to interact with a specific database is contained within a concrete adapter then. You instantiate and inject an adapter at runtime.
Add additional adapters as you see fit.