To give some context, I'm using python. We have the following package structure:
/package_name
/request.py # defines class Request
/response.py # defines class Response
Let also assume that we have a bidirectional dependency at class and module levels:
- The method
Request.run
returns aResponse
instance. Response
needs aRequest
instance to be__init__
ialised.
My naive solution would consist defining the abstract class IRequest
and IResponse
to be stored in the respective modules. As a result
Request
will implementIRequest
and depend onIResponse
Response
will implementIResponse
depend onIRequest
so the circular dependency at class level is defeated.
However, we still have a bidirectional dependency at module level, which prevents the modules to be imported when using python. The bidirectional module dependency can be solved storing the abstract classes in a third module say interfaces.py
, but it sounds a dodgy solution to me.
Question: Am I violating any architectural principle? What are possible solutions?
from request import IRequest
to document the expected input types, soresponse.py
dependes onrequest.py
(circular dependency). Could you extend your last sentence here or in an answer?Context
or something like that will return theResponse
when you send aRequest
. That way there is no direct dependency withRequest
to anything else. YourClient
andResponse
objects themselves would have a clear dependency structure.Context
orClient
can help manage default values for your request objects and introduce the concept of a session if you need to in the future. Still worth a thought.