4

I am about to start creating a new application and wanted to get some feedback on the approach I plan on using. We will be using spring and spring data jpa.

  1. Can controllers call domain services and repositories or should the controller only make calls to application and infrastructure services?

  2. If its "ok" to call domain services and repositories from a controller and a domain service operation needs a transaction, can/should I just put the @Transactional annotation on the domain service method? Or should I have an application service that wraps the call (not sure I like this approach because you would end up with a bunch of pass through methods)?

  3. If its not "ok" to call domain services and repositories from a controller do I need to create application services to wrap all possible domain service calls(like I said in 2 that could be a lot of pass through methods)?

The best solution I can come up with is something like the following:

  1. Repositories will have the @Transactional annotation with propagation REQUIRED when writing to the database and propagation set to readOnly=true and propagation set to SUPPORTS when reading from the database.

  2. Application and Domain Services will add the @Transactional annotation as needed

If the controller ever needs to make make a direct call to a repository a domain service or an application service it can. No pass throughs.

enter image description here

testing123
  • 11,367
  • 10
  • 47
  • 61

2 Answers2

0

I am not clear for your question. What is the Domain Services doing? I knew Application Services and Domain Repositories very well.

In spring , there are two layers service and data access layer. Service layer can used @Service (In your design it will be application Services) but not used @Transactional Tag.

Data access layer used @Repository Tag and also @Transactional Tag, Because This layer is directly connected and make operation with the Database. So, I like to know what 's function of the Domain Service. I am not clear for that.

Thanks buddy.

Myo
  • 72
  • 3
  • Domain services represent stuff that deals with the real world domain, the application services deal with the stuff your application needs to do. There are a lot of posts explaining it more in detail. Here is the first one I found: http://stackoverflow.com/questions/3839386/domain-services-vs-application-services – testing123 Sep 10 '13 at 16:34
0

I personally would only access your domain and application services from your controllers. That way you only have to put @Transactional annotations at one "level". You get transactionality out of the box at your repository layer if you're extending the regular Spring Data repository interfaces. I would leave that layer as simple as possible. Put your readOnly and other configuration at the service layer.

Creating "pass through" methods allows you more flexibility down the road too if you ever decide to change your DAO implementation.

Ryan Walls
  • 6,962
  • 1
  • 39
  • 42