I have been learning C# for the last year or so and trying to incorporate best practices along the way. Between StackOverflow and other web resources, I thought I was on the right track to properly separating my concerns, but now I am having some doubts and want to make sure I am going down the right path before I convert my entire website over to this new architecture.
The current website is old ASP VBscript and has a existing database that is pretty ugly (no foreign keys and such) so at least for the first version in .NET I do not want to use and have to learn any ORM tools at this time.
I have the following items that are in separate namespaces and setup so that the UI layer can only see the DTOs and Business layers, and the Data layer can only be seen from the Business layer. Here is a simple example:
productDTO.cs
public class ProductDTO
{
public int ProductId { get; set; }
public string Name { get; set; }
public ProductDTO()
{
ProductId = 0;
Name = String.Empty;
}
}
productBLL.cs
public class ProductBLL
{
public ProductDTO GetProductByProductId(int productId)
{
//validate the input
return ProductDAL.GetProductByProductId(productId);
}
public List<ProductDTO> GetAllProducts()
{
return ProductDAL.GetAllProducts();
}
public void Save(ProductDTO dto)
{
ProductDAL.Save(dto);
}
public bool IsValidProductId(int productId)
{
//domain validation stuff here
}
}
productDAL.cs
public class ProductDAL
{
//have some basic methods here to convert sqldatareaders to dtos
public static ProductDTO GetProductByProductId(int productId)
{
ProductDTO dto = new ProductDTO();
//db logic here using common functions
return dto;
}
public static List<ProductDTO> GetAllProducts()
{
List<ProductDTO> dtoList = new List<ProductDTO>();
//db logic here using common functions
return dtoList;
}
public static void Save(ProductDTO dto)
{
//save stuff here
}
}
In my UI, I would do something like this:
ProductBLL productBll = new ProductBLL();
List<ProductDTO> productList = productBll.GetAllProducts();
for a save:
ProductDTO dto = new ProductDTO();
dto.ProductId = 5;
dto.Name = "New product name";
productBll.Save(dto);
Am I completely off base? Should I also have the same properties in my BLL and not pass back DTOs to my UI? Please tell me what is wrong and what is right. Keep in mind I am not a expert yet.
I would like to implement interfaces to my architecture, but I am still learning how to do that.