0

I am now struggling with a dilemma. In my system I get a lot of data from various sensors in one complicated message and not well structured message. I can't influence it's structure and it also changes quite often, so as a first step i translate it to my own object, let's call it MyMessage.

Then there is a rule that if a sensor of the data exist in my DB (valid data), I should process the data and if not (invalid data), then I should store the data to a table and try to reprocess them later.

My question is following: Who should do the validation if the sensor exist in the database. Should MyMessage decide what data are valid and provide them separately from the invalid data.

I try to combine the Domain Driven Design and also keep the Single Responsibility Principle. That means if the MyMessage would be able to validate itself, then I will break the SRP. But if there is some Validator or Splitter, that splits the valid and invalid data, then I break the fundamentals of DDD, because I add some new component, that has nothing to do with the domain.

Michal Krasny
  • 5,434
  • 7
  • 36
  • 64

3 Answers3

2

You have two domain objects here:

IncomingMessage MyMessage

IncomingMessage has functionality to validate itself, and also to produce a MyMessage from itself, or save itself to the database if it is not valid.

SRP does not mean one object, you have two different objects here.

Rob Conklin
  • 8,806
  • 1
  • 19
  • 23
1

So you are making an ACL (anti coruption layer) for this message?

In my opinion validation should be done in application service layer, since sensor data is retrieved using some sensor repository, and repository should be used within app service.

in example:

service new_incoming_sensor_message(message_data):
    new_incoming_message = IncomingSensorMessage(message_data)
    # validation part
    try catch:
        sensor = SensorRepository.get(message_data.sensor_id)
    except NoResults:
        invalid_message = InvalidMessage(new_incoming_message)
        InvalidMessageRepository.save(invalid_message)
        return
    # process with message and sensor

You can of course move validation part to separate function or use policy pattern.

Rafał Łużyński
  • 7,083
  • 5
  • 26
  • 38
1

I would approach this problem by implementing a domain service to act upon my domain model (MyMessageValidator or something), and having my anti-corruption layer choose when to pass my domain model to this service for validation. The ACL may then choose how to proceed based on the results.

llwatkins
  • 11
  • 2