Search code examples
business-logicseparation-of-concernsdata-access

Where to put restrictions on entities when separating Business layer from Data Layer


I am attempting to create the the business and data layers for my big ASP.NET MVC application. As this is the first time for me attempting a project of this scale I am reading some books and trying to take good care at separating things out properly. Usually my applications mix the business logic and data access layers, and multiple business entities are intertwined in the single class (which has confused me a few times when I was trying to figure out where to add things).

Most of what I have been reading is to separate out the business and data layers. This seems all fine and dandy, but I am having trouble visualizing exactly how to do this in some scenarios. For example, let's say I am creating a system that allows admins to add a new product to the system:

public class Product
{ 
   public int Id { get; private set; }
   public string Name { get; set; }
   public decimal Price { get; set; }
}

Then I separate out the data access by creating a repository

public class ProductRepository
{
   public bool Add(Product product);
}

Let's say I want to require a product's name to have at least 4 characters. I can't see how to do this cleanly.

One idea I had was to expand the Name's set property and only set it if it's 4 characters long. However, there is no way for a method that is creating the product to know the name didn't get set except that Product.Name != whatever they passed in.

Another idea I had is to put it in the Add() method in the repository, but then I have my business logic right there with the data logic, which also means if the Add call fails I don't know if it failed for the business logic or because the DAL failed (and it also means I can't test it using mock frameworks).

The only thing I can think of is to put my DAL stuff in a 3rd layer that gets called from the Add() method in the repository, but I don't see this in any of the domain modelling examples in my book or on the web (that I've seen at least). It also adds to the complexity of the domain models when I am not sure it is needed.

Another example is wanting to make sure that a Name is only used by one product. Would this go in the Product class, ProductRepository Add() method, or where?

As a side note, I plan to use NHibernate as my ORM however, to accomplish what I want it (theoretically) shouldn't matter what ORM I am using since TDD should be able to isolate it all.

Thanks in advance!


Solution

  • I usually approach this by using a layered architecture. How to do this? You basically have the following (ideally) VS projects:

    • Presentation layer (where the UI stuff resides)
    • Business layer (where the actual business logic resides)
    • Data access layer (where you communicate with your underlying DBMS)

    For decoupling all of them I use so-called interface layers s.t. in the end I have

    • Presentation layer (where the UI stuff resides)
    • IBusiness layer (containing the interfaces for the business layer)
    • Business layer (where the actual business logic resides)
    • IDataAccess layer (containing the interfaces for the DAO layer)
    • Data access layer (where you communicate with your underlying DBMS)

    This is extremely handy and creates a nicely decoupled architecture. Basically your presentation layer just accesses the interfaces and not the implementations itself. For creating the according instances you should use a Factory or preferably some dependency injection library (Unity is good for .Net apps or alternatively Spring.Net).

    How does this impact on your business logic / testability of your app?
    It is probably too long to write everything in detail, but if you're concerned about having a well testable design you should absolutely consider dependency injection libraries.

    Using NHibernate,...whatever ORM
    Having a DAO layer completely separated through the interfaces from the other layers you can use whatever technology behind for accessing your underlying DB. You could directly issue SQL queries or use NHibernate, as you wish. The nice thing is that it is totally independent from the rest of your app. You could event start today by writing SQLs manually and tomorrow exchange your DAO dll with one that uses NHibernate without a single change in your BL or presentation layer.
    Moreover testing your BL logic is simple. You may have a class like:

    public class ProductsBl : IProductsBL
    {
    
       //this gets injected by some framework
       public IProductsDao ProductsDao { get; set; }
    
       public void SaveProduct(Product product)
       {
          //do validation against the product object and react appropriately
          ...
    
          //persist it down if valid
          ProductsDao.PersistProduct(product);
       }
    
       ...
    }
    

    Now you can easily test the validation logic in your SaveProduct(...) method by mocking out the ProductDao in your test case.