-1

I have developing an ASP.NET application to make error statistics in production. I started to make it into layers: presentation, business logic, data access

I created a simple project to describe the question.

The sample database is this:

enter image description here

In my data access layer I used DAO pattern like classes

For example the ProductDao and ProductDto classes in my data acces layer is these:

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)
        { ... }
    }
}

And the data transfer object:

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
    }
}

These methods just call stored procedures. For example this is for 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

I created this for all of my tables in the database.

My business logic classes have an instance of the necessary Dto class and uses the Daos to interact with the database. The properties of the BLL class returns with the Dtos properties.

Example:

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);
        }

    }
}

As presentation layer I used ASP.NET Web Forms framework.

To display the prodcuts for example I use GridView and 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>

So is this a good way to separate layers?

dvjanm
  • 2,351
  • 1
  • 28
  • 42

1 Answers1

1

Your Order table is limited. You can only have one product per order. Perhaps you intended for this. If not, then it's a good idea to have an Order table and an Order Details table. The Order table would have data common to the entire order, such as the customer info, the delivery address etc. The Order Details table primary key is the combination of Order ID and Line Item No. Then, you can have multiple different items per order.

Your supplier class implies that each product can only come from one supplier. Oftentimes, multiple suppliers can supply the same product. I'll leave that to you to figure out how to implement. It probably involves a relationship table between Product and Supplier tables.

You haven't specifically asked your question. Which layers are you talking about? Data should be stored in the database, the business logic stored in the middle tier, and the presentation layer should just be a GUI into the data access layer that doesn't keep your business logic. Seems like you've done that well enough.

mason
  • 31,774
  • 10
  • 77
  • 121
  • Hi, thanks for reading, but my question is not for this code I just have posted. I just make a simple example profject to show the method of how I separate these layers. Of course it's a limited code. The question is whether it is a good idea to make three separated project, one for the Daos nad Dtos, one for the BLL having a reference to the data access layer, and keep this simple structure. Or are there better solutions for this? – dvjanm Jun 02 '13 at 07:31