Disadvantages of n-layered architectures

Most of the people use n-layered architectures in their application developement. There are lots of advantage for n-layered architecture but I want to talk about disadvantages. actually it's not a good idea to design a somplex architecture for every problem , you don't need to apply n-layered architecure in every case because it's mostly about making complex things simple whilst a complex architecture can make a system more and more hard to understand rather than making performance problems. Based on this presentation, making software via n-layered application could be cost effective and dangerous for performance. Suppose an architecture like picture below:

n-layered

 

When you make a request from your browser your request goes throw several layers and assemblies to persist your request in data base and then comes back the same way to make a change (you can simply assume the scenario like the picture!). Let's take an example about that, imagine that I'm gonna to persist the sent message(Contact) by user in "ContactUs" of n-layered architecture web site (the same example I showed in StackOverflow).

This is my action to create the Contact:

  1. [HttpPost]
  2. public ActionResult Create(ContactViewModel contactViewModel)
  3. {
  4.     var request = new CreateContactRequest(contactViewModel);
  5.     _contactService.CreateContact(request);
  6.     return View();
  7. }

 

and about the service implementation, I've done like this:

  1. public CreateContactResponse CreateContact(CreateContactRequest request)
  2. {
  3.     var response = new CreateContactResponse();
  4.     var contact = request.ContactViewModel.ConvertToContactModel();
  5.     try
  6.     {
  7.         _contactRepository.Add(contact);
  8.         _unitOfWork.Commit();
  9.         response.Success = true;
  10.         response.MessageType = MessageType.Success;
  11.         response.Message = ServiceMessages.GeneralServiceSuccessMessageOnCreation;
  12.         _logger.Log(response.Message);
  13.     }
  14.     catch (Exception exception)
  15.     {
  16.         response.Success = false;
  17.         response.MessageType = MessageType.UnSuccess;
  18.         response.Message = ServiceMessages.GeneralServiceErrorMessageOnCreation;
  19.         _logger.Error(exception.Message);
  20.     }
  21.     return response;
  22. }

  

And  finally in repository layer, I have this generic Add method:

  1. public void Add(T entity)
  2. {
  3.     SessionFactory.GetCurrentSession().Save(entity);
  4. }
  5.  

As you can see there are lots scenario in this example such as converting ViewModel to Model, using request and response pattern in Service, injecting the interfaces,  adding to the repository, committing by UnitOfWork and blah blah. But you I can also do all of above with just a simple action method:

  1. [HttpPost]
  2. public ActionResult Create(Contact contact)
  3. {
  4.     SessionFactory.GetCurrentSession().Save(contact);
  5.     return View();
  6. }

 

You know the above implementation on an action is not the best solution when your application is complex and large scale, but if it is not try to use something like that as it's easy to develop and defiantly high performance.

Optimizing n-layered architectures is not easy task, But it is easy to change the content of any layer without making corresponding changes in other layers. It is more readable, maintainable, extensible, flexible, testable and is suitable for parallel development specially in team working but you know it has its own disadvantages.

If you have enough factors to break your application into layers, for example if you want to let other applications to access to your application via a service, separate your concerns;so apply to layered architecture but keep in mind that:

 

  1. This architecture leads to reduction of network traffic, faster network communications, greater reliability and greater overall performance.
  2. As layers usually live in separate assemblies so This results in high communications overhead.
  3. More layer cause more network and network bandwidth, process involved. 


Tags: Architecture


comments powered by Disqus