DataCets到POCO - 关于DAL架构的查询 [英] DataSets to POCOs - an inquiry regarding DAL architecture

查看:166
本文介绍了DataCets到POCO - 关于DAL架构的查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须非常快速地开发一个相当大的ASP.NET MVC项目,我想对我的DAL设计有一些看法,以确保没有什么会回来咬我,因为BL可能会变得非常复杂。有一点背景:我正在使用Oracle后端,所以内置的LINQ to SQL是出来的;我还需要使用生产级别的库,所以Oracle EF提供程序项目已经出来了;最后,我无法使用任何GPL或LGPL代码(Apache,MS-PL,BSD都可以),所以NHibernate / Castle Project已经出来了。我宁愿 - 如果可能的话 - 为了避免掉钱,但我更关心实施正确的解决方案。总而言之,我的要求是:


  1. Oracle后端

  2. 快速开发

  3. (L)无GPL


  4. 免费


我对DataSets感到非常满意,但是我将从使用POCO作为DataSet和视图之间的中介获益。谁知道,也许在某个时候,另一个DAL解决方案会出现,我会得到时间来切换它(对,对)。所以,虽然我可以使用LINQ将我的DataSet转换为IQueryable,但我想要一个通用的解决方案,所以我不必为每个类写一个自定义查询。



我现在正在修改反思,但同时我有两个问题:


  1. 我忽略了这个问题解决方案?

  2. 有没有其他方法可以推荐将DataSets转换为POCO?

感谢提前。

解决方案

没有正确的答案,虽然你会发现谁会尝试给你一个有些事情要记住:




  • 由于您无法获得EF或Linq-to-SQL的优势,不用担心使用IQuerable界面;你不会得到它的主要优点。当然,一旦你有你的pocos,LINQ对象将是一个伟大的方式来处理他们!许多存储库方法将返回 IQueryable< yourType>


  • 只要你有一个很好的存储库返回你的pocos,使用反射来填补它们是一个很好的策略a>,起初。 如果你有一个封装良好的资料库,我再说一遍。您可以随时切换反射填充实体对象代码以获得更高效的代码,BL中的任何内容都不会知道差异。如果你使自己依赖直接反映(不是优化反射像nHibernate),你可能会后悔无效率。


  • 我建议研究 T4模板。几个月前,我从T4模板生成了实体类(以及所有的代码来填充它们,并持续存在)。我卖了我的T4模板中的代码是非常可怕的,这是第一次尝试,但它吐出一些,一致的代码。


  • 必须为您的存储库方法制定计划,并密切监视您的团队创建的所有方法。您不能拥有一般的 .GetOrders()方法,因为它将每次都会获得所有客户,然后您的LINQ对象将看起来不错,但会覆盖一些不好的数据访问!有一些方法,如 .GetOrderById(int OrderID) .GetOrderByCustomer(int CustomerID)。确保返回实体的每个方法至少在DB中使用索引。如果基本查询返回某些浪费的记录,那没关系,但是它不能进行表扫描,并返回成千上万的浪费记录。




一个例子:

  var Order = From O在rOrders.GetOrderByCustomer(CustID)
其中O.OrderDate> PromoBeginDate
选择O

在此示例中,将检索客户的所有订单,只是为了获得某些的订单。但是不会有大量的浪费,而CustomerID当然应该是订单的索引字段。您必须确定这是否可以接受,或者是否将日期区别添加到您的存储库中,作为新方法或重载其他方法。这没有捷径您可以在效率和维护数据抽象之间走上一条路。您不想在整个解决方案中的每个数据查询中都有一个存储库中的方法。



我发现最近的一些文章,人们正在摔跤完全这样做:




I have to develop a fairly large ASP.NET MVC project very quickly and I would like to get some opinions on my DAL design to make sure nothing will come back to bite me since the BL is likely to get pretty complex. A bit of background: I am working with an Oracle backend so the built-in LINQ to SQL is out; I also need to use production-level libraries so the Oracle EF provider project is out; finally, I am unable to use any GPL or LGPL code (Apache, MS-PL, BSD are okay) so NHibernate/Castle Project are out. I would prefer - if at all possible - to avoid dishing out money but I am more concerned about implementing the right solution. To summarize, there are my requirements:

  1. Oracle backend
  2. Rapid development
  3. (L)GPL-free

  4. Free

I'm reasonably happy with DataSets but I would benefit from using POCOs as an intermediary between DataSets and views. Who knows, maybe at some point another DAL solution will show up and I will get the time to switch it out (yeah, right). So, while I could use LINQ to convert my DataSets to IQueryable, I would like to have a generic solution so I don't have to write a custom query for each class.

I'm tinkering with reflection right now, but in the meantime I have two questions:

  1. Are there any problems I overlooked with this solution?
  2. Are there any other approaches you would recommend to convert DataSets to POCOs?

Thanks in advance.

解决方案

There's no correct answer, though you'll find people who will try to give you one. Some things to keep in mind:

  • Since you can't get the advantages of EF or Linq-to-SQL, don't worry about using the IQuerable interface; you won't be getting the main advantage of it. Of course, once you've got your pocos, LINQ to object will be a great way of dealing with them! Many of your repository methods will return IQueryable<yourType>.

  • As long as you have a good repository to return your pocos, using reflection to fill them out is a good strategy, at first. If you have a well-encapsulated repository, I say again. You can always switch out the reflection-filled entity object code for more efficient code later, and nothing in you BL will know the difference. If you make yourself dependent on straight reflection (not optimized reflection like nHibernate), you might regret the inefficiency later.

  • I would suggest looking into T4 templates. I generated entity classes (and all the code to populate them, and persist them) from T4 templates a few months ago, for the first time. I'm sold! My code in my T4 template is pretty horrible this first try, but it spits out some nice, consistent code.

  • You will have to have a plan for your repository methods, and closely monitor all the methods your team creates. You can't have a general .GetOrders() method, because it will get all the customers every time, and then your LINQ to object will look nice, but will be covering some bad data access! Have methods like .GetOrderById(int OrderID) and .GetOrderByCustomer(int CustomerID). Make sure each method that returns entities uses an index at least in the DB. If the basic query returns some wasted records, that's fine, but it can't do table scans and return thousands of wasted records.

An example:

var Order = From O in rOrders.GetOrderByCustomer(CustID)    
            Where O.OrderDate > PromoBeginDate    
            Select O

In this example, all the Order for a customer would be retrieved, just to get some of the orders. But there won't be a huge amount of waste, and CustomerID should certainly be an indexed field on Orders. You have to decide whether this is acceptable, or whether to add a date distinction to your repository, either as a new method or with overloading other methods. There's no shortcut to this; you have walk the line between efficiency and maintaining your data abstraction. You don't want to have a method in your repository for every single data inquiry in your entire solution.

Some recent articles I've found where people are wrestling with how exactly to do this.:

这篇关于DataCets到POCO - 关于DAL架构的查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆