我有 10 个原子服务,每个都有自己的实体。(每个服务的单独项目)
在每个服务中,我都有一个 Helper,它做的事情完全相同,但取决于它们的实体。
我想停止在每个服务上重复代码,所以我正在创建一个实用服务,它将为其他人提供该帮助,我的问题来了:
如果我在帮助程序和每个服务的实体之间有这种依赖关系,我该怎么做?
这是帮助程序的一个示例:
...
SomethingPK somethingPK = toSomethingPK(something);
SomethingEntity somethingEntity = (SomethingEntity)session.get(SomethingEntity.class, somethingPK);
somethingEntity = toSomethingEntity(something);
AnotherEntity another = new AnotherEntity();
//attribute sets
somethingEntity.setAnother(another);
...
每个服务都有自己的 SomethingPK、SomethingEntity 和 AnotherEntity 作为 Hibernate 上的映射实体。我怎样才能松开这个连接?
说得通?
今天:重复的代码如
服务ABC项目
abc.entities.AbcType (DTO)
abc.entities.AbcEntity (Entity)
abc.entities.AbcPK (Entity's PK)
abc.entities.AbcAnotherEntity (Entity)
abc.helpers.CommonHelper (Bean)
abc.helpers.CommonHelpers.createSomethingThatIsNotBusinessLogic(Session session, AbcType abc) {
...
AbcPK key = parseToAbcPK(abc);
AbcEntity abcEntity = (AbcEntity)session.get(AbcEntity.class, key);
abcEntity = parseToAbcEntity(abc);
AbcAnotherEntity abcAnother = new AbcAnotherEntity();
AbcEntity.setAnother(another);
...
}
服务XYZ项目
xyz.entities.XyzType
xyz.entities.XyzEntity
xyz.entities.XyzPK
xyz.entities.XyzAnotherEntity
xyz.helpers.CommonHelper
xyz.helpers.CommonHelpers.createSomethingThatIsNotBusinessLogic(Session session, XyzType xyz) {
...
XyzPK key = parseToXyzPK(xyz);
XyzEntity xyzEntity = (XyzEntity)session.get(XyzEntity.class, key);
xyzEntity = parseToXyzEntity(xyz);
XyzAnotherEntity xyzAnother = new XyzAnotherEntity();
XyzEntity.setAnother(another);
...
}
需要重构来实现这样的事情:
服务ABC项目
abc.entities.AbcType (DTO)
abc.entities.AbcEntity (Entity)
abc.entities.AbcPK (Entity's PK)
abc.entities.AbcAnotherEntity (Entity)
服务XYZ项目
xyz.entities.XyzType
xyz.entities.XyzEntity
xyz.entities.XyzPK
xyz.entities.XyzAnotherEntity
服务公用事业项目
utilities.CommonHelper
utilities.helpers.CommonHelpers.createSomethingThatIsNotBusinessLogic(Session session, ?Type type) {
...
//same code prepared to deal with any entity, entityPK or type.
...
}