0

I'm working on a project using Python Flask-SQLAlchemy with a model like this:

car has components, components can have errors

car has a hybrid_property 'broken' which is true when one of its components has errors.

Tags can be added and are assigned in a table(car_id, error_id, tag_id)

one of the tags will be something like 'irrelevant'

if all of a car's components' errors are tagged as irrelevant, broken should be false.

Is there a way to achieve this without hard-coding it into the hybrid property?

1
  • 1
    Are you asking about a Flask-SQLAlchemy specific feature or about your code?
    – marstato
    Aug 4, 2017 at 12:08

1 Answer 1

1

Of course there is. But you certainly do not want to do that; i'd be a horrible and unmaintainable mess.

Your domain model seems fine to me; hard-coding this into the hybrid property also seems perfectly fine to me. It would go something like this (pseudo-java, i dont know python, sorry):

// in Car

public boolean isBroken() {
    return this.components.stream()
       .flatMap(Component::getErrors)
       .anyMatch(Error::isRelevant);
}

// in Error

public boolean isRelevant() {
    return !this.hasTag(ErrorTags.IRRELEVANT);
}

EDIT: After thinking about it for a bit something else came to my mind:

The question really is whether the error is relevant to the Component or to the Car. If the error is irrelevant for both component and car, it'd be better to include that logic into the component:

// in Component
public boolean isBroken() {
    return this.errors.stream().anyMatch(Error::isRelevantForComponent);
}

If the error is relevant for the component but not for the car, that logic should be in the car class.
If you actually decide to go into that level of detail, the irrelevant tag should have another attribute context that describes in what context the error is relevant. You can then set that proeprty either to "CAR" or to "COMPONENT" describing that the error is irrelevant or relevant to the car respectively.

2
  • Error is only relevant for car in this case, thanks a lot for your aswer
    – tzimme
    Aug 4, 2017 at 12:49
  • @tzimme You can accept & upvote the answer if you like it :)
    – marstato
    Aug 4, 2017 at 13:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.