views:

456

answers:

3

What flaws do you see in the following design, (and what would be your ideal architecture suggestion) for a typical web 3-tier application?

My current blueprint approach is very roughly this (assuming Java, Spring, Hibernate, JSP)


Controller

Stateless, potentially wrapped with a read only transaction (to avoid lazy init exceptions), get's entities from persistence storage via the service only, passes them to the view as the model. Conducts business logic on them (should the BL be in the service layer only?), passes back to the service layer for persistance if required.

Pros: For readonly transaction wrapping - only one connection, no redundant hits for the same persistent entity, utilizes query cache better, service layer shouldn't "know" request parameters, or required init graph span, avoid lazy init exceptions.

Cons: The read-only transaction approach can be risky, controllers are not the ideal Business Logic hanging place... very hard to do JUnits (your input is a request...)


View

Non transactional (access to non lazy collections / members will result in a lazy init exception)

Pros:

  • The view author shouldn't affect by mere dot notation the performance of the application (e.g. cause N+1 selects due to lazy initializing a large collection.

  • Also in disconnected clients (Flex, or other Rich Clients) lazy initialization remotely is either non supported, or just not a smart thing to do

Cons: the controller / service / dao must prepare carefully the right graph of entities for the view, and may be overshooting (performance) / undershooting (lazy init exception). a myriad of server side methods can cause clutter as there is a Cartesian product to the number of permutations an entity graph can be initialized


Model

Using the persistent objects as is, (no data transfer objects), state is saved in the session.

Pros: no need to rewrite POJOs, reuse of existing entities, session state is more secure than hidden fields state handling.

Cons: Bad for disconnected frameworks, risk of saving stale disconnected objects, risk of locking issues, overriding other's data, requires optimistic locking sometimes.


Service

Transactional, doesn't know the request scope, calls a DAO layer for actual persistence storage access. this is where the BL should classically be, but it seems the BL leaks to the controller side over and over.


DAO

Contains atomic persistence storage facade, ignorant of the BL, or any context


Finally, the question:

What would you fix in the above architecture?

Do you think (like me) it is a quite common approach (with some minor differences, such as open session in view, etc)? Or is it the first time you see it and I'm doing something terribly wrong (or right)?

How do you solve it in your applications? Do you use your entity POJOs also for your model and view? or do you wire it up to simpler UI beans (all fully initialized and secure)?

This may be a subjective question, but I'm sure there are clear best practice design patters that cluster to one, two or three max general "religions".

+2  A: 

Overall, it seems like a very good architecture. If you haven't already read it, I would recommend Martin Fowlers Patterns of Enterprise Application Architecture, which describe every subject in your question.

It is not clear from the question how large an issue you expect performance to be. In my experience, the performance bottlenecks are rarely where you think they are, and the sooner you find them, the easier it is to change the architecture to match.

You are right that testability is a major concern. I have used Martin Fowlers Passive View-pattern with some success. You should also take a look at Supervising Controller, from the same site.

Søren Elverskov
+1  A: 

Your above approach sound good.

But I think you should use UI-Beans. Of course this UI-Bean should be effectively immutable. As soon as it is created its state (and the encapsulated domain object) should not be changed.

Very simplified example:


class UIBean {
  DomainObject o;

  public String getDescription(){
     return trimToSummaryText(o.getDescription());
  }

  private static String trimForSummaryText(){
     ....
  }
}

Main pros:

  • Template code tends to get cleaner and consise. Your Frontend-Developer will be happy about this.
  • You don't tend to add frontend-specific helper methods to domain object classes.
  • Grouping of different domain objects or view-beans is possible (ui-bean could have multiple fields). Encapsulation of lists is especially nice here.

Yes, it involves more java-classes for that. But this abstraction layer is nearly always fine as soon as your webapp and pages grow.

manuel aldana
This approach is the safe way to go I assume, but it feels so wrong :) why use POJOs for your domain objects if they are "dangerous" and unsecure? Isn't there a way to automate UI-Beans? e.g. using annotations to create automatic UI facade of your domain objects?
Ehrann Mehdan
i think wrapping/decorating objects to serve a dedicated use-case (in this case the frontend) is ok. but you are right sometimes the wrapping methods just delegate completely to the domain object. this glue-code seems to be the trade-off (but in my view most often outweighed the benefits). your asked about generating beans: this would be easier with dynamic typed languages where a lot of nice reflective stuff can be easily done during runtime. i avoid this in java. adding a generative approach (either code generation or reflection) with java is very often a pain and a too big trade-off.
manuel aldana
+1  A: 

Super unless doing a SOFEA style front-end, which basically gets rid of the Controller part in the above architecture.

The front end is entirely contained on the client, which directly invokes REST or SOAP services returning JSON or XML. That seems to solve 100% of the problem with transforming domain objects for display!!!!

Perhaps, some suggest, the reason there's no clean solution to the N-tier architecture described above is because it's just plain wrong.

Links

  1. http://raibledesigns.com/rd/entry/sofea%5Falso%5Fknown%5Fas%5Fsoui
  2. http://www.theserverside.com/news/thread.tss?thread%5Fid=47213
  3. http://wisdomofganesh.blogspot.com/2007/10/life-above-service-tier.html

My current project uses a somewhat outdated N-tier architecture with Data Transfer Objects. The DTOs are unnecessary because the application is not distributed and never will be. The one benefit (which is not worth it IMO) of using the DTOs is that it forces a clean interface for the business methods - the View components can traverse the object graph on the pseudo-models however they wish - no lazy-initialization exceptions can be thrown.

One of the architectural pain-points I see in our architecture is that the business interfaces are two damned complicated. It seems as if we need a meta-business interface to encapsulate all of the little business interfaces. Indeed, this ends up happening where one service ends up calling three other services to do its work.

The controllers (in our case, Struts 1.2 Action classes) end up calling any combination of these ultra-fine grained or coarse-grained business components. In most cases, sadly, developers unwittingly or lazily code a variety of what should be business logic in the controller classes. I scream every time I look at one of these three-hundred line Action methods!!!!

The SOFEA approach seems to offer a much cleaner approach. AJAX allows even web applications running on browsers to have their front end coded using the proper MVC pattern, which is good for both users (by offering a more dynamic UI) and developers (allowing them to code proper MVC).

The UI is completely decoupled from the reusable business logic. Is the GUI thick or thin? It really won't matter -they'll be coded in basically the same way.

SOFEA / SOUI is new to me and I've never tried it, but I've been reading about it lately and I thought I'd share my thoughts.

LES2
I think this is the future, but it is hard to integrate on existing projects
Ehrann Mehdan