如果我想显示数据库中的数据,我是否应该在 BLL 项目类中创建与 DAL 项目中的 poco 类相同的类并将其返回到 UI 项目? [英] Should I create in BLL project class the same like poco class in DAL project and return it to UI project if I want to display data from database?

查看:18
本文介绍了如果我想显示数据库中的数据,我是否应该在 BLL 项目类中创建与 DAL 项目中的 poco 类相同的类并将其返回到 UI 项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个架构问题.我有带有 poco 类的 DAL 项目(数据库中的等效表)、BLL 项目和 UI 项目.UI工程引用BLL工程,BLL工程引用DAL工程.

I have an architecture question. I have DAL project with poco classes (equivalent tables in database), BLL project and UI project. UI project has reference to BLL project and BLL project has reference to DAL project.

我想在 UI 项目中显示数据,例如来自数据库表 Product 的数据.我是否应该在 BLL 项目类中创建与 DAL 项目中的 poco 类相同的类并将其返回到 UI 项目并显示它?

I would like to display in UI project data for example from table Product from database. Should I create in BLL project class the same like poco class in DAL project and return it to UI project and display it?

这是我在 DAL 中的 poco 类(数据库中的等效表):

So this is mine poco class in DAL (equivalent table in database):

public class Product 
{

    public int  ID  {get; set; }
    public string  Name   {get; set; }
    public String  Address {get; set; }
}

在 BLL 中,我创建了与上面的 poco 类相同的业务对象:

In BLL I have created business object the same like poco class above:

public class ProductBO
{
    public int ID { get; set; }
    public string Name { get; set; }
    public String Address { get; set; }
}

在 BLL 中,我还有从 DAL 获取产品并将它们映射到业务对象的方法 - ProductBO:

In BLL I have also method which gets products from DAL and map them to business objects - 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 显示数据.

And now in UI project in controller I call service from BLL which returns List and in view I can display data using business object 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>

这是正确的架构方法吗?

推荐答案

嗯,没有唯一正确的方法.但我倾向于避免创建一组 DAL 类.现代 ORM 允许使用 POCO 类.是的,有一些限制(如枚举),但恕我直言,不值得为每个业务实体创建两个副本并在它们之间进行映射.因此,我使用位于业务逻辑组件中的单个 POCO 实体.实体框架与该实体一起工作,从数据库中保存和加载它.没有映射.

Well, there is no single correct approach. But I tend to avoid creating set of DAL classes. Modern ORMs allow to work with POCO classes. Yes, there is some limitations (like enums) but IMHO it does not worth creating two copies of each business entity and mapping between them. So, I go with single POCO entity which sits in business logic assembly. Entity Framework works with that entity saves and loads it from database. No mapping.

表示层不同.通常,您在不同页面上有多个相同实体的表示.您还可以使用不同的数据注释属性来设置对视图模型或某些 UIHint 的限制.这会使用特定于 UI 的逻辑污染您的业务实体.此外,您经常需要显示格式化或修改过的数据,例如 FullName 而不是您的 Person 实体的 FirstName 和 LastName.所以,在这里我不使用我的 POCO 业务实体,而是创建视图模型.

Presentation layer is different. Usually you have several representations of same entity on different pages. You also would use different Data Annotation attributes to set restrictions on view models, or some UIHints. That would pollute your business entity with UI-specific logic. Also you will often need to display formatted or modified data, like FullName instead of FirstName and LastName of your Person entity. So, here I don't use my POCO business entities, and create view models instead.

对于您的产品样本,这种方法将如下所示:

With your product sample this approach will look like:

  • 业务逻辑组件具有 POCO 实体 Product
  • 持久性程序集引用业务逻辑程序集并使用相同的Product
  • UI 项目有不同的视图模型ProductViewModelBriefProductViewModel 等.它还负责Product 和视图模型之间的映射.注意 - 手动映射非常耗时.我建议你使用一些映射库,比如 AutoMapper 或 ValueInjecter
  • Business logic assembly has POCO entity Product
  • Persistence assembly references business logic assembly and uses same Product
  • UI project has different view models ProductViewModel, BriefProductViewModel etc. It's also responsible for mapping between Product and view models. Note - manual mapping is time-consuming. I suggest you to use some mapping library, like AutoMapper or ValueInjecter

这篇关于如果我想显示数据库中的数据,我是否应该在 BLL 项目类中创建与 DAL 项目中的 poco 类相同的类并将其返回到 UI 项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆