dimanche 10 décembre 2017

How to maintain SRP with multiple dependices

I am confused in a situation where a class must have to depend on other factors.

For example

class Storage:
    def __init__(self):
        self.logger = Logger()
        self.client = Elasticsearch()

    def index(document):
        try:
            self.client.index(document)
         except ElasticsearchException as e:
             self.logger.error(str(e))

Here my class have to have logger and a elasticsearch object to perform its operations. How can I maintain SRP in this situation where there are possibly two scenarios my class have to be changed

  1. I switched to different database
  2. I switched to different logging library

One could argue that instead of logging here I should let the client class handle the exception. But in a scenario where client is just yeilding the documents to be inserted and it is accepted to have failed index operation client class doesn't bother about errors. Also even if i rethrow the exception to client class, the same problem SRP problem will occur there.

I would appreciate for explanatory answer in my context.

Thanks

Aucun commentaire:

Enregistrer un commentaire