data-access-layerbusiness-logic-layerpresentation-layer

Separating BLL, PL and DAL in a web application


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?


Solution

  • These are the recommended layers:

    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
      }
    }