如何使用LINQ to SQL时你抽象出你的持久code? [英] How do you abstract out your persistence code when using LINQ to SQL?

查看:149
本文介绍了如何使用LINQ to SQL时你抽象出你的持久code?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我爱LINQ to SQL的,但它已被窃听我,在使用它,我的仓库code成为了LINQ to SQL的框架产生的,因此紧密耦合到一个SQL Server数据库。

I love LINQ to SQL but it has been bugging me that in using it, my repository code becomes generated by the LINQ to SQL framework and hence tightly coupled to an SQL Server database.

使用LINQ到SQL的一个抽象,松散耦合的方式,如果是这样,你是怎么保持接近的问题,任何您在code与数据库无关的?

Are any of you using LINQ to SQL in an abstracted, loosely coupled fashion and if so, how did you approach the problem of keeping your code database-independent?

推荐答案

有关自己;我的内容重新使用LINQ / DBML产生,因为现实中的属性不会伤害我,和任何其他的实现可以提供一个类似的,足够的模型对象模型 - 但我不使用我的数据上下文外DAL。所以,我有这样的:

For myself; I'm content to re-use the object model that LINQ/dbml generates, since in reality the attributes don't hurt me, and any other implementation could provide a similar-enough model - but I don't use my data-context outside the DAL. So I have something like:

  • IFooRepository - 定义可用的方法同时使用DBML对象和一些POCO模型类生成
  • FooRepository - 知道该数据范围内实施
  • IFooRepository - defines the methods available using both the generated dbml objects and some POCO model classes
  • FooRepository - the implementation that knows about the data-context

我的存储库的方法不公开LINQ的概念,如的IQueryable< T> 防爆pression< ...> ,因为它们是漏水的抽象;其它实现将不同的方式工作(EF同时支持的不同方面,例如)。

My repository methods don't expose LINQ concepts like IQueryable<T> and Expression<...>, since they are leaky abstractions; other implementations will work differently (EF supports different aspects of both, for example).

另外 - 我已经采取了标记大多数的关联属性的内部,并且仅在DAL查询使用它们(与其说是在面向对象的工作)

Further - I've taken to marking most association properties as internal, and only using them during DAL queries (not so much during the OO work).

我可以映射到纯波苏斯,但我看不到好处。我对这个位置些心思:务实LINQ

I could map to pure POCOs, but I don't see the benefit. I have some more thoughts on this here: Pragmatic LINQ.

这篇关于如何使用LINQ to SQL时你抽象出你的持久code?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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