I am trying to come up with a design for an application I am working on.

Here is a basic picture of what I am looking to do. Everything up to the Data Layer is totally fine but the Data Layer makes request from the Network Layer as shown in the picture, and that is one way, from Data Layer to Network Layer.

enter image description here

Here is my dilemma. Some network requests need a token to be executed, but how do I get this token in the network layer if I cant access the Data Layer from Network Layer

The token gets generated by first logging in. So the user starts the login process from the UI Layer -> Login Service -> LoginInfoRepository -> Get User (from network layer). Then the token gets cached.

Process of a request that requires token

User executes a process that requires token from UI Layer -> XXX Service -> XXRepository -> Get XXX (from network layer); however, the GET XXX request needs a token (which was generated from the login process and now it exits in the LoginInfoRepository) to make the request. How do I get access to that token?? I am confused, maybe I am doing something wrong in this design?

Note: I am trying to adhere to single responsibility principle.

One way I thought to solve it is to pass the token from the repository to network layer in the constructor but again I am trying to adhere to SRP and a single repository can only control itself it can't access a different repository such as LoginInfoRepository

Note I am still learning the concept of DDD and all it’s rules so if you feel I have violated the architecture or misused it please feel free to correct me as well.

Edit My understand of DDD is as follows The UI layer is responsible presenting data and accepting user input. If if needs to do any operation, say login. It would call the service in the domain layer, this is responsible on all the business logic including creating/retrieving different data but not single CRUD operations. So the service would call repositories to access the data. And finally the repository would make calls to network layer to get data from different APIs.

I don’t think what I am trying to achieve is impossible but I think I might have tackled the problem incorrectly. I mean apps all around do login and have a JWT that gets passed to the network layer. I am confused to be honest.

  • 1
    The service in what you call Domain Layer, is that in reality an application service? Because typically domain services do not have dependencies on repositories, but application services do. So if it's an application service, it is not / should not be in the domain layer and it can have a dependency on what you call network layer. – Rik D Nov 19 '20 at 11:16
  • @RikD Interesting. I believe (apologize for not sure but I am confused in the design now, just got started with ddd) it is Domain Service in this case since it is really responsible on doing business logic but how can it do business logic if it doesn’t have access to repository? Also the network layer is what makes requests to the web. to one of the available APIs. My idea and thinking was as follows. UI layer is responsible for accepting user input, which gets sent to the Domain Layer which does bunch of business logic and then store/retrieve appropriate data from repository layer – Muhand Jumah Nov 19 '20 at 14:35
  • And then the repository layer would return the appropriate data from cache or the web or any other medium. And finally the network layer would make the actual API calls to request the data. I guess we can call this an application service? Which in that case it puts it under Domain Layer and hence it can access repository? – Muhand Jumah Nov 19 '20 at 14:36
  • We now enter a really broad topic, which makes the question likely off-topic (If you can imagine an entire book that answers your question, you’re asking too much). And in fact books have been written on this topic. Look into things like Clean Architecture, Onion Architecture and Hexagonal Architecture, for guidance how to properly separate concerns into layers. – Rik D Nov 19 '20 at 14:48
  • how can it do business logic if it doesn’t have access to repository An application service calls the repository to get an Aggregate Root (AR). This AR is your entry point into the Domain. Calling a method on the AR changes it's internal state. Finally the application service tells the repostitory to persist the AR with the new state. – Rik D Nov 19 '20 at 15:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.