这是在ASP.NET应用程序中分离图层的好方法吗?

时间:2013-06-01 19:50:08

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

我开发了一个ASP.NET应用程序来生成错误统计信息。 我开始将它分成几层:演示文稿,业务逻辑,数据访问

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

示例数据库是:

enter image description here

在我的数据访问层中,我使用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>

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

1 个答案:

答案 0 :(得分:1)

您的订单表格有限。每个订单只能有一个产品。也许你打算这样做。如果没有,那么拥有Order表和Order Details表是个好主意。订单表将包含整个订单的共同数据,例如客户信息,交货地址等。订单明细表主键是订单ID和订单项编号的组合。然后,您可以为每个订单添加多个不同的商品

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

您没有具体问过您的问题。你在谈论哪些层?数据应存储在数据库中,业务逻辑存储在中间层,而表示层应该只是数据访问层中不能保留业务逻辑的GUI。好像你做得那么好。

相关问题