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:
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?