当前网站是旧的ASP VBscript,并且存在一个相当丑陋的数据库(没有外键等)。因此,至少在.NET的第一个版本中,我不想使用或学习任何ORM工具。
我有以下项目,它们位于不同的命名空间中,并设置了UI层只能看到DTO和Business层,而Data层只能从Business层看到。这里是一个简单的例子: 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
}
}
在我的用户界面中,我会这样做:
ProductBLL productBll = new ProductBLL();
List<ProductDTO> productList = productBll.GetAllProducts();
为了保存:
ProductDTO dto = new ProductDTO();
dto.ProductId = 5;
dto.Name = "New product name";
productBll.Save(dto);
我完全偏离了正确的方向吗? 我是否也应该在我的BLL中具有相同的属性,而不是将DTO传回UI? 请告诉我什么是错误的,什么是正确的。 请记住我还不是专家。
我想将接口实现到我的架构中,但我还在学习如何做到这一点。