Chúng tôi sử dụng Spring's @Configurable (cùng với toán tử mới thông thường) hoạt động như một sự quyến rũ. Không còn nữa anemic domain models. Cuối cùng, đây là thiết kế hướng đối tượng nhiều hơn, không phải là:
Person person = new Person(firstname, lastname);
// weird
peopleService.save(person);
// good (save is @Transactional)
person.save();
Mail mail = new Mail(to, subject, body);
// weird
mailService.send(mail);
// good (send is @Transactional)
mail.send();
Chúng tôi chưa thực hiện bất kỳ so sánh hiệu suất nào. Cho đến nay, chúng tôi chỉ đơn giản là không cảm thấy cần phải làm như vậy.
EDIT: đó là cách lớp người sẽ trông như thế:
@Configurable("person")
public class Person {
private IPersonDAO _personDAO;
private String _firstname;
private String _lastname;
// SNIP: some constructors, getters and setters
@Transactional(rollbackFor = DataAccessException.class)
public void save() {
_personDAO.save(this);
}
@Transactional(readOnly = true)
public List<Role> searchRoles(Company company) void{
return _personDAO.searchRoles(this, company);
}
// it's getting more interesting for more complex methods
@Transactional(rollbackFor = DataAccessException.class)
public void resignAllRoles(Company company) {
for (Role role : searchRoles(company)) {
role.resign();
}
}
}
// the implementation now looks like this
personService.getPerson(id).resignAllRoles(company);
// instead of this
roleService.resignAll(personService.searchRoles(personService.getPerson(id), company));
Và đó là cấu hình mùa xuân:
<context:spring-configured />
<bean id="person" class="org.example.model.Person" lazy-init="true">
<property name="personDAO" ref="personDAO" />
</bean>
Lưu ý: như bạn thấy, vẫn còn có những dịch xung quanh, ví dụ để tìm kiếm các đối tượng (personService.getPerson (id)) nhưng tất cả các phương thức hoạt động trên một đối tượng được truyền (ví dụ: một người) được chuyển đến chính lớp đó (tức là person.save() thay cho personService.save (person)). Phương thức này vẫn giữ nguyên và làm việc với bất kỳ lớp truy cập dữ liệu cơ bản nào (JDBC thuần túy, Hibernate, JPA, ...). Nó chỉ đơn giản là di chuyển nơi nó thuộc về.
http://stackoverflow.com/questions/2091749/domain-driven-design-and-transactions-in-spring-environment – Bozho