Java & Spring: Impressions From a non coding perspective
I am a bit funny when it comes to learning technology, I read about it away from the computer, because otherwise there would be a lot of stuff that I would be doing that would interfer with the process of actually reading a book. Anyway, I am reading Agile Java Development right now, and I got to the point where the author shows how to build a simple page.
The sheer amount of steps required is scaring me. What is more worrying is that the author keeps saying how simple this is compare to other approaches (which I presume Java guys would be familiar with). I keep comparing it to MonoRail + Windsor, and I can't believe that this is so complex.
From an architecture point of view, it is very separated, but from an implementation point of view, it looks messy. Here is the piece of code that caused this post. It is meant to put the current object for the request in the context:
{
if (request.getParameter(TID) != null
&& request.getParameter(TID).trim().length() > 0)
return timesheetManager.getTimesheet(Integer.parseInt(request
.getParameter(TID)), false);
Timesheet timesheet = new Timesheet();
Employee employee = (Employee) applicationSecurityManager
.getEmployee(request);
timesheet.setEmployeeId(employee.getEmployeeId());
timesheet.setStatusCode("P");
timesheet.setPeriodEndingDate(DateUtil.getCurrentPeriodEndingDate());
return timesheet;
}
I am not a Java guy, and I never did anything beyond an applet in Java, so I may be missing something, but is this considered to be a good form of writing your code?
Issuses that I have with the code:
- Dealing directly with the request, extracting inputs, going to the database, etc. Those are infrastructure style stuff (and fairly repetitive, I would imagine). I want my controller to deal with business use case.
- The whole model seems to be based on overriding specific methods calls, while I appriciate the design, to me it seems like it unnecessarily complicating things by moving related functionality to separate methods, when it could be in the same place. (The timesheet object returned from this method is passed to another method down the line, for instance.)
Am I missing something?
(Hm, angering the Java people, angering the P&P people, all I have left now is to anger the Ruby guys and I am set. I shouldn't worry about that, I have a safe place to hide, Cell #6.)
Comments
Ayende,
This is not the best example. First o all, it looks like the controller is being overloaded to serve as both adding and editing Timesheet objects. Generally, you would write a single CRUD controller to handle those types of requests so you did not have to code that stuff everytime. Second, normally Spring would handle the binding of form parameters directly to the backing object and there would not be all the extra code. However in this example, they are retrieving an object from applicationSecurityManager (that has nothing to do with the request besides they are probably getting the principal from the request), and setting some parameters on the new object they are creating.
You might want to check out some better examples of databinding and Spring to get a better appreciation for the web framework. http://www.springframework.org/docs/reference/mvc.html shows a couple, but does not nearly capture the power of the binding. Binding dropdowns direclty to their entity objects and collections of elements directly to collections without code is something I do often.
Hey Ayende!
do you know anything similiar to the CastleProject framework for Java?
I know that there is Grails, but it is for Groovy.
I like so much CastleProject, but we have some project where we need to be tied to Java...
by the way: very nice blog ;-)
Paolo
@Paolo,
As I said, I am not a Java guy, so I am not very familiar with the stuff over there.
Hammett (lead for Castle) was on the Avalon project, but I don't think that they are even on the same spirit, beyond that, I don't know.
@Ayende
Ya think that's bad?
I've had the pleasure of going through a J2EE course in Oracle University.
The OC4J (which is their J2EE server implementation) is so messy, it's unbelievable.You have xml files, a complex project structure, and much more, all for the sake of running your first "hello world" rendering servelet.
Now, remember that servlets aren't thread safe (it's a kinduva singleton) and you can start talk about repeatitive code.
yuck.
btw, AFAIK, websphare is not much better. JBoss is probably a lot more developer friendly due to its OS nature, but I know very little about it.
I really do not intent to go after any J2EE people. I just pity them.
Nothing in that code thrills me. It's been a while since I've done Java (my job took me the direction of .NET about 2 years ago), so I'm not sure if there's a particular framework or approach being implied in the rest of the code, but if so, I don't think I like it.
I don't remember anything about spring implying a design like this. IIRC, I'd be using Spring's form binding to get a form object more or less automatically from the lower-level servlet request, and then passing from into a controller where I could coordinate the activities within the domain model (in a more easily unit-testable fashion).
In short, this isn't code I'd be surprised to see in a production business application, but I'd want to refactor it to something cleaner (unless I'm missing something about the rest of the context). I wouldn't worry too much about this being the recommended approach. There's a lot of .NET resources out there that laud the code-behind as the best place to implement your controller logic, but I'm certainly not interested in that approach.
Rob,
What worried me is that this is in a book about agile java, which is not something that I would expect to see.
If there is an agile .Net book that lauds the code behind, I have yet to read it.
Hah, who am I to talk. To try and refresh my memory, I just peeked at the code for a sample application I was writing to explore this stuff, and have structurally identical implementation of formBackingObject...I have no defense. :-)
I seem to recall it working fairly well, but I wasn't nearly well versed in building real, meaningful unit tests yet. If I had any time, I might consider taking another crack and seeing how testable the whole thing is.
I guess this means I'm a .NET guy now. :-)
@Paolo
take a look at Stripes http://mc4j.org/confluence/display/stripes/Home - a java MVC web framework very cool and very close to monorail...
Makes you apreciate the RoR vs Java ads even more ->
http://www.railsenvy.com/2007/5/14/ruby-on-rails-commercial :))
When you're done with the Agile Java grab a copy of Agile Web Development with Rails. I really do appreciate what MonoRail brought to the table for the .NET web stack but even then it still feels heavy compared to Rails.
Comment preview