从 DAL 返回自定义类?

发布于 2024-10-26 17:09:03 字数 402 浏览 1 评论 0原文

我有一个具有以下属性的对象

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

现在,我想返回表示层中 gridview 的对象,该对象比原始对象有更多属性

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

我目前没有任何由这些属性组成的业务对象我不想仅仅为了网格视图而创建它。

我如何创建&退货定制 像这样的物体在飞行中?我是 DTO 的新手。有人可以给我一个 在我的 DAL 中返回 DTO 的示例 ?

谢谢, 达米安.

I have an object with following attributes

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

Now, I want to return 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

卖梦商人 2024-11-02 17:09:03

如果您不想弄乱数据库对象,请为其创建一个装饰器

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

并在 gridView 中使用:

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

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") %>" />
菩提树下叶撕阳。 2024-11-02 17:09:03

这是我在谷歌上找到的。有帮助吗?

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

http://aspalliance.com/1215

我认为您可以扩展您的图书类或继承它。

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.

删除→记忆 2024-11-02 17:09:03

您如何从动态的 Linq 查询中投影您的对象?

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

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();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文