Abhinav Rai

Domain Driven in GoLang — A problem with worker & the solution

Domain Driven in Golang means you have a specific “domain package” handle the “domain things” via an interface.

Structure of Project ->
internal
    app
        handler (dir)
        domain (dir)
            domain_service.go
        worker (dir)
    pkg
        config
        db
        kafka
        server

Job of Command is to create a new handler or worker (depending on the command). Eg: For server command, we create a NewDomainService and pass it to the NewDomainHandler. Job of Handler is just to respond to the api call (REST/GRPC) by using services it’s initialised with. NewDomainService returns an interface with all the things it has to do. Say it does 3 things.

  1. Thing 1 -> Upload to AWS
  2. Thing 2 -> Some DB Operation and push to Kafka
  3. Thing 3 -> Some DB Operation only

So NewDomainService requires 3 things (AWS, DB Store and Kafka Producer) as it’s dependency.

Let’s talk for worker now. The same process again. We have a domain service now which knows the domain. We have a NewDomainWorker just like the NewDomainHandler and we initialise it with domain service(s) and it does the job.

But I just have one thing to do (say Thing 3 or interface 3) of the service. But I would have to pass AWS & Kafka Producer also to NewDomainService although we don’t use it!

Solutions

  1. Create a NewDomainService2 in the same domain package. But there’s a smell! [Anti-pattern to Domain Driver]
  2. Pass nil/empty value for AWS and Kafka Producer args in NewDomainService. [Much better] [Accepted]