views:

667

answers:

6

A few weeks ago I asked the question "Is a PHP, Python, PostgreSQL design suitable for a non-web business application?" http://stackoverflow.com/questions/439759/is-a-php-python-postgresql-design-suitable-for-a-business-application

A lot of the answers recommended skipping the PHP piece and using Django to build the application. As I've explored Django, I've started to question one specific aspect of my goals and how Django comes into play for a non-web business application.

Based on my understanding, Django would manage both the view and controller pieces and PostgreSQL or MySQL would handle the data. But my goal was to clearly separate the layers so that the database, domain logic, and presentation could each be changed without significantly affecting the others. It seems like I'm only separating the M from the VC layers with the Django solution.

So, is it counterproductive for me to build the domain layer in Python with an SQL Alchemy/Elixir ORM tool, PostgreSQL for the database layer, and then still use Django or PHP for the presentation layer? Is this possible or pure insanity?

Basically, I'd be looking at an architecture of Django/PHP > Python/SQLAlchemy > PostgreSQL/MySQL.

Edit: Before the fanboys get mad at me for asking a question about Django, just realize: It's a question, not an accusation. If I knew the answer or had my own opinion, I wouldn't have asked!

+3  A: 

You've effectively got a 3 layer architecture whether you use Django's ORM or SQLAlchemy, though your forgo some of the Django's benefits if you choose the latter.

Jeff Bauer
+8  A: 

You seem to be saying that choosing Django would prevent you from using a more heterogenous solution later. This isn't the case. Django provides a number of interesting connections between the layers, and using Django for all the layers lets you take advantage of those connections. For example, using the Django ORM means that you get the great Django admin app almost for free.

You can choose to use a different ORM within Django, you just won't get the admin app (or generic views, for example) along with it. So a different ORM takes you a step backward from full Django top-to-bottom, but it isn't a step backward from other heterogenous solutions, because those solutions didn't give you intra-layer goodness the admin app in the first place.

Django shouldn't be criticized for not providing a flexible architecture: it's as flexible as any other solution, you just forgo some of the Django benefits if you choose to swap out a layer.

If you choose to start with Django, you can use the Django ORM now, and then later, if you need to switch, you can change over to SQLalchemy. That will be no more difficult than starting with SQLalchemy now and later moving to some other ORM solution.

You haven't said why you anticipate needing to swap out layers. It will be a painful process no matter what, because there is necessarily much code that relies on the behavior of whichever toolset and library you're currently using.

Ned Batchelder
For the long term development of any system, hasn't the rise of browser-based interfaces shown us a need to separate the domain layer from the presentation?
wrburgess
Also, I'm not criticizing Django ... I'm asking a question to make sure I'm looking at it correctly.
wrburgess
The domain layer shouldn't be aware of the presentation layer, indeed. But the presentation layer is necessarily dependent on the domain layer.Now I don't see how this relates to Django. The domain layer part (views) is not tied to presentation (views and templates).
bruno desthuilliers
+1 Excellent summary.
Carl Meyer
+3  A: 

Based on my understanding, Django would manage both the view and controller pieces and PostgreSQL or MySQL would handle the data.

Not really, Django has its own ORM, so it does separate data from view/controller.

here's an entry from the official FAQ about MVC:

Where does the “controller” fit in, then? In Django’s case, it’s probably the framework itself: the machinery that sends a request to the appropriate view, according to the Django URL configuration.

If you’re hungry for acronyms, you might say that Django is a “MTV” framework – that is, “model”, “template”, and “view.” That breakdown makes much more sense.

At the end of the day, of course, it comes down to getting stuff done. And, regardless of how things are named, Django gets stuff done in a way that’s most logical to us.

hasen j
+6  A: 

Django will happily let you use whatever libraries you want for whatever you want to use them for -- you want a different ORM, use it, you want a different template engine, use it, and so on -- but is designed to provide a common default stack used by many interoperable applications. In other words, if you swap out an ORM or a template system, you'll lose compatibility with a lot of applications, but the ability to take advantage of a large base of applications typically outweighs this.

In broader terms, however, I'd advise you to spend a bit more time reading up on architectural patterns for web applications, since you seem to have some major conceptual confusion going on. One might just as easily say that, for example, Rails doesn't have a "view" layer since you could use different file systems as the storage location for the view code (in other words: being able to change where and how the data is stored by your model layer doesn't mean you don't have a model layer).

(and it goes without saying that it's also important to know why "strict" or "pure" MVC is an absolutely horrid fit for web applications; MVC in its pure form is useful for applications with many independent ways to initiate interaction, like a word processor with lots of toolbars and input panes, but its benefits quickly start to disappear when you move to the web and have only one way -- an HTTP request -- to interact with the application. This is why there are no "true" MVC web frameworks; they all borrow certain ideas about separation of concerns, but none of them implement the pattern strictly)

James Bennett
Thanks for the answer. This makes a lot of sense. However, since I'm NOT developing a web-based application, that's where my question came in. I need 3 separate layers because not every interaction is necessarily an HTTP request.
wrburgess
Django is just Python. There is no problem whatsoever having Django code that accesses the model layer without being tied to an HTTP request/response cycle.
Carl Meyer
@Carl True, but most of the utility of Django is in being an integrated web stack (though I've used it on top of OS X Core Data stores occasionally, for fun).
James Bennett
A: 

There's change and there's change. Django utterly seperates domain model, business rules and presentation. You can change (almost) anything with near zero breakage. And by change I am talking about meaningful end-user focused business change.

The technology mix-n-match in this (and the previous) question isn't really very helpful.

Who -- specifically -- benefits from replacing Django templates with PHP. It's not more productive. It's more complex for no benefit. Who benefits from changing ORM's. The users won't and can't tell.

S.Lott
@wrburgess: mix-n-match is inevitable? Does that mean all technologies are bad and must inevitably be replaced? Or does that mean that all technologies inevitably devolve to PHP?
S.Lott
+3  A: 

You seem to be confusing "separate layers" with "separate languages/technologies." There is no reason you can't separate your concerns appropriately within a single programming language, or within an appropriately modular framework (such as Django). Needlessly multiplying programming languages / frameworks is just needlessly multiplying complexity, which is likely to slow down your initial efforts so much that your project will never reach the point where it needs a technology switch.

Carl Meyer
I guess my concern is that if I develop the domain layer in Django, then I am restricted to a presentation layer that is likewise Django. But if I develop the domain layer with Python, I can change the presentation or model pieces down the road with whatever technology I choose?
wrburgess
Except that's not true. It's not even coherent. Django is Python. Using Django's ORM (which is Python) is no different from using SQLAlchemy (which is Python). They each have strengths and weaknesses, but one is not inherently more pluggable than another.
Carl Meyer