coronavirus-tracker-api icon indicating copy to clipboard operation
coronavirus-tracker-api copied to clipboard

Aggregate Pattern for DataSource

Open MikePresman opened this issue 3 years ago • 0 comments

Modified Files:

  • 'app/data/__init.py'
  • 'main.py'
  • 'app/routers/v2.py'

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.

MikePresman avatar Jul 15 '21 16:07 MikePresman