Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
205 views
in Technique[技术] by (71.8m points)

java - What's the best approach to divide model and actions into classes in MVC pattern

Let's say I have a class Employee with a huge amount of fields. And I have a lot of actions related to db with it like CRUD, filter etc.

In MVC pattern all of these stuff could be placed into one class of Model part. But again, what if I have a lot of fields and a lot of actions. How to properly split to classes the basic object staff (fields, basic methods: getters & setters, toString etc.) and the actions. Like Employee and EmployeeActions? Or any best approach? Need your experience)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

In principle, the domain model, e.g. the model layer, e.g. the "model", should be divided into the following components:

  • Entities, e.g. domain objects (like your Employee) and value objects. Each of the entities contains not only the specific data, but, most important, the required behaviour related to it (ONLY).
  • Data mappers abstractions (like EmployeeMapperInterface). Their implementation (like EmployeeMapper) should NOT be part of the domain layer. The data mappers are the objects responsible with the transfer of data between the entities and database (or any other persistence layer). So they are the only components which know how to communicate with the database through its API. E.g. they contain the SQL statements/calls. Such statements should NOT be in any way part of the entities, since the same entities can be used by multiple applications, and not all applications require db access, or the same db access as the other. The entities should not even know about any persistence at all.
  • As an optional abstraction layer: repository abstractions (like EmployeeRepositoryInterface, or EmployeeCollectionInterface, or EmployeesInterface). Their implementation (like EmployeeRepository, or EmployeeCollection, or Employees) should also NOT reside in the domain layer, but outside its boundaries. They are constructs having the role of hiding the type of persistence from the model components and have two functionalities/characteristics: 1) They transfer the entities from the domain model to the data mappers, in order to update the db data and 2) They store the collection of entities "fetched" from the db using the corresponding data mappers, making it available to the domain layer.
  • Services, as part of the service layer (like AuthorizationService). There can be application services and, if needed, domain services (which are used by the former ones). The services handle all other domain layer components in order to properly respond to the user requirements. From the user's point of view, they are the only gateway to the domain model.
  • Abstractions of external services (like MailServiceInterface, or PaymentServiceInterface, or PrintingServiceInterface). Their implementations (like ExampleMailer, or PayPalPayment, or PdfPrinter) lie outside the domain model.

Resources:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...