Separating BLL, PL and DAL in a web application

3.7k views Asked by At

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?

1

There are 1 answers

5
Mohamed Abed On BEST ANSWER

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