0

I have been given to develop a web application that can have multiple presentation layers, currently its on the web but soon to be on desktops and other platforms. So I searched around on how to do it best. I have found that its better to use a layered approach.

I am thinking to have BLL as a web-service which could be accessed by different PLs. The BLL will be accessing DAL for data specific operations. So far so good, but I was thinking of using ASP.NET MVC for the web app. Now I am a bit confused because the "controllers" essentially contain the business logic, right. Is it a good option ? If i do follow the same path, using MVC and the above mentioned layers, my controllers wont necessarily contain the BLL but will only be, kinda dummies.

Is it the right way to do it?

intoTHEwild
  • 448
  • 7
  • 24

1 Answers1

3

These are the recommended layers:

  • Presentation (MVC, WPF, Whatever): contains only presentation logic (never includes business logic) the controller only handle communication with application/services layer to co-ordinate communication.

  • Distributed Services (Remote Facade): as you will have many clients, some of them would be windows apps and others are web apps, it is recommended to create a remote service layer (WCF services or web services) which exposes business layer to consumers (preferable send and receive DTOs).

  • Application Layer: a layer which handles the communication with domain layer, and co-ordinate transaction logic and technical services, and if you are using DTOs it translates domain objects into DTOs and vice versa

  • Domain Layer: contains entities and value objects, this is the core of business logic designed in terms of object oriented domain objects encapsulating data and logic. it also could contains repository interfaces if using repository pattern. and domain services for logic that does not fit into a single entity.

  • Data Access: using ORM like NHibernate or EF or whatever data access techniques to map entities into database tables.

  • Infrastructure / Common: infrastructure code and cross cutting technical services like logging

I will try to give a tiny example about each layer: a hypothetical incomplete example say you want to activate a purchase order

Presentation Layer (MVC):

public class PurchaseOrderController
{
  public ActionResult ActivateOrder(int id)
  {
    var response = _orderManagementService.ActivateOrder(id); // Call distributed service (Web Service)

    if(response.Succeed)
      return new SuccessActionResult();
    else
      return new FailedActionResult(response.Reason);
  }
}

Distributed Service Layer (Web Service):

public class OrderManagementWebService : IOrderManagementService
{
  private readonly IOrderExecutionService _orderService;

  public OrderManagementWebService(IOrderExecutionService orderService)
  {
    _orderService = orderService; // Order Service from application service
  }

  public ActivationResult ActivateOrder(int id)
  {
    var response = _orderService.ActivateOrder(id); // Call the application layer to execute the logic
    if(
  }
}

Application Layer:

public class OrderExecutionService : IOrderExecutionService
{
  private IOrderRepository _orderRepository;

  public OrderExecutionService(IOrderRepository orderRepository)
  {
    _orderRepository = orderRepository;
  }

  public ActivationResult ActivateOrder(int id)
  {
    var order = _orderRepository.GetById(id); // Get the order from repository

    try
    {
      order.Activate(); // Call business logic inside the order entity
      return new ActivationResult { Success = true } ;
    }
    catch(ActivationException ex)
    {
      LogFactory.GetLog().Exception(ex); // Call log from infrastructure layer
      return new ActivationResult { Success = false, Reason = ex.Message } ;
    }
  }
}

Domain Layer:

public class PurchaseOrder : Entity
{
  // Properties and fields (Data)
  public int Id { get; private set; }
  public Customer Customer { get; private set; }

  // Methods (contains business logic)
  public void Activate()
  {
     if(Customer.IsBlacklisted)
       throw new InvalidCustomerException(...);

     if(_lineItems.Count == 0)
       throw new NoItemsException(...);

     this.SetStatus(OrderStatus.Active);

     .....
  }
}

Repositories (Data Access Layer):

public class OrderRepository : IOrderRepository
{
  public PurchaseOrder GetById(int id)
  {
    // data access code to access ORM or any data access framework.
  }
}

Infrastrucute:

public class Logger : ILogger
{
  public void Exception(Exception ex)
  {
   // write exception to whatever
  }
}
Mohamed Abed
  • 5,025
  • 22
  • 31
  • thanks @mohamed for the detailed explanation. I am not very clear with a few keywords that you have used, I will try to explain what i have understood. Distributed Services are most likely where the business logic is. Presentation layer is clear too. but i am not too sure about Application Layer and Domain Layer. Data Access looks like it will be accessed only by Distributed Services and wont be exposed externally. Also please explain Infrastructure/Common layer as well. I know I am asking a lot, thanks in advance. – intoTHEwild Oct 20 '11 at 09:33
  • why do we need distributed service layer and application layer separately, there is not much meaningful code in distributed service layer ? – intoTHEwild Oct 20 '11 at 11:09
  • As i said before it is only if you intend to have external consumers (other than your internal web application) – Mohamed Abed Oct 20 '11 at 11:13
  • why cant i host the application layer directly on the web service ? and let the external customers use them as well ? – intoTHEwild Oct 20 '11 at 11:25
  • because the application layer adds a level or abstraction and it co-ordinate transaction logic, and call repositories, all these dependencies are not needed in the web service context, also if you are developing an in house web application you will not need to call the web service instead just call the application layer directly without having to access web service throw additional layer of http – Mohamed Abed Oct 20 '11 at 11:29