我应该在哪一层将2个实体连接在一起? [英] In which layer should I join 2 entities together?

查看:162
本文介绍了我应该在哪一层将2个实体连接在一起?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Spring MVC和常规JDBC.

I use Spring MVC and a regular JDBC.

我刚刚了解到,我应该将业务流程分为表示层,控制器层,服务层和存储库/DAO层.现在假设我有一个名为Person的实体,该实体可以具有多个Jobs. Job本身是另一个具有自己属性的实体.从我收集到的信息来看,存储库层仅管理一个实体.现在,我有一个包含另一个实体的实体.我在哪里加入"他们?服务层?

I've just learned that I should separate business process into layers which are presentation layer, controller layer, service layer, and repository/DAO layer. Now suppose that I have an Entity called Person that can have multiple Jobs. Job itself is another entity which have its own properties. From what I gathered, the repository layer only manages one entity. Now I have one entity that contains another entity. Where do I "join" them? The service layer?

假设我想得到一个person,它的job尚不为人所知(延迟加载).但是系统稍后可能会询问该特定personjob是什么.在这种情况下,每一层的作用是什么?

Suppose I want to get a person whose job isn't known yet (lazy loading). But the system might ask what the job of that particular person is later on. What is the role of each layer in this case?

如果需要在此问题中添加任何细节,请告诉我.

Please let me know if I need to add any detail into this question.

推荐答案

在典型的OOP中,对象之间的关系是通过创建关联来建立的. JobPerson之间的关联是否为多对一.然后,应将Person的属性添加到Job.另一方面,PersonJob之间的关联是一对多的,因此您可以向Person添加一组Job.如果您不想加载该人的所有关联工作,则可以将此关联映射为延迟加载.在ORM和JPA中,默认情况下使用它.

In the typical OOP the relationship between objects are made by creating an association. If the association between a Job and Person is many-to-one. Then you should add a property of a Person to the Job. From the other hand the association between Person and Job is one-to-many, so you could add a set of Jobs to the Person. You can map this association for lazy loading if you don't want to load all associated jobs of the person. This is used by default in ORM and JPA.

class Person {
 Set<Job> jobs;
}

class Job {
 Person person;
}

用于分离和解耦用于处理相同或不同对象的逻辑的每一层.

Each layer used to separate and decouple the logic used to handle the same or different objects.

另一方面,用于映射对象模型的对象在每一层上可能都不同,并且当您需要更新模型时必须转换数据.这取决于用于持久层的持久性框架的实现.有了服务层,您可以从持久层实现中抽象出来,并且如果您最近更改了持久性框架,则封装在服务层中的业务逻辑也不会改变.表示层也可能包含其自己的对象,称为视图对象,用于处理表示层的不同方面.创建,操作和表示这些对象的逻辑属于表示层,该层显然由表示框架实现.

On the other hand the objects used to map your object model could be different on each layer and you have to transform data when you need to update the model. It depends on implementation of the persistence framework used for the persistence layer. Having a service layer you can abstract from the persistence layer implementation, and if you lately change the persistence framework the business logic that is encapsulated in the service layer wouldn't change. The presentation layer might also contain its own objects known as a view objects used to handle different aspects of the presentation layer. The logic of creating, manipulating, and presenting these objects belongs to a presentation layer which is obviously implemented by the presentation framework.

这篇关于我应该在哪一层将2个实体连接在一起?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆