Skip to content

Instantly share code, notes, and snippets.

@richiefrost
Last active January 9, 2022 22:06
Show Gist options
  • Save richiefrost/4c8961d22a8ae74c4a6b58501da8032c to your computer and use it in GitHub Desktop.
Save richiefrost/4c8961d22a8ae74c4a6b58501da8032c to your computer and use it in GitHub Desktop.
Simple example of using dependency injection
# 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