且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

LINQ to SQL和存储库模式

更新时间:2023-02-13 15:59:38

事情是这样的,LINQ to SQL的是不是真正的对象关系映射器的(ORM),它是一个数据访问层发生器。你可以把它变成一个ORM通过不断深入手动编辑XML文件,并与 SqlMetal 并打诸如此类的东西,但照到哪里哪里是为 DAL

The thing is this, LINQ to SQL is not a true Object Relation Mapper (ORM), it is a data access layer generator. You can make it be an ORM by going deep by hand editing XML files and playing with SqlMetal and whatnot, but where it shines is as a DAL.

背后的ORM的想法是这样的。你有你的SQL数据库,你的域对象。设计数据库正常,你会做的事情(如标准化)在逻辑上并没有转化为一个设计合理的对象模型,反之亦然。这就是所谓的阻抗不匹配,一个ORM的作用就是处理与错配的清洁,有效和高效的方式。在那么痛苦的数据库交互几乎是次要的东西。

The idea behind an ORM is this. You have your SQL database, and your domain objects. To design a database properly, you are going to do things (like normalization) that logically don't translate into a properly designed object model, and vice-versa. This is called "Impedance Mismatch", the role of an ORM is to deal with that mismatch in a clean, effective, and efficient way. The less painful database interaction is almost a secondary thing.

背后的存储库的想法是,它封装了对基础架构的所有持久性逻辑和依赖应用程序的其余部分。当你的应用需要一个Customer对象,它不应该有知道它是从SQL Server,MySQL和一个XML文件,或ASP.NET成员的到来。一旦你有一个去耦,您对持久层的任何变化对应用程序的其他部分没有任何影响。

The idea behind a repository is that it encapsulates all persistence logic and dependencies on infrastructure from the rest of your application. When your application needs a Customer object, it shouldn't have to know whether it is coming from SQL Server, MySQL, an XML file, or ASP.NET Membership. Once you have that de-coupling, any changes you make to your persistence story have no effect on the rest of your application.

考虑到这一点,它变得更清楚,为什么他做了什么,他做到了。 LINQ to SQL是用于生成DAL,但应该知道关于DAL的唯一的事情就是仓库,所以翻译是由他的域对象。这样,他就可以重构自己的域模型,而不用担心他的坚持的故事,而且他可以重构自己的数据库,而不必担心过他的申请荡漾的效果。他也可以开始决定像ORM干什么用的,甚至在哪里存储他的数据问题,然后编码的业务逻辑。

With that in mind, it gets more clear why he did what he did. LINQ to SQL is used to generate the DAL, but the only thing that should be known about the DAL is the repository, so a translation is made to his domain objects. That way he can refactor his domain model without worrying about his persistence story, and he can refactor his database without worrying about rippling effects through his application. He could also start coding on business logic before deciding on questions like what ORM to use, or even where to store his data.

如果他是用真正的ORM(如 NHibernate的),该映射code被其他地方处理(无论是在XML或引导类)。我想的LINQ to SQL(和抢断开源DAL,亚音速)都是伟大的项目,但更多的设计更小,两层应用类似仓库的模式是矫枉过正。店面也是为什么NHibernate的额外的复杂性可能是重要的一个很好的例证。他可以通过去的东西,用来处理那种情况下,而不是人工做的这一切都保存了自己大量的code。

If he were to use a real ORM (like NHibernate), that mapping code gets handled elsewhere (either in XML or bootstrapping classes). I think LINQ to SQL (and Robs open source DAL, SubSonic) are great projects, but more designed for smaller, two-tier applications where something like the repository pattern is overkill. The storefront is also a good illustration of why the additional complexity of NHibernate can be important. He could have saved himself a lot of code by going with something built to handle that sort of scenario, rather than doing it all manually.