I was wondering for a few ways to accomplish abstracting the data storage access away from the main application and a small example, an IoC framework seems overkill, maybe passing object via constructor parameter (facade).
is the below pseudo-code a good way to do things and how do i fill in the missing pieces with python?
main.py
resp = Repository(engine=NoSql) # can easily switch to nosql???
resp.save("hello");
resp.select("hello");
repository.py
class Repository:
def __init__(self, engine):
self.engine = engine
def save(self, str)
engine.save(str)
def select(self, str)
engine.select(str)
nosql.py
class NoSql:
def save(self, str)
nosql.save(str)
def select(self, str)
nosql.select(str)
mysql.py
class MySql:
def save(self, str)
mysql.save(str)
def select(self, str)
mysql.select(str)
I’d say the above isn’t a great way to go about it. The consumer (in this case
main.py) shouldn’t know anything about the model’s implementation details. What I might do is store key/value pairs in an external configuration file to be used by the model to determine the engine to use.