Is this a good Spring Architecture (include testin

2019-07-24 11:48发布

问题:

I'm currently working on a Spring project. I made a diagram to illustrate what i'm saying. Is the diagram UML below represent a correct/good architecture to follow with Spring ?

To explain, the RestController redirects requests. This controller have an interface dependancy injected with the real class (here a class that handle storage of reports with files).

The class DatabaseFile implements DatabaseInterface. On one hand, some methods for content treatment ( like getContentFromReport(string) -> call readFile(String) then for instance take only important lines), on the other hand pure file method (like ReadFile using (Reader, FileUtil...)).

My problem is that file methods (readFile(), deleteFolder()) doesn't contain dependancy Injection and I can't mock some objects.

The diagram :

回答1:

You must follow the standard MVC approach(view will be the response in JSON/XML). You must keep all you business logic inside DAO layer and inject DAO inside the service layer and then inject this service into the rest controller. For the testing purpose you can mock the dao and service layer.



回答2:

You should have Controller, Service layer (interface and implementation), and DAO layer (repository interface and implementation).

Business logic you must keep inside service layer, not in the DAO. DAO shouldn't know anything about your business logic. It's response only for communication with database.