实体类从LINQ分离到SQL提供程序,以实现存储库模式.如何? [英] Entity classes decoupled from LINQ to SQL provider for implementing the Repository pattern. How?

查看:120
本文介绍了实体类从LINQ分离到SQL提供程序,以实现存储库模式.如何?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我查看了存储库模式,并发现了过去使用的一些想法,这些想法让我感觉很好.

I have looked over the Repository pattern and I recognized some ideas that I was using in the past which made me feel well.

但是,现在我想编写一个使用此模式的应用程序,但我希望从存储库提供程序中分离出实体类.

However now I would like to write an application that would use this pattern BUT I WOULD LIKE TO HAVE THE ENTITY CLASSES DECOUPLED from the repository provider.

我将创建几个程序集:

  1. 一个接口"程序集,它将承载包括IRepository接口在内的常见接口
  2. 一个实体"程序集,将承载诸如产品,用户,订单等实体类.该汇编程序将由接口"汇编程序引用,因为某些方法将返回此类类型或它们的数组.主应用程序程序集(例如Web应用程序)也将引用它
  3. 一个或多个存储库提供程序程序集.每个类都将(至少)包括一个实现IRepository接口的类,并且它将与某个数据存储一起使用.数据存储区可能包括SQL Server,Oracle服务器,MySQL,XML文件,Web/WCF服务等.

直到我发现生成的类与CustomDataContext类之间的深层依赖关系时,研究LINQ to SQL似乎在实现所有方面所花费的时间上都非常有成效.

Studying LINQ to SQL which looks very productive in terms of time taken to implement all seems well until I discover the deep dependency between the generated classes and the CustomDataContext class.

在这种情况下如何使用LINQ to SQL?

How can I use LINQ to SQL in such a scenario?

推荐答案

我不知道这是否正是您想要的,但是您可能想看看Rob Conery的MVC店面代码.他将存储库模式的变体与linq提供程序一起使用.他将LINQ to Sql对象映射到域对象,然后将域对象从存储库提供程序返回到服务层,该服务层包装了提供程序,使他可以在返回的数据到达业务层之前对其进行某些逻辑处理.

I don't know if this is exactly what you want, but you may want to take a look at Rob Conery's MVC Storefront code. He uses a variant of the repository pattern with a linq provider. He maps the LINQ to Sql objects to domain objects and then returns the domain objects from the repository provider to a service layer which wraps the provider allowing him to work some logic on the data returned before it hits the business layer.

MVC店面网络广播
代码

对我来说,听起来像您希望提供者返回DTO,然后将DTO映射到存储库/服务层中的域对象.如果是这种情况,您可以将LINQ to SQL提供程序映射到DTO,让它返回它们,然后将DTO映射到存储库/服务层中的域对象.这应该可以正常工作,但是可能会变得很乏味,因为您现在将具有2个映射层.

To me it sounds like you want the providers to return DTOs and then you want to map the DTOs to the domain objects in the repository/service layer. If this is the case you could map your LINQ to SQL provider to the DTOs, have it return them, then map the DTOs to domain objects in the repository/service layer. This should work just fine, but it may become tedious as you now would have 2 mapping layers.

在这种情况下,您将拥有: ProductService,它带有IProductRepository.它在IProductRepository上调用方法以取回DTO.然后,它将DTO映射到真实的业务对象,并将它们返回给调用代码.

In this case you would have: ProductService, which takes an IProductRepository. It evokes methods on the IProductRepository to get back your DTOs. It then maps the DTOs to the real business objects and returns them to the calling code.

这篇关于实体类从LINQ分离到SQL提供程序,以实现存储库模式.如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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