DDD and application layer - java

Is the application layer (application services) where I add #Stateful, #Stateless, #WebService etc. in DDD? From the link below this seems to be right.
Second question: I have made a repository class, should all method calls involving repository be wrappen in an application service? Or can I use repository classes directly in let say backing beans in JSF? When and what do I put in the application layer. I don't understand where the EJBs belong in this artitechture.
Application Layer: This layer coordinates the application activity. It doesn't contain any business logic. It does not hold the state of business objects, but it can hold the state of an application task's progress.
http://www.infoq.com/articles/ddd-in-practice

Java EE advocates anemic model, which is the opposite of DDD. If you want DDD, your entities must also perform the business logic, which is contrary to the separation of service layer and entities.

It's supposed that in DDD architecture the Application Layer and all the other layer communicate only with lower layer of the system. For this reason the application service wrapp repositories, but if you are using dependency inyection you can reuse all your repositories along the application service layer
The real work of AppService is to coordinate operations beetwen other service or repositories or inclusive domain services.
In your entities must to be bussiness logic only about your own entities. Always respecting Single Responsibility Principle (http://www.developerfusion.com/article/137636/taking-the-single-responsibility-principle-seriously/).
I hope be helpful

Related

How to use Spring cache in hexagonal architecture?

In hexagonal architecture, domain layer has no dépendency with framework.
Is it possible to use Spring cache in domain layer ?
Caching is application concept, not a business concept: you could write another application for the same business that would not have cache involved. In an hexagonal architecture, caching should go in the adapter layer, not in the domain layer.
Hexagonal architecture isolates the domain logic from the Infrastructure, such as the database, search engine, message queue, mail delivery, and the cache system. The domain is free of any infrastructure and framework boilerplat. Only Infrastructure Layer must contain the implementation details of Spring Cache. It is achieved via Dependency Inversion Principle by making the class depend on abstractions (interfaces or abstract classes) instead of concrete classes. This means that the depending class has no knowledge about the concrete class that it is going to use, it has no reference to the fully qualified class name of the classes that it depends on. High-level modules should not depend on low-level modules. Both should depend on abstractions. Abstractions should not depend upon details. Details should depend upon abstractions.
Let's review several implementations where cache makes sense.
Cache at the Repository Level
You can define repository contract in Domain Layer and the implementation must be done in Infrastructure Layer where you define persistence and Spring Cache details. Domain will not know details about the cache system but will use it thru repository implementation. Every client of your repository will use the cache.
Cache at the Application layer
Application Service is about use cases. Implementation cache on this level gives you more control of where and when you want to use a cache. You can define Cahce Manager contract at Application layer and perform implementation details in Infrastructure Layer. Also it can be done like proxy or decorator for your services where you will apply Spring Cache annotations.

REST - Application layer or Service layer?

I am writing web application which exposes REST interface and web socket for clients. This project interacts with underlying database. The UI layer and overall business logic is handled as separate project. Now I wanted to apply layers in this web application.
I have split my web application as,
1. Service layer (Exposes REST and Websocket)
2. Domain layer (Handling web application's Business logic)
3. Persistance layer (DB access)
Is this separation right? or Should i consider REST as application layer? Is application layer a.k.a service layer? Which is more meaningful here?
The REST interface belongs in the same layer as the UI. It is a way of interacting with your app, just doesn’t have a user interface. I like to put the API in a separate project from the UI so it can be deployed separately and scaled separately. Ideally the UI would use the API for its data access.
With that said, asking this question implies you are designing your own architecture pattern, which is not a good idea. Much bigger brains that mine have attacked and solved this problems many times so you should pick one and use it. The one I find most flexible is the Onion Architecture (https://dzone.com/articles/onion-architecture-is-interesting), which fits well with DDD or Active Record. In the Onion Architecture, though, things are split between interface and implementation in a way that takes some getting used to so if you decide to migrate that way give yourself a little extra time to get accustomed to it. Once you do, you will be happy with the flexibility.

Passing Presentation Modal objects to the service and business layer

I am developing a web application using struts2 EJB3(Service/business layer) and Hibernate. I am using Wildfly 10 as a server. Struts is at presentation later, EJB3 at business layer along with simple java classes as Service layer and hibernate is used in persistence later.
Now in one of my action classes I have passed the modal object to the service layer(Simple java classes). Now when I created the EAR and tried to deploy it on the WIldfly. WIldfly refused to start. Then I realised that my ejb module is not able to find the classes of web module. So now I have two ways to resolve this problem:-
1) Either include my web classes in EJB jar:- I think it will completely kill the layered architecture and decoupling of presentation layer and service layer.
2) Or map the modal classes to some other modal classes present in service layer:- It will require to create redundant POJO classes in service layer as well.
Not really know what should I do in this case and if someboddy can suggest me with some better layering structure
There are serveral ways you can architect your models for each layer but at the end of the day, it depends on how much you wish to couple each layer with one another.
Obviously the most granular and cleanest solution is to allow each layer to have its own models and to map accordingly at each integration point.
Persistence models, e.g. your #Entity classes
Domain models, e.g. those your service tier takes as input and returns as output.
View models, e.g. those your presentation tier takes as input and returns as output.
The advantage of such a distinction is that it allows the models at each level to morph over time without a significant impact to the tiers above it. In other words, adding a new field or changing something with your persistence model doesn't necessarily mean your domain models must change, but just the mapping code between them.
Obviously there are a number of sources on the internet which advocate simply reusing your #Entity classes as the model at each tier and for simple applications, that's definitely acceptable. But in more complex, scalable solutions that eventually becomes a liability.
Sometimes it's acceptable to collapse (1) and (2) into a single model, the #Entity and then use special view models for rendering so at least your view code isn't impacted when you make model changes over-time, but I typically err on the side of using a different model for all three tiers in many cases for complex applications.

Using delegation to separate layers

We refactored a Java app where the code was spilled all over and didn't follow even the most basic principles or patterns.
We managed to extract the following layers:
a data layer
a repository layer with command and query repositories for each kind of db entity
a service layer which works only with repositories and can have business logic too
a service orchestration layer which does service composition in order to implement the business of the application (doesn't work with repositories)
an API and a Controller layer for displaying the views, which should work only with orchestration services
There are multiple situations where a controller or the API needs to call a method from a service (service layer), but given our desire to have the controllers working only with orchestration services we are forced to create an orchestration service which doesn't do anything but delegates the method calls to the actual service.
Is this a correct approach, or should we remove the middle man (orchestration) when it doesn't do anything but delegation?
Best regards,
Cristian.
If it were my code, I would prefer to follow a standard - even if the orchestration layer sometimes just delegates straight to the service layer. In the event that a business process changes, that change will hopefully be isolated to the orchestration layer, leaving the calling code unaffected by the change. In addition, if you're trying to enforce standards around code structure, be consistent about it, lest bad practice starts creeping in again. If you sometimes invoke the service layer directly, and at other times, go via the orchestration layer, it might just lead to confusion amongst developers in terms of which approach to use when.
Lastly, review the granularity of your operations - do certain method calls truly exist in complete isolation, or do they form part of a larger business process? I agree with your layering scheme - your presentation layer should know how to invoke a business process (exposed by the orchestration layer), but it should be ignorant of the details involved in executing that process (exposed by the service layer). Even if the business process consists of only a single step, it's still a business process that can be exposed by the orchestration layer - it just happens to be a very simple process.

Technical architecture : Service layer or not?

we have a platform composed of few applications.
We have to develop an API which must factorize all interaction to a database.
In this API, we will too have a version of the database in XML format.
The XML format will be used just by one application.
Our application are developped in a classic architectural : dao - service layer - presentation layer.
The DAO layer will be moved into API. No problem on that point.
In many application, the service layer is just a gateway between presentation & dao layer without specific business code.
So my questions are :
should we create the API with a Service layer for each dao ?
if yes, so keep a service layer in application which call service layer from API ?
should we create a service layer in API which manage the factory (database/XML), but that mean each application has to give an information to choose the dao to select (when just one application has this need) ?
or just create all dao in different packages for database & xml in API, keep factory in the application which has the need, and call dao (database) in all others ?
Need help ! :p
I'm lost...
FYI, we are in Java 1.6 with Spring 3.1.1. JDBC Template in DAO for moment.
We are looking informations about spring data jdbc to replace JDBC Template.
Any suggestion about that can be appreciated ^^
[ No Hibernate - JPA solution ]
Thank you.
[edit 1]
In other word, keep a service layer in application & in the API is a way for me to have an abstraction layer. If we have to modify the database structure, maybe we don't have to edit all applications if we can make some changes directly in the service layer of API too.
Imagine 3 possibilities :
service layer in API with factory to choose which dao to use (xml/database)
just dao in API
service layer in API for database & specific layer in API for XML
What solution do you choose ?
[edit 2]
Is it interesting to create an unique class to call the API ? like in a Facade design Pattern.
Use the Service Layer for what is meant to do: provide real world services, by using one ore more DAOs per service offered (and other stuff like managing transactions, sanitizing Strings and other things like that).
If you (together with a more experienced coworker) think you don't need this service layer, then do not put it into your architecture. But make sure to create a proof of concept of your architecture (this may contain the implementation of a somewhat complex functionality of the system or part of it) so you can evaluate it later to demonstrate if you really don't need such layer (or if you do).
My understanding is that you need to decouple and reuse the business layer from the presentation layer, having multiple client applications using the same business implementation.
In this case you need to implement the service layer in the API. Some advantages:
You don't need to repeat the service implementation for any client application handling the presentation.
You can easily decouple the database model design from the business. Design patterns (DTO, Facade) and cross cutting concerns may be easily introduced.
A well designed service layer will require a minimum amount of modifications, compared to DAOs holding DB implementation details.
In this API, we will too have a version of the database in XML format. The XML format will be used just by one application.
By providing this implementation via the API more client applications will be able to use it in the future, if needed.
Spring remoting is offering excellent tools for such a design (RMI, HTTP invoker etc)
I don't see any added value in proving an API for DAOs.

Categories

Resources