Last active
January 9, 2022 22:06
-
-
Save richiefrost/4c8961d22a8ae74c4a6b58501da8032c to your computer and use it in GitHub Desktop.
Simple example of using dependency injection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Don't do this | |
def get_data_bad(query_text): | |
db = SQLDB() | |
return db.get(query_text) | |
# What if you need to use a DocDB instance? Or a DynamoDB instance? | |
# Do this instead | |
def get_data(db, query_text): | |
return db.get(query_text) | |
# Example | |
sqldb = SQLDB() | |
query = 'SELECT * FROM Foo' | |
data = get_data(sqldb, query) | |
# Or, if you need to use DocDB instead, you don't need to change your original get_data method | |
docdb = DocDB() | |
query = 'SELECT c.* FROM c' | |
data = get_data(docdb, query) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment