从页面方法返回自定义对象列表的快速方法,无需单独的 BLL

发布于 2024-07-14 04:47:42 字数 1264 浏览 7 评论 0 原文

我正在使用 jQuery 从页面方法检索 JSON 对象。 我有一个使用 SubSonic 的 DAL,如果我返回从 SubSonic 生成的类创建的对象,我会堵塞管道。 :) 你知道,所有公共属性都会被序列化。 我不希望这个应用程序有一个单独的业务层,因为它很小并且专注于读取操作,而另一个层似乎有点矫枉过正。 为了避免下载一些 SubSonic 臃肿的对象(也可能包含敏感信息)并避免构建单独的层,我尝试返回一个对象列表,如下所示:

[WebMethod]
public static List<object> GetFiles()
{
    FileCollection collection = DB
        .Select()
        .From(DataAccess.File.Schema)
        .ExecuteAsCollection<FileCollection>();

    List<object> files = new List<object>(collection.Count);

    foreach (DataAccess.File file in collection)
    {
        files.Add(new {
                          file.FileId,
                          file.ApplicantFirstName,
                          file.ApplicantLastName,
                          file.UploadDate
                      }
        );
    }

    return files;
}

它有效,并且我得到了一个很好的 JSON 对象作为回报(忽略 DateTime 值):

[{"FileId":1,"ApplicantFirstName":"Paweł","ApplicantLastName":"Krakowiak","UploadDate":"\/Date(1235656448387
)\/"}]

这是一个好方法吗? 我担心 List - 它比返回 List> 更糟糕吗? 表现? 还有别的事吗?

这是.NET 2.0,我无法使用3.5的功能。 至少匿名类型可以工作......

I am using jQuery to retrieve a JSON object from a page method. I have a DAL which uses SubSonic and if I return objects created from SubSonic-generated classes I will clog up the pipes. :) You know, all public properties get serialized. I don't want a separate business layer for this application, because it's small and focused on read operations and yet another layer seems like an overkill. To avoid downloading some SubSonic bloated objects (possibly with sensitive information as well) and avoid building a separate layer I tried returning a list of objects, like this:

[WebMethod]
public static List<object> GetFiles()
{
    FileCollection collection = DB
        .Select()
        .From(DataAccess.File.Schema)
        .ExecuteAsCollection<FileCollection>();

    List<object> files = new List<object>(collection.Count);

    foreach (DataAccess.File file in collection)
    {
        files.Add(new {
                          file.FileId,
                          file.ApplicantFirstName,
                          file.ApplicantLastName,
                          file.UploadDate
                      }
        );
    }

    return files;
}

It works and I get a nice JSON object in return (disregard the DateTime value):

[{"FileId":1,"ApplicantFirstName":"Paweł","ApplicantLastName":"Krakowiak","UploadDate":"\/Date(1235656448387
)\/"}]

Is this a good approach? I am concerned about List<object> - is it worse than returning say a List<SomeDomainObject>? Performance? Something else?

This is .NET 2.0, I can't use 3.5 features. At least anonymous types work...

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

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

发布评论

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

评论(3

情未る 2024-07-21 04:47:42

最大的建议可能是使其成为“集合”而不是列表,但通过简单的 Web 服务返回,这并不是什么大不了的事情,因为该建议通常是在对象仍然存在于 . NET 程序集。

我认为它也很容易阅读。

The biggest recommendation might be to make it a "Collection" rather than a List, but with a simple webservice return, it isn't as big of a deal, as that recommendation is most typically in environments where the object still lives in a .NET assembly.

I think it is easy to read as well.

夏末的微笑 2024-07-21 04:47:42

在这种情况下,使用 List而不是 List 的唯一缺点是在调用 GetFiles 时失去强类型访问权限直接来自.net代码的方法。

The only downside to using List<object> instead of List<SomeDomainObject> in this scenario would be losing strongly-typed access when calling your GetFiles method directly from .net code.

绾颜 2024-07-21 04:47:42

看来我的做法并没有什么问题。 我想做的就是向调用客户端(浏览器)返回一个 JSON 对象来更新 UI。 这个应用程序执行 99% 的读取操作,所以我对此很满意。 我实际上开始添加服务和域(我将我的业务实体保留在这里)层,但我将把它们扔掉。 我真的尽力让这个应用程序保持简单,并且不添加我不需要的东西。

Looks like there's nothing wrong with my approach. All that I want to do is to return a JSON object to the calling client (browser) to update the UI. This application does 99% read operations, so I am fine with it. I actually started adding a Services and Domain (I keep my business entities here) layers, but I'm going to throw them away. I really try to keep it simple for this application and don't add stuff I don't need.

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