开发者

Returning custom class from DAL?

I have an object with following attributes

Books
----------
ID , Name

Now, I want to 开发者_如何学JAVAreturn an object for a gridview in presentation layer which has a lot more attributes then the original object

Gridview Object
--------------- 
ID, Name, Quantity, Location, ISBNNO, LastIssued

I don't have any business object composed of these attributes currently & I don't want to create it just for the sake of a gridview.

How do I create & return custom objects like these on the fly ? I am new to DTO's. Can someone give me an example to return a DTO inside my DAL ?

Thanks, Damien.


if you don't want to mess with your database objects, create a decorator for it

public class myGVObject
{
    public Book { get; set; }

    public int Quantity { get; set; }
    public string Location { get; set; }
    public string ISBNNO { get; set; }
    public DateTime LastIssued { get; set; }
}

and in your gridView use:

<asp:Label id="lblBookId" text="<%# Eval("Book.ID") %>" />
<asp:Label id="lblLastIssue" text="<%# Eval("LastIssued") %>" />


this what I found on google. Is it helping ?

http://www.codeproject.com/KB/cs/datatransferobject.aspx

http://aspalliance.com/1215

I think you could extend your book class or inherit from it.


How about you project your object out of a Linq query on the fly?

var result = from record in datastore
             select new
             {
                 ID = record.ID,
                 Name = record.Name,
                 Quantity = record.Quantity,
                 Location = record.Location,
                 ISBNNO = record.ISBN,
                 LastIssued = record.LastIssued
             };
GridView1.DataSource = result;
GridView1.DataBind();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜