Three-tier architecture in Ruby and Ruby on Rails - ruby

I'm an ASP.NET MVC developer who decided to learn Ruby and Ruby on Rails. I already know something and created a web site on RoR. Developing on ASP.NET MVC, I have always used a three-tier architecture: data layer, business layer and UI (or presentation) layer.
Trying to use this approach in a Ruby on Rails application, I found that there was no information about it (or maybe I just couldn't find it?).
Perhaps someone can suggest me how to create or use three-tier architecture on Ruby on Rails?
P.S. I use ruby 1.9.3 and Ruby on Rails 3.2.3.

I would suggest following Ruby on Rails (RoR) style while making RoR applications. Rails way of seeing MVC architecture does not quite fit into Asp.net 3 Tier architecture.
UI (Presentation Layer | View)
These two follow the same logic. No major differences.
Controller (Business Layer | Controller)
Both Business Layer and Controller receive requests from UI and they send back responses. In Asp.net Business Layer takes care of validation and business logic. But in Rails, validations and business logic belong to Model.
Model (Data Layer | Model)
Rails' Model does more than Asp.net's Data Layer. Model deals with business logic and validations. Data Layer and Model take care of data transfer to storage.
When moving from Asp.net to RoR, try to keep your controllers thin. RoR sets serious constraints on how you structure your web application. And once you'll stick with those, you'll make more professional RoR apps.

Ruby on Rails is a three tier (MVC) architecure. In rails the data layer is the called the Model, the buisness layer is called the Controller, and the user interface layer is called Views.
Here are a couple good places to start:
rails guides,
rails tutorial for rails 3.2

After 4+ years, this is still a relevant question. It appears there's a confusion around what people mean by application tiers.
Previous answers are of course correct when you think of MVC tiers, and when you define an application as being 3-tier if it has proper models, views and controllers. Of course Rails is by default 3-tier in this sense, with notable differences to ASP.NET MVC as pointed out by others.
However, I have a feeling that the original question referred to how things are deployed (I may of course be wrong, but people looking at this question may still be looking for this). Enterprise applications often have the requirement to be three tier in a sense that a web application (presentation) can only talk to services (business logic), which can only talk to a database due to network level restrictions, and for security reasons.
In this model, the front-end web application performs security functions (protection against several types of attacks like CSRF or Clickjacking, maybe session management, etc.), and presents any data read from services on application servers. These application servers are not directly accessible by endusers, often trust web servers (or not, depending on the security model), and persist their data in databases, which cannot be accessed directly by web servers. In a Windows environment, web, app and database servers are typically in different domains.
Rails, as described in pretty much all tutorials, is a web and app server at the same time, which does not fit the latter model, and there really are not much resources available on how to do this properly. However, especially with Rails 5, it is very easy to turn Rails into a proper service to be used as an application server with an API (see the API-only Applications Guide), and it is also possible to create another Rails application for the web (presentation) tier. You will have to solve quite a lot of things for yourself though, from authentication between web and app to making service queries from web to app, especially now that ActiveResource is kind of retired.

Related

what is the difference between 3 tier architecture and a mvc?

what is the difference between 3 tier architecture and a mvc ?
Are they same?
Both have 3 layers i.e model, views and controller
Comparison with the MVC architecture
At first glance, the three tiers may seem similar to the
model-view-controller (MVC) concept; however, topologically they are
different. A fundamental rule in a three tier architecture is the
client tier never communicates directly with the data tier; in a
three-tier model all communication must pass through the middle tier.
Conceptually the three-tier architecture is linear. However, the
[model-view-controller] MVC architecture is triangular: the view sends
updates to the controller, the controller updates the model, and the
view gets updated directly from the model.
Source: http://en.wikipedia.org/wiki/Multitier_architecture#Three-tier_architecture
MVC is a pattern used to make UI code easier to maintain and test. When the MVC pattern is used a larger portion of the UI code can be unit tested.
Here is a good article which describes the MVC pattern in more detail: http://martinfowler.com/eaaDev/uiArchs.html
3 tier architecture is a pattern used for a completely different reason. It separates the entire application into meaningful "groups": UI, Business Logic, Data Storage.
So 3 tier application refers to all code in the application. The MVC pattern is a pattern used in the UI tier.
Here is a good article on the 3 tier architecture: http://dotnetslackers.com/articles/net/IntroductionTo3TierArchitecture.aspx
For further information you can search the internet and find a gazzilion articles on both subjects.
Their are similar in a way, like:
3 tier divides the whole app in: UI, logic and data
MVC divides the UI part in: view (kind of UI of the UI), model (data) and controller (logic)
But the difference comes from how the tiers communicate with each other:
3-tier: anything goes through the logic tier (a->b, b->c and c->b, b->a)
MVC: they communicate 2 by 2, in a triangular way. (a->b, b->c, c->a)
http://en.wikipedia.org/wiki/Multitier_architecture
Briefly, in 3-tier architecture, presentation tier never communicates directly with data tier. In MVC, the relation among model, view, and controller is triangular. Two of three can communicate each other
In three tier solution the UI is separated from the business tier to make sure that the UI designer who is concerned with the look and feel is not confused with the heavy programming which is left to the programming professions.
This architecture (three tier) is essential when a large number of people are involved in producing a large application.
The main difference between both is:
A “tier” in this case can also be referred to as a “layer”. The three tiers, or layers, involved include:
A Presentation Layer that sends content to browsers in the form of HTML/JS/CSS. This might leverage frameworks like React, Angular, Ember, Aurora, etc.
An Application Layer that uses an application server and processes the business logic for the application. This might be written in C#, Java, C++, Python, Ruby, etc.
A Data Layer which is a database management system that provides access to application data. This could be MSSQL, MySQL, Oracle, or PostgreSQL, Mongo, etc.
MVC architecture is triangular: the view sends updates to the controller, the controller updates the model, and the view gets updated directly from the model
first of all, tier is for physical deployment, what you mean maybe layers, and MVC is a framework for the presentation layer, that's all
I recommend you try building a web app with some technologies that use MVC and three tiers to really understand the differences between them, I suggest you use ruby on rails with a templating engine since rails is one of the most known MVC frameworks

html5 and webservices versus mvc3

At my company we have a discussion as to in what way our future web applications should be build. We currently have 2 groups of developers who agree on several things. We all want to use html5 and css3 and jQuery. We mostly agree that web services will be very important.
However, one group advocates the use of mvc because it would add more structure? to the project. The other half (to which I admit I belong to), sees no reason not to consume the webservices directly from the client, forgoing mvc controller views etc.
To me adding mvc will only add another performance burden on the server that could be shifted to the client and structure could also be added to non mvc projects, though it might ask a little more effort from the developers.
At the risk of starting a flamewar. I would like to hear some arguments for either side from outside the company.
You are having the wrong discussion. HTML5, CSS and Javascript (and JQuery) are client side technologies. ASP.NET MVC is a server side technology. You still need code on the server that sends and retrieves data from the database. The next version of MVC (4, beta) will have Web API which is an excellent solution to create those webservices with.

Moving to a New Stack - AJAX, REST & NoSQL

All,
I'm beginning to explore what frameworks (open source) and tools for building web applications. What should I select and learn for the following layers,
Layer 1
Client side JavaScript / AJAX library or framework that will invoke REST style services provided by layer-
2
Layer 2
Provides a framework to rapidly create REST style services out of existing applications and out of a NoSQL document oriented database provided by layer-3. I need this layer in cases where I need to expose REST style services out of my traditional apps and RDBMS.
Layer 3
Which NoSQL to use - CouchDB or MongoDB that would work well with layer-2?
Will I need a MVC framework like RoR or a web/component framework like Wicket? Am I missing anything?
I also need recommendations for which tooling/IDE (and associated plugins) for the development environment. Thanks in advance for your answers/thoughts.
We've had pretty decent luck using a Java stack:
For the presentation, we use jQuery and jQueryUI, with Freemarker for XHTML/CSS templating, including to invoke REST web services through various UIs.
Restlet (www.restlet.org) is a wonderfully rich framework for crafting REST web services in Java. We decided to use it on a major product after it was strongly recommended to us by the engineering director of a top 10 e-commerce site in the US. And everything he said about it was true.
Unless you know you're going to face a really large amount of write volume, you're probably best off using one of the tried and true SQL databases supporting ACID transactional guarantees. We used Oracle, then switched to PostgreSQL, using the MyBatis (formerly iBatis) SQL Mapper to shield our code from the details of the database. With the advent of 64-bit addresses and scads of inexpensive DRAM, plus SSDs, these old workhorses do scale quite high.
If you are anticipating very large amounts of writes, by all means consider a so-called "NoSQL" database. I heard very good things about Vertica from the top network ops folks at a major technology company last week. MongoDB and CouchDB both look interesting. Or you may be able to leverage persistent distributed cache technology like Redis or EhCache to offload a traditional database.
The task you're trying to accomplish determines the technology you use.
If you're interested in the .NET platform, consider:
RavenDB for the data layer.
WCF Data Services for your REST services layer. Here's a guide to WCF Data Services with some great video tutorials.
If you need a web front-end, consider ASP.NET MVC or plain HTML as you see fit. jQuery and AJAX modules are well suited in both.
Arguably, ASP.NET MVC IS a REST endpoint of sorts, so you could skip the WCF services.
All the above can be leveraged with free tools, including:
Visual Studio Express
Raven DB's HTML interface at http://localhost:8080/

Reasons not to use MVC architecture for web application

In the past I have primarily built all my web applications using an N-tier architecture, implementing the BLL and DAL layers. Recently, I have started doing some RoR development as well as looking into ASP.NET MVC.
I understand the differences between the different architectures(as referenced by some other SO posts), but I can't really think of any reasons why I wouldn't choose an MVC model going forward for a new project.
Is there any reasons/times in your experience when an MVC architecture would not be suitable, or any reasons why you would choose a BLL/DAL architecture instead?
I don't think your options are mutually exclusive. You could perfectly use MVC while using BLL/DAL for your model logic.
You can implement the M part of MVC as you prefer, there is no restriction about that. Using BLL and DAL would be a valid option.
For me? the only reason I'd not use MVC is because the application I'm working on was already started in web forms. I'm not a big proponent of scrap/rewrite, but anything new I do is in MVC.
One of the factors could be the statefulness of your web application. If it's a basic web application that gets everything from the server with a few JavaScript hooks such as client side validations, then the Rails type MVC is really great. I am not familiar with MVC on ASP.NET, but I've heard it's similar to that in Rails.
If the web application is really stateful, then a better approach would be to have a dual MVC layer - one on the client side, and the other for the server. The MVC on the server will mostly concern itself with authentication, authorization, churning out data in standard formats, etc. The client side MVC will concern itself with things such as DOM events, user actions, how they affect the application state, and how/when should data be requested/sent to the server.
MVC is just a way to organize code, just as BLL or DAL would do. MVC in Rails basically hides DAL altogether by using a set of conventions. Usually business logic resides in the models itself. However, if your application demands more complex BLL where object interactions can be intricate, then there's no reason why that BLL can't peacefully co-exist with the M in MVC.
I don't use MVC only on really tiny projects consisting of ~1-2 files and ~10-20 lines of code. And they will hardly evolve into something bigger.
But if they will, it will be time to rearchitect them into a MVC ones.
The only drawback we've had is that MVC pushes you toward a html/javascript interface where rich internet applications become more difficult. For example if you want to present the user with a calendar control, you may need to roll your own since you can't drop one on from the toolbox. That said, MVC is great. When we really need RIA applications we use MVVM and Silverlight.
For certain pages, MVC can be a little overkill:
splash pages
landing pages
marketing pages that are going to be thrown away after one use
one-off's
It's easy to get wrapped up creating a beautiful MVC architecture, when a small page, concisely written, can be OK on these situations.
MVC may also be impractical if you're in time trouble, and you need something out REALLY fast. Like your marketing team is out at a conference, they're having trouble, and needs something to show in their booth this second before they lose their biggest customer.
Life above the service tier suggests you should use the MVC pattern in a way that adheres to the SOFEA principles, and watch out for "Front controller" type frameworks disguising behind the MVC acronym.
(or, you can still use them, but at least read the article, understand the differences and choose wisely).
The simple answer is, no.
MVC is all around a better architecture than your old-school n-tier architecture, for many reasons. It's the standard approach in other UI models (e.g. Swing). The only reason it took so long to make it to web applications was because it took the software community, collectively, a little while to get used to the statelessness of the web and to be able to deal with the views and controllers in a way that made sense.
Personally, I would rate it based on the complexity of the target application. MVC (or more structured approaches in general) lend themselves very well to large scale applications where design consistency and segregation of code is a benefit outweighing the cost of supporting the design.
If its a small site, or very few pages/controls, I would avoid sticking to strict design patterns. But that is just my preference.
As one poster said, you also have to consider the state of any existing applications, and your development team skills / experience.
We have already use the MVC for the Windows application,Now we need to convert that thing in the Web application we don't have any problem in any thing. We are using the Web service and every Business Logic is in the Web service.
So you can use the MVC in the web application.
M-Model(Functions and Procedure which communicate with Business logic)
V-View(Design)
C-Controller(Form Logic)
so that is no connection in the DAL,BLL and in MVC.
you can define your Business logic and use in it any where in the MVC.
That's my point of view MVC is very useful for Re-usability i prefer if your application is big then you must use MVC.
I wouldn't use MVC pattern only in the case when I have an existing desktop application built with MVP and I have to convert it to a web environment. That's because I already have written logic for presenter.
In any other case I would use MVC.
You can refer to following
http://blogs.msdn.com/b/webtopics/archive/2009/09/01/asp-net-mvc-what-is-it-and-should-i-use-it.aspx
and http://msdn.microsoft.com/en-us/magazine/dd942833.aspx#id0080017 refer "Undisputable Facts".

What is SOFEA?

I've came across this new acronym, SOFEA, apparently a new programming paradigm for web applications.
Still didn't manage to make sense of it - if someone volunteers to make it clearer for me I'll be very thankful.
SOFEA is an abbreviation for Service Oriented Front End Applications/Architecture, it refers to a web application architecture where all of the UI functionality is provided by javascript or flash-based logic. It doesn't interact with the web server per se to provide application level access to data, but rather interacts with web services through AJAX. Essentially, you provide all of the UI logic in a single (or few) web pages. These pages work with web services to access and update data.
Reference at InfoQ
AKA SOUI by the way... (Service Oriented UI) ref: here
As integration on the response above, as alternative for the comunication between the two decoupled layer presentation and busines logic you can use different technics and technology as XML or Json using REST & RESTFUL paradigm or even SOAP and last but defenetly not least websocket.

Resources