I am trying to create business and data layers for my large ASP.NET MVC application. Since this is the first time that I am trying to complete a project of this magnitude, I read several books and try to carefully understand what needs to be done. Typically, my applications mix levels of business logic and data access, and several business objects are intertwined in one class (which confused me several times when I tried to figure out where to add things).
Most of what I read is the separation of business and data layers. Everything seems fine and dandy, but it's hard for me to figure out how to do this in some scenarios. For example, let's say I create a system that allows administrators 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; } }
I then allocate data access by creating a repository
public class ProductRepository { public bool Add(Product product); }
Say I want the product name to contain at least 4 characters. I donβt understand how to do this.
One of my ideas was to extend the Name set property and set it only if it contains 4 characters. However, there is no way for the method that creates the product to know that the name is not set, except that Product.Name! = Everything that they passed.
Another idea I put is to put it in the Add () method in the repository, but then I have my business logic with data logic, which also means that if the Add failed failed call, I donβt know failed for business logic or because the DAL failed (and this also means that I cannot test it using mock frameworks).
The only thing I can think of is to put my DAL stuff in level 3, which is called from the Add () method in the repository, but I do not see this in any of the domain modeling examples in my book or on the Internet (as I saw , at least). It also adds complexity to domain models when I'm not sure if this is necessary.
Another example is the desire to make sure that a name is used by only one product. Will this go in the Product class, ProductRepository Add (), or where?
As a side note, I plan to use NHibernate as my ORM, however, to accomplish what I want (theoretically), it doesn't matter which ORM I use, since TDD should be able to isolate all this.
Thanks in advance!