如果我稍后应用 ado 实体框架,推荐的数据访问层设计模式是什么?

发布于 2024-08-04 03:58:51 字数 900 浏览 2 评论 0 原文

我正在创建一个网站并使用 Linq to SQl 作为数据访问层,并且我愿意使该网站可以在 linq to sql 和 ado 实体框架上工作,而无需更改其他层中的许多内容:业务逻辑层或 UI层,

实现此目标的推荐模式是什么?你能简单解释一下如何做到这一点吗?

更新

正如下面回答的那样,存储库模式将对我有很大帮助,

我检查了书呆子晚餐网站并理解了它,但我在里面找到了这段代码:

public class DinnersController : Controller {

        IDinnerRepository dinnerRepository;

        //
        // Dependency Injection enabled constructors

        public DinnersController()
            : this(new DinnerRepository()) {
        }

        public DinnersController(IDinnerRepository repository) {
            dinnerRepository = repository;
        }

这意味着据我所知,它使用接口 IDinnerRepository 声明了一个dinnerRepository ,并在构造函数中给了它 DiningRepository,在我的例子中,这将是 linq to sql 实现,

我的问题是,如果我需要切换到 ado.net 实体框架,我将需要编辑此构造函数行,或者有有更好的解决方案吗?

更新2

我应该把这个存储库接口和实现它的类放在我的解决方案中的哪里,数据访问层还是业务层?

I am creating a website and using Linq to SQl as a data access layer, and i am willing to make the website can work on both linq to sql and ado entity framework, without changing many things in the other layers: business logic layer or UI layer,

Whats the recommended pattern to achieve this goal? can you explain in brief how to do that?

UPDATE

As answered below that repository pattern will help me a lot,

i checked nerd dinner website and understood it, but i found this code inside:

public class DinnersController : Controller {

        IDinnerRepository dinnerRepository;

        //
        // Dependency Injection enabled constructors

        public DinnersController()
            : this(new DinnerRepository()) {
        }

        public DinnersController(IDinnerRepository repository) {
            dinnerRepository = repository;
        }

Which means as i understood that it declare a dinnerRepository using the interface IDinnerRepository , and in the constructor gave it the DinnerRepository, which will be in my case for example the linq to sql implementation,

My question is if i need to switch to ado.net entity framework, i will need to edit this constructor line or there is a better solution for this?

Update 2

Where should i put this Respository Interface and the classes which implement it in my solution, in the data access layer or in the business layer?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

半世蒼涼 2024-08-11 03:58:51

存储库模式是一个不错的选择。如果将其实现为接口;那么您可以更改具体的类,而不必更改其他任何内容。

书呆子晚餐演练 有一个很好的存储库模式示例(带有接口)。

您在那里列出的代码将进入您的控制器(如果您正在执行 MVC 应用程序);你可以创建任何你想要的类,只要它实现了 IDinnerRepository 接口(或者,如果你想设计一个每个人都必须的接口,你可以拥有类似 IRepository 接口的类)实现执行基本的 CRUD 操作,然后如果您需要更多操作,则实现特定的接口(但我们不要让接口变得疯狂),

如果您要“分层”您的应用程序,那么该部分将进入“业务逻辑”层,并且存储库将位于“数据访问层”中,该构造函数契约将是“松散”耦合的部分。

The Repository pattern is a good choice. If you implement it as an interface; then you can change out the concrete classes and not have to change anything else.

The Nerd Dinner walkthrough has an excellent example of the Repository pattern (with interface).

The code you listed there would go in your controller (if you were doing an MVC Application); and you create any class you wanted so long as it implemented the IDinnerRepository interface (or you could have something like an IRepository interface if you wanted to design an interface that everyone had to implement that did the basic CRUD actions, and then implement specific interfaces if you needed more (but let's not go interface crazy).

If you're 'tiering' your application, then that part would go in the "Business Logic" layer, and the Repository would be in the "Data Access Layer". That constructor contract would be the 'loosely' coupled part.

倾听心声的旋律 2024-08-11 03:58:51

我最终使用了“存储库”模式的一个微小变化。我从优秀的书呆子晚餐教程中学到了它。您可以找到 整个教程在这里代码位于 Codeplex

如果您不处于 MVC 情况,请不要让所有 MVC 阻碍您,Linq2SQL 的底层封装是一个很好的封装。在最近的代码库更新中,我从 Linq2SQL 升级到 Linkq2EF,所有更改都在存储库中得到了很好的处理,无需触及任何外部代码。

还值得注意的是,RIA 服务也具有类似的模式。您将其指向 Linq2Sql 或 Linq2EF,它会在其上构建一个包含 CRUD 的基本层。该层位于源代码中,因此您可以将其撕下来并在非 RIA 项目中使用它,但我只是将其保留原样并在其他项目中链接到它,因此即使我忽略了无线传输,我也使用该层能力。

I wound up using a minor variation on the "Repository" pattern. I picked it up from the excellent Nerd Dinner tutorial. You can find the whole tutorial here and the code is on Codeplex.

Don't let all the MVC put you off if your not in an MVC situation, the underlying encapsulation of Linq2SQL is a good one. In a recent update of a codebase I went from Linq2SQL to Linkq2EF and all the changes were nicely dealt with in the repository, no outside code had to be touched.

It is also worth noting that the RIA Services stuff comes with a similar pattern. You point it at Linq2Sql or Linq2EF and it build you a basic layer over it complete with CRUD. That layer is in source code so you could just rip it out and use it in a non RIA project but I just leave it as is and link to it in other projects so I use the layer even if I ignore the over-the-wire abilities.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文