关注点分离:从 Linq 查询返回层之间的投影数据

发布于 2024-12-18 06:52:38 字数 1099 浏览 5 评论 0原文

我正在使用 Linq,但在做一些我认为应该是微不足道的事情时遇到了困难。我想从一层返回数据,以便它可以独立于另一层中的 linq 使用。

假设我有一个数据访问层。它了解实体框架以及如何与其交互。但是,它并不关心谁访问它。我的一个有趣的要求是实体框架中的查询返回不属于实体模型本身的投影数据。 请不要要求我更改这部分要求并为每种返回类型制作 POCO,因为考虑到我要解决的问题,这不是最佳设计。下面是一个示例。

public class ChartData
{
    public function <<returnType??>> GetData()
    {
        MyEntities context = new MyEntities();
        var results = from context.vManyColumnsOfData as v
                      where v.CompanyName = "acme"
                      select new {Year = v.SalesYear, Income = v.Income};
        return ??;
    }
}

然后,我希望 ASP.Net UI 层能够调用数据访问层来获取数据,以便将其绑定到控件。 UI 层不应该知道数据来自哪里。它应该只知道它拥有需要绑定的数据。下面是一个例子。

    protected void chart_Load(object sender, EventArgs e)
    {
       // set some chart properties
       chart.Skin = "Default";
       ...

       // Set the data source
       ChartData dataMgr = new ChartData();
       <<returnType?>> data = dataMgr.GetData();
       chart.DataSource = data;
       chart.DataBind();
    }

将 linq 投影数据发送回另一层的最佳方法是什么?

I'm using Linq and having trouble doing something that I believe should be trivial. I want to return data from one layer so it can be used independently of linq in another layer.

Suppose I have a Data Access Layer. It knows about the entity framework and how to interact with it. But, it doesn't care who accesses it. The one interesting requirement I have is that the queries in the entity framework return projected data that is not part of the Entity Model itself. Please don't ask me to change this part of the requirement and make POCOs for each return type, as it is not the best design given the problem I am trying to solve. Below is an example.

public class ChartData
{
    public function <<returnType??>> GetData()
    {
        MyEntities context = new MyEntities();
        var results = from context.vManyColumnsOfData as v
                      where v.CompanyName = "acme"
                      select new {Year = v.SalesYear, Income = v.Income};
        return ??;
    }
}

Then, I would like to have an ASP.Net UI layer be able to call into the Data Access Layer to get the data in order to bind it to a control. The UI layer should have no notion of where the data came from. It should only know that it has the data it needs to bind. Below is an example.

    protected void chart_Load(object sender, EventArgs e)
    {
       // set some chart properties
       chart.Skin = "Default";
       ...

       // Set the data source
       ChartData dataMgr = new ChartData();
       <<returnType?>> data = dataMgr.GetData();
       chart.DataSource = data;
       chart.DataBind();
    }

What is the best way to send linq projected data back to another layer?

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

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

发布评论

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

评论(4

陌生 2024-12-25 06:52:39

请不要让我更改这部分要求并使
每种返回类型的 POCO,因为它不是考虑到的最佳设计
我正在尝试解决的问题。

我觉得我应该正确地忽略这一点,因为最好的做法是返回已定义的类型。当匿名类型完全包含在创建它们的方法中时,它们很有用。一旦你开始传递他们,就该给他们适当的班级待遇了。

但是,为了满足您施加的限制,您可以从方法返回 IEnumerable并在调用站点使用它或 var 并依赖于控件的动态绑定获取数据。如果您需要以编程方式处理对象,它不会对您有帮助,但它对于数据绑定来说非常有用。

Please don't ask me to change this part of the requirement and make
POCOs for each return type, as it is not the best design given the
problem I am trying to solve.

I feel like I should rightly ignore this, as the best thing to do is to return a defined type. Anonymous types are useful when they are wholly contained within the method that creates them. Once you start passing them around, it is time to go ahead and give them the proper class treatment.

However, to live within your imposed limitations, you can return IEnumerable<object> from the method and use that or var at the callsite and rely upon the dynamic binding of the control to get at the data. It's not going to help you if you need to deal with the object programmatically, but it will serve fine for databinding.

烏雲後面有陽光 2024-12-25 06:52:39

您不能返回匿名类型,因此基本上为此您将需要 POCO,即使您不需要它们。

“考虑到我试图解决的问题,这不是最好的设计”

您能解释一下您想要实现的目标吗?可能会返回某种类型的包含项目字典(即行和列)的列表。想想像非类型化数据集这样的东西(恶心)

You can not return an anonymous type, so basically for this you will need POCO's even though you don't want them.

"not the best design given the problem I am trying to solve"

Could you explain what you are trying to achieve a little more? It might be possible to return some type of list containing a dictionary of items (ie rows and columns). Think something like an untyped dataset (yuck)

不羁少年 2024-12-25 06:52:39

您的 GetData 方法可以使用 IEnumerable(“旧的”非泛型接口)作为其返回类型。

任何动态解析(例如 ASP.NET 或 XAML 绑定)都应该按预期工作,这似乎就是您想要做的。

但是,如果您想在代码中使用结果,您可能必须求助于 .NET 4 的 dynamic 关键字。

以下示例可以在 LINQPad 中运行(在“C# 程序”模式下)并说明了这一点:

void Main()
{
  var v = GetData();

  foreach (dynamic element in v)
  {
    ((string)element.Name).Dump();
  }
}

public IEnumerable GetData()
{
  return from i in Enumerable.Range(1, 10)
         select new
         {
              Name = "Item " + i,
              Value = i
         };
}

请记住从设计角度来看,这样的编码会让大多数人皱眉并且会影响性​​能。

Your GetData method can use IEnumerable (the "old" non-generic interface) as its return type.

Any dynamic resolution (e.g. ASP.NET or XAML bindings) should work as expected, which seems to be what you want to do.

However, if you want to use the results in your code, you will probably have to resort to .NET 4's dynamic keyword.

The following example can be run in LINQPad (in "C# Program" mode) and illustrates this:

void Main()
{
  var v = GetData();

  foreach (dynamic element in v)
  {
    ((string)element.Name).Dump();
  }
}

public IEnumerable GetData()
{
  return from i in Enumerable.Range(1, 10)
         select new
         {
              Name = "Item " + i,
              Value = i
         };
}

Keep in mind that, design-wise, coding like this will make most people frown and can affect performance.

依 靠 2024-12-25 06:52:38

如果不需要静态使用投影类型,只需返回 IEnumerable即可。

If you don't need to use the projected type statically, just return IEnumerable<object>.

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