I am developing a typical Web Application with the following layers
- UI Layer (MVC)
- Business Logic Layer (BAL)
- Data Access Layer (DAL)
Each layer has its own DTO object including the BAL and DAL. My questions regarding this are as follows
-
The DTO returned by the DAL is simply converted to the corresponding DTO in the BAL and sent to the UI Layer. Both attributes and the structure of the DTO objects are the same in some cases. In such scenarios is it better to simply return the DTO in the DAL to the UI layer without including an intermediate object.
-
What is the best way to name these DTO objects and other objects in each layer. Should I use some prefix such as DTOName, ServiceName? The reason why I am asking to use a prefix is because if not the classes in my Solution clashes with other classes in the Framework and with a prefix it is easier for me to understand where each class belongs?
3
Preface
Hopefully this is obvious, but… in the suggested namespaces below, you would replace MyCompany
and MyProject
with the actual names of your company and project.
DTOs
I would recommend using the same DTO classes across all layers. Fewer points of maintenance that way. I usually put them under a MyCompany.MyProject.Models
namespace, in their own VS project of the same name. And I usually name them simply after the real-world entity that they represent. (Ideally, the database tables use the same names too, but sometimes it makes sense to set up the schema a bit differently there.)
Examples: Person
, Address
, Product
Dependencies: None (other than standard .NET or helper libraries)
DAL
My personal preference here is to use a one-for-one set of DAL classes matching the DTO classes, but in a MyCompany.MyProject.DataAccess
namespace/project. Class names here end with an Engine
suffix in order to avoid conflicts. (If you don’t like that term, then a DataAccess
suffix would work fine too. Just be consistent with whatever you choose.) Each class provides simple CRUD options hitting the database, using the DTO classes for most input parameters and return types (inside a generic List
when there are more than one, e.g., the return from a Find()
method).
Examples: PersonEngine
, AddressEngine
, ProductEngine
Dependencies: MyCompany.MyProject.Models
BAL/BLL
Also a one-for-one mapping here, but in a MyCompany.MyProject.Logic
namespace/project, and with classes getting a Logic
suffix. This should be the only layer that calls the DAL! Classes here are quite often just a simple pass-through to the DAL, but if & when business rules need to be implemented, this is the place for it.
Examples: PersonLogic
, AddressLogic
, ProductLogic
Dependencies: MyCompany.MyProject.Models
, MyCompany.MyProject.DataAccess
API
If there’s a web services API layer, I use the same one-for-one approach, but in a MyCompany.MyProject.WebApi
namespace/project, with Services
as the class suffix. (Unless you’re using ASP.NET Web API, in which case you would of course use the Controller
suffix instead).
Examples: PersonServices
, AddressServices
, ProductServices
Dependencies: MyCompany.MyProject.Models
, MyCompany.MyProject.Logic
(never bypass this by calling the DAL directly!)
An Observation on Business Logic
It seems to be increasingly more common for people to leave out the BAL/BLL, and instead implement business logic in one or more of the other layers, wherever it makes the most sense. If you do this, just be absolutely certain that (1) all application code goes through the layer(s) with the business logic, and (2) it’s obvious and/or well-documented where each particular business rule has been implemented. If in doubt, don’t try this at home.
A Final Note on Enterprise-Level Architecture
If you’re in a large company, or other situation where the same database tables get shared across multiple applications, then I would recommend leaving the MyProject
portion out of the above namespaces/projects. That way those layers can be shared by multiple front-end applications (and also behind-the-scenes utilities like Windows Services). But only do this if you have strong cross-team communication and thorough automated regressing testing in place!!! Otherwise, one team’s changes to a shared core component are likely to break another team’s application.
4
I usually build application as
ProjectName.Core // "framework"/common stuff
|- Extenders
|- Gravatar.cs
ProjectName.DataProvider // database provider layer
|- Migrations
|- ApplicationDbContext.cs // entity framework
ProjectName.Domain // database objects
|- Post.cs
|- Tag.cs
ProjectName.Services // validations, database stuff
|- PostService.cs
It is somewhat similar to Sharp-Lite.
About prefixes, I hate them. Microsoft’s Internal Coding Guidelines hate them as well. Also there is a tool called StyleCop that complains about prefixes as well.
1