Aggregate Pattern for DataSource #348
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Modified Files:
Modified the file 'app/data/init.py' to implement the AGGREGATION pattern.
Replaces the original way of accessing the DATA_SOURCES dictionary. Rather than having the dictionary be a globally accessible object the dictionary is now only accessible through an instance of the class 'DataSource' which acts as the root in the boundary.
The boundary of 'DataSource' would thus include the __DATA_SOURCE dictionary which is now encapsulated, which holds references to the services (NYT, CSBS, Jhu).
As such the dictionary is now accessible through a getter
def get_data_source(self, source: str) -> LocationService:And the dictionary can also be appended through a setter
def add_data_source(self, source: str, reference_to_source: LocationService) -> None:Also to ensure consistency in functionality with how sources are accessed in each request in 'v2.py' the class (DataSource) implements a
def get_all_sources(self) -> dict:method which returns the dictionary. This prevents breaking any functionality in 'v2.py'.For example instead of an endpoint accessing a source via
locations = await request.state.source.get_all()It is now accessed with a minor change of
locations = await request.state.source.get_all_sources()[source].get_all()Minor changes in 'main.py' are implemented in order to set the default source.
Before:
source = data_source(request.query_params.get("source", default="jhu"))After:
source = DataSource()source.get_data_source(request.query_params.get("source", default = "jhu"))Again, this implementation encourages aggregation which in turn enforces the objects inside the boundary to be only accessible through the root. This ensures invariants such as the fact that only one source should be returned through the getter.