RedHatInsights/insights-host-inventory

Decouple DB model from logic

Glutexo opened this issue · 2 comments

Persisting hosts into a database is a form of serialization. The Host model’s responsibility is to represent the database record. It shouldn’t on the other hand

  1. contain any logic – instantiation, operations, export
  2. represent a host in the application

This can be done in separate steps:

The logic of how the fields and values are updated are in no way related to the database storage. Decoupling allows the application to operate with a simple object representing a Host instead of a database model with a lot of added behavior. The database model would be used only for the storage/retrieval. This increases predictability of the application.

Currently working on decoupling the export methods.

Adding that currently storing data to the database is also a kind of serialization. Splitting the database model (a SQLAlchemy object) from a business logic representation would allow to internally store the data in a smarter way. E.g. to use a namedtuple for canonical facts and a defaultdict for facts.