【问题标题】:Is this a good way to separate layers in an ASP.NET application?这是在 ASP.NET 应用程序中分离层的好方法吗?
【发布时间】:2013-05-28 10:00:46
【问题描述】:

我正在开发一个 ASP.NET 应用程序来在生产中进行错误统计。 我开始把它分成几层:表示、业务逻辑、数据访问

我创建了一个简单的项目来描述这个问题。

示例数据库是这样的:

在我的数据访问层中,我使用了类 DAO 模式

例如,我的数据访问层中的 ProductDao 和 ProductDto 类是:

namespace CustomerSupplierDAL.Dao
{
    public class ProductDao
    {
        public void Insert(ProductDto product)
        { ... }

        public void Update(ProductDto product)
        { ... }

        public void Delete(Guid id)
        { ... }

        public void DeleteAllBySupplier(Guid supplier)
        { ... }

        public ProductDto Select(Guid id)
        { ... }

        public List<ProductDto> SelectAll()
        { ... }

        public List<ProductDto> SelectAllBySupplier(Guid supplier)
        { ... }
    }
}

以及数据传输对象:

namespace CustomerSupplierDAL
{
    public class ProductDto
    {
        #region Constructors

        public ProductDto()
        {
        }

        public ProductDto(Guid id, string description, int price, Guid supplier)
        { ... }

        #endregion

        #region Properties

        public Guid Id { get; set; }

        public string Description { get; set; }

        public int Price { get; set; }

        public Guid Supplier { get; set; }

        #endregion
    }
}

这些方法只是调用存储过程。例如这是public ProductDto Select(Guid id)

create procedure [dbo].[ProductSelect]
(
    @Id uniqueidentifier
)

as

set nocount on

select [Id],
    [Description],
    [Price],
    [Supplier]
from [Product]
where [Id] = @Id

我为数据库中的所有表创建了这个。

我的业务逻辑类有一个必要的 Dto 类的实例,并使用 Daos 与数据库进行交互。 BLL 类的属性与 Dtos 属性一起返回。

例子:

namespace CustomerSupplierBLL
{
    [DataObject(true)]
    public class Product
    {
        private ProductDto pDto;
        private ProductDao pDao;
        private Supplier supplier;

        public Product(Guid id)
        {
            this.pDto = pDao.Select(id);
            supplier = null;
        }

        #region Properties

        public Guid Id
        {
            get { return this.pDto.Id; }
        }

        public Guid Supplier
        {
            get { return this.pDto.Supplier; }
        }

        #region Related Objects

        public Supplier SupplierInstance
        {
            get 
            {
                if (this.Supplier == null)
                {
                    this.supplier = new Supplier(this.Supplier);
                }

                return this.supplier;
            }
        }

        #endregion

        #endregion

        [DataObjectMethod(DataObjectMethodType.Select, false)]
        public List<ProductDto> GetProducts(Guid supplierId)
        {
            return this.pDao.SelectAllBySupplier(supplierId);
        }

    }
}

作为表示层,我使用了 ASP.NET Web Forms 框架。

以显示产品为例,我使用 GridView 和 ObjectDataSource:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
    DataSourceID="ProductsObjectDataSource">
    <Columns>
        <asp:BoundField DataField="Id" HeaderText="Id" SortExpression="Id" />
        <asp:BoundField DataField="Description" HeaderText="Description" 
            SortExpression="Description" />
        <asp:BoundField DataField="Price" HeaderText="Price" SortExpression="Price" />
        <asp:BoundField DataField="Supplier" HeaderText="Supplier" 
            SortExpression="Supplier" />
    </Columns>
</asp:GridView>

<asp:ObjectDataSource ID="ProductsObjectDataSource" runat="server" 
    OldValuesParameterFormatString="original_{0}" SelectMethod="GetProducts" 
    TypeName="CustomerSupplierBLL.Product">
    <SelectParameters>
        <asp:Parameter DbType="Guid" Name="supplierId" />
    </SelectParameters>
</asp:ObjectDataSource>

那么这是分离图层的好方法吗?

【问题讨论】:

    标签: asp.net data-access-layer business-logic-layer data-transfer-objects


    【解决方案1】:

    您的订单表是有限的。每个订单只能有一种产品。也许你打算这样做。如果没有,那么最好有一个 Order 表和一个 Order Details 表。 Order 表将包含整个订单共有的数据,例如客户信息、送货地址等。Order Details 表的主键是 Order ID 和 Line Item No 的组合。然后,每个订单可以有多个不同的项目.

    您的供应商类别意味着每种产品只能来自一个供应商。通常,多个供应商可以提供相同的产品。我将把它留给你弄清楚如何实施。它可能涉及到 Product 和 Supplier 表之间的关系表。

    您没有特别提出您的问题。你说的是哪几层?数据应该存储在数据库中,业务逻辑存储在中间层,表示层应该只是数据访问层的 GUI,不保留业务逻辑。看来你已经做得很好了。

    【讨论】:

    • 嗨,感谢阅读,但我的问题不在于我刚刚发布的这段代码。我只是做了一个简单的示例项目来展示我如何分离这些层的方法。当然,这是一个有限的代码。问题是制作三个独立的项目是否是一个好主意,一个用于 Daos nad Dtos,一个用于引用数据访问层的 BLL,并保持这种简单的结构。或者有更好的解决方案吗?
    猜你喜欢
    • 2012-06-23
    • 1970-01-01
    • 2013-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-28
    • 2011-11-05
    相关资源
    最近更新 更多