How to implement a DTO for my gridview?
I have gridview binded to a dumb business object. The business object had 30 fields & I only needed to display 8 in my gridview so the rest of fields of the business object were being wasted.
I was advised to design a DTO for this purpose. I have designed a DTO but it does not look any different than a dumb business object itself ?
Currently I have the DTO for the grid, its collection class List <DTO>
and a DAL class. I am filling the DTO in DAL and returning it to presentation layer.
Is this the right way ? Do I have to make a base class for the DTO ?
My DTO atm looks like this
public class ftMasterSummaryDTO
{
private int ftId;
private DateTime entryDate=DateTime.MinValue;
private int ftYear;
private string ftCateg=string.Empty;
private string ftType=string.Empty;
public int FtId
{
get { return ftId; }
set { ftId = value; }
}
public DateTime EntryDate
{
get { return entryDate; }
set { entryDate = value; }
}
public int FtYear
{
get { return ftYear; }
set { ftYear = value; }
}
public string FtCateg
{
get { return ftCateg; }
set { ftCateg = value; }
}
public stri开发者_运维技巧ng FtType
{
get { return ftCateg; }
set { ftCateg = value; }
}
}
Use ObjectDataSource
to bind the GridView to your custom-business-objects. Create a DTOManager class that shall return the IList for your DTOs. Then in the design-mode of the GridView you can remove the coloumns you do not wish to display.
Your DTOManager class may look like this.
/// <summary>
/// Business Manager class for DTO object
/// </summary>
[DataObjectAttribute()]
public class DTOManager
{
/// <summary>
/// Function to get all DTOs records
/// </summary>
/// <returns>Returns List of DTOs</returns>
[DataObjectMethod(DataObjectMethodType.Select, true)]
public static IList<DTO> GetDTOs()
{
return DTO_DB.GetAll();
}
}
Note: You can then bind your GridView to ObjectDataSource in the Design mode. Your ObjectDataSource configuration wizard will automatically detect [
DataObjectAttribute
] DTOManager and it's SELECT method GetDTOs.
精彩评论