我有一个架构问题。我有带有 poco 类(数据库中的等效表)的 DAL 项目、BLL 项目和 UI 项目。 UI项目引用了BLL项目,BLL项目引用了DAL项目。
我想在 UI 项目中显示数据,例如来自数据库中的表 Product 的数据。我应该在BLL项目类中创建与DAL项目中的poco类相同的类并将其返回到UI项目并显示它吗?
这是我在 DAL 中的 poco 类(数据库中的等效表):
public class Product
{
public int ID {get; set; }
public string Name {get; set; }
public String Address {get; set; }
}
在 BLL 中,我创建了与上面的 poco 类相同的业务对象:
public class ProductBO
{
public int ID { get; set; }
public string Name { get; set; }
public String Address { get; set; }
}
在 BLL 中,我还有从 DAL 获取产品并将它们映射到业务对象的方法 - ProductBO:
public class ProductService
{
public List<ProductBO> GetAllProducts()
{
List<ProductBO> productsBO = new List<ProductBO>();
using (var context = NorthwindFactory.CreateContext())
{
List<Product> products = context.Product.ToList();
foreach (var product in products)
{
productsBO.Add(new ProductBO { ID = product.ID, Address = product.Address, Name = product.Name });
}
}
return productsBO;
}
}
现在,在控制器的 UI 项目中,我从返回 List 的 BLL 调用服务,并且在视图中我可以使用业务对象 ProductBO 显示数据。
@model IEnumerable<WebApplication1.BLL.BusinessObjects.ProductBO>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.Name)
</th>
<th>
@Html.DisplayNameFor(model => model.Address)
</th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Address)
</td>
</tr>
}
</table>
这是正确的架构方法吗?