|
#51
|
||||
|
||||
|
Quote:
Code:
employeeDao.save(employee); //this one is questionable. Quote:
Code:
EmployeeService{
void fire(Employee employee){
employee.fire();
employeeDao.save(employee);
deskDao.save(employee.getDesk());
}
}
Quote:
Anyway, there are many ways to deal with lazy loading problems - OpenViewSession is the first step. Another way is to use You will still have problems at some points, but there are many ways to solve them. One very elegant way of doing it is to use AOP: http://jroller.com/page/cenkcivici?e...s_to_hibernate Also, if you have so big problem with lazy initialization, you can have session-per-conversation: http://www.hibernate.org/42.html Anyway, there a lot of information about lazy initialization problems out there. Quote:
Last edited by igorstoyanov; Dec 1st, 2005 at 08:58 AM. |
|
#52
|
||||
|
||||
|
Quote:
Quote:
![]() Quote:
Quote:
Last edited by Alarmnummer; Dec 1st, 2005 at 10:45 AM. |
|
#53
|
|||
|
|||
|
Quote:
dino |
|
#54
|
|||
|
|||
|
Quote:
Code:
interface EmployerDAO{
void save(Employer employer);
}
Code:
class HibernateEmplyerDAO extends SomeSpringClass implements EmpoyerDAO{
void save(Employer employer){
getHibernateTemplate().save(employer); //this will save the desk object as well.
}
}
Code:
class SomeOtherImplementationEmplyerDAO implements EmpoyerDAO{
void save(Employer employer){
someSaveMethodForEmployer(empoyer);
someSaveMethodForDesk(employer.getDesk());
}
}
Persistent Logic in Domain Objects When you code according to OOP, you should think about object behavior. So, if you should have “fire” behavior, then you have: Code:
Employee{
void fire(Employee employee){..}
}
Code:
employeeDao.save(employee); Code:
EmployeeManager{
boolean isEmployeeForFire(Employee employee){
employee.fire();
//do other stuff (to count the employees that are left for example;
//if number of employee/desk are less then expected
return false;
else
return true;
}
}
Of course, you can again say you can avoid this and there is nothing wrong with persistence calls inside domain objects. I think that that’s why many of the people have a lot of problem with lazy initialize. The persistence mechanism should be implemented in DAO and should be performed in service with transaction managed when needed. Quote:
Anyway, I think that the two framework Spring and Hibernate almost saved java and J2EE right now. They make thing simple, very productive and pleasure to work with. I am kind of surprised to see so many people having problems with Hibernate overall architectural design. Last edited by igorstoyanov; Dec 1st, 2005 at 08:57 PM. |
|
#55
|
|||
|
|||
|
Quote:
Dino |
|
#56
|
||||||||
|
||||||||
|
Since lazy loading is mostly Hibernate problem, I will start with this old post (2003) when Spring wasn’t very popular. In this blog, Gavin (I don’t want to start who said what) has expressed his opinion about this:
http://raibledesigns.com/page/rd?anc...ession_in_view Quote:
Quote:
http://www.springframework.org/docs/...iewFilter.html Quote:
Quote:
http://www.jroller.com/page/cardshar...n_view_pattern Some exerts: Quote:
Quote:
Anyway, this will very depend on the size of your object graph and how many of the objects in the graph you will use in a single request. Quote:
In the past, I also was raving about Big Upfront Design Architectures or something like that. I used DTO – not only for remoting but for normal web application to completely decouple the view from the domain and things like that. I saw a lot of inefficiency, lost productivity and most importantly, a lot of bugs because of having to manage so much repeating code around. I violated basic principle like DRY, good OOD with creating object that are actually data holders without ANY behaviors, but most importantly, I didn’t bring too much business value to my customers (I am a consultant) with the type of applications that are “justified from an architectural point of view”. Now, I am questioning what a good architecture actually is. (BTW, not only me: Martin Fowler's keynote on OOPSLA 2005- "does good design even matter?": http://ivan.truemesh.com/archives/000547.html ) So, I would say that if the architecture works well for your needs, it is easy to be refactored or modified (this totally excludes repeating code in almost every layer) and bring business value to the customer/user, then this sounds like a good architectural design to me. Another good thing about Agile is TDD. One of its principles is not to write any code before you first write the test(specification) for the functionality(behavior) that is needed from your object. Try writing a test specifying behavior for DTO! Quote:
And yes, in some cases Open View Session would not be a good solution. In other application DTO probably will make sense (probably as properties in controllers, page beans or something similar but not as pure form of DTO). However, I feel that java community starts to loose a lot because of this over-engineering in every possible aspect. I will probably repeat myself but frameworks like Spring and Hibernate were created about simplicity. Let’s try to keep this spirit. |
|
#57
|
|||
|
|||
|
Quote:
Dino |
|
#58
|
|||
|
|||
|
Some additional discussion on TheServerside.com: Spring2 vs Anemic Model http://www.theserverside.com/news/th...hread_id=38047
|
|
#59
|
|||
|
|||
|
Quote:
|
|
#60
|
|||
|
|||
|
Interesting thread and a lot of good replies. I'm hoping some of the folks on this thread could also comment on the approach I've taken to the whole problem of seperating domain layer and persistence layer using Spring, without having to inject DAOs into domain objects or having to call DAO persistence methods from the service layer.
Would appreciate some feedback on the same. My approach is published here - http://jroller.com/page/funwithjava?...model_solution Thanks Amit |
![]() |
| Thread Tools | |
| Display Modes | |
|
|