简体   繁体   English

使用实体框架返回要迭代的数据表

[英]Using Entity Framework to return a table of data to iterate against

I am currently using EF 6 to do the following. 我目前正在使用EF 6执行以下操作。 Execute a stored procedure, then bring in the data I need to use. 执行一个存储过程,然后输入我需要使用的数据。 The data is usually 30-40 rows per application run. 每个应用程序运行的数据通常为30-40行。

I then iterate over the var, object, table (whatever you would like to call it), performing similar (sometimes different) tasks on each row. 然后,我遍历var,object,table(无论您要调用什么),在每行上执行相似(有时不同)的任务。 It works great. 效果很好。 I am able to create an Entity object, expose the different complex functions of it, and then create a var to iterate over. 我能够创建一个Entity对象,公开它的不同复杂功能,然后创建一个var进行迭代。

Like: 喜欢:

foreach (var result in StoredProcedureResult)
{
string strFirstname = result.FirstName
string strLastName = result.LastName
//more logic goes here using those variables and interacting with another app

}

I recently thought it would be cool if I had a class solely for accessing the data. 我最近认为,如果我有一个仅用于访问数据的类会很棒。 In this way, I could just reference that class, toss the corresponding connection string into my app.config, and then I can keep the two sets of logic separate. 这样,我可以只引用该类,将相应的连接字符串扔到我的app.config中,然后可以将两组逻辑分开。 So when attempting to do the above in that structure, I get to the point at which, you can't return a var, or when I attempt to match object return type. 因此,当尝试在该结构中执行上述操作时,我到达了不能返回var的地步,或者尝试匹配对象的返回类型。 The return type of the execution of a stored procedure is object (which I can't iterate on). 存储过程执行的返回类型是object(我无法对其进行迭代)。

So my question is, how does one get to the above example, except, the var result, get returned from this data access class? 所以我的问题是,如何从上述数据访问类返回上面的示例(除了var结果之外)?

If I am missing something, or its not possible because I am doing this incorrectly, do let me know. 如果我遗漏了某些东西,或者由于我做错了而无法做到,请告诉我。 It appeared right in my head. 它出现在我的脑海中。

I'm not going to describe the architecture in full. 我不会完整描述该体系结构。 But based on your comments you can do the following (this is not the definitive nor the only way how to do it): 但是根据您的评论,您可以执行以下操作(这不是确定的方法,也不是唯一的方法):

  1. in your data access project you keep the DBContext class, all the code for the stored procedure call and also the class that defines the result of the SP call, let's call it class A; 在数据访问项目中,您保留DBContext类,存储过程调用的所有代码以及定义SP调用结果的类,我们将其称为A类;
  2. in your shared layer project - I would suggest calling it Service layer - you can create a XYService class, that has a method eg GetListOfX that connects to the DB and calls the procedure, if needed this method can also perform some logic, but more importantly: it doesn't return class A, but returns a new class B (this one is defined in the service layer, or can be defined in yet another project - that might be the true shared/common project; as it would be just a definition of common structures it isn't really a layer); 在您的共享层项目中-我建议将其称为服务层-您可以创建XYService类,该类具有一个方法,例如GetListOfX ,该方法连接到数据库并调用该过程,如果需要的话,该方法还可以执行一些逻辑,但是更重要的是:它不返回A类,而是返回一个新的B类(该类在服务层中定义,或者可以在另一个项目中定义-这可能是真正的共享/公共项目;因为它仅仅是一个通用结构的定义实际上不是一个层);
  3. in your application layer you work only with the method GetListOfX of the XYService and the class B, that way you don't need a reference to the data access project 在您的应用程序层中,您只能使用GetListOfX的GetListOfX方法和类B,这样,您就无需引用数据访问项目

In a trivial case the class B has the same properties as the class A. But depending on your needs the class B can have additional properties/functionality it can also ignore some properties of A or even combine multiple properties into one: eg combining the FirstName and LastName as one property called simply Name . 在普通情况下,类B具有与类A相同的属性。但是,根据您的需要,类B可以具有其他属性/功能,它也可以忽略A的某些属性,甚至可以将多个属性组合为一个:例如,组合FirstNameLastName作为一个属性,简称为Name

Basically what you are looking for is the multi-tier application architecture (usually 3-4 tier). 基本上,您正在寻找的是多层应用程序体系结构(通常是3-4层)。 The full extent of such approach (which includes heavy usage of concepts like interfaces and dependency injection) might not be suitable or needed based on your goals, eg if you are building just a small application for yourself with a couple of functions or you know there won't be any reuse of the components of the final solution, then this approach is too wasteful and you can work faster with everything in one project - you should still apply principles like SOLID , DRY and Separation of concerns . 根据您的目标,这种方法的全部范围(包括大量使用诸如接口和依赖项注入等概念)可能不适合或不需要,例如,如果您只是为自己构建一个具有几个功能的小型应用程序,不会重复使用最终解决方案的组件,因此这种方法太浪费了,您可以更快地处理一个项目中的所有内容-您仍应应用SOLIDDRY关注分离的原则。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM