还是失去了库和去耦,ASP.NET MVC [英] Still lost on Repositories and Decoupling, ASP.NET MVC

查看:140
本文介绍了还是失去了库和去耦,ASP.NET MVC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我仍然在我永恒的追求建立(和理解)脱钩,国际奥委会,DI,等我到哪里,我试图找出如何建立一个资料库部分的现代编程惯例。我检查了邮递<一个href=\"http://stackoverflow.com/questions/2347337/database-abstraction-layer-design-using-irepository-the-right-way\">Database抽象层设计 - ?使用IRepository正道这是非常有益的,但​​我仍然有一些问题,这些问题只是令人迷惑我一路

I'm still on my eternal quest to build (and understand) modern programming convention of decoupling, IoC, DI, etc. I'm to the part where I am trying to figure out how to build a repository. I've examined the post at Database abstraction layer design - Using IRepository the right way? which was very helpful, but I've still got some problems that are just befuddling me all the way.

我有我的计划,现在在4层...

I have my program in now 4 layers...

网络(项目| ASP.NET MVC应用程序) - 参考Models.dll和Persistence.dll

Web (Project | ASP.NET MVC Application) - References Models.dll and Persistence.dll

模式(域对象)

持久性(域对象的功能NHibernate映射)

Persistence (Fluent nHibernate Mapping of Domain Objects)

公用事业(供应商,资料库)

Utilities (Providers, Repositories)

现在的话,我想编写一个简单的会员库。我的第一个任务......?

Now then, I'm trying to write up a simple Membership Repository. My first task... ?

检查是否当有人试图注册的电子邮件地址。似乎一切都很好 - 让我去尝试,并找出在何处放置此

Check to see if an email address exists when someone tries to register. That seemed all well and good - so I go to try and figure out where to place this.

起初虽然,我只想把它放在的MembershipProvider CREATEUSER 方法内。然而,这驻留在公用工程。到目前为止,公用设施没有NHibernate的知识。只有持久性项目有NHibernate的任何知识。

At first though, I would just place it inside of the MembershipProvider class CreateUser method. This, however, resides in the Utilities project. So far, Utilities has no knowledge of nHibernate. Only the Persistence Project has any knowledge of nHibernate.

于是,我的 CREATEUSER 方法需要查询我的数据库。那么什么是最好的做法吗?难道我创建一个 UserRepository 持久性的项目,只是让被称为整个方法 CheckEmail ?或者,我只需添加NHibernate的.dll文件对我的工具项目,并提供内写会话查找?

So then, my CreateUser method needs to query my database. So what's the best practice here? Do I create a UserRepository in the Persistence project, and just make an entire method called CheckEmail? Or do I simply add the nHibernate .dll's to my Utilities project, and write session lookup within the Provider?

好像的更多工作,使我的持久性项目做具体的行动比它使供应商库。为什么我连使得供应商,如果我必须让库又在哪里?是不是所有的这些新方法的目的是停止code重复?但感觉喜欢让事情'独立'我必须写在同一code 2〜3次。什么是这里的最佳实践?

It seems like more work to make repositories in my Persistence Project that do specific actions than it is to make the providers. Why am I even making the Providers if I have to make Repositories for them? Isn't the purpose of all of these new methods to stop code repetition? But it feels like to keep things 'separate' I have to write the same code 2 or 3 times. What is the best practice here?

推荐答案

您库确实应该在你的持久组装实现的。假设你是单元测试它们,你将定义的接口在域装配的每个存储库中。

Your repositories should really be implemented in your Persistence assembly. Assuming you are unit testing them, you would define the interface for each repository in your Domain assembly.

CREATEUSER 方法不应该直接查询数据库,以确定该电子邮件地址已经存在,而不是在你的 DoesEmailExist创建一个单独的方法负责这样做检查。每个方法都应该有一个单一的责任心。

Your CreateUser method shouldn't be directly querying the database to determine if the email address already exists, instead create a separate method in your DoesEmailExist which is responsible for doing that check. Each method should have a single responsiblity.

为了响应jfar的疑虑:

这是正确的,该域定义接口定义了什么可以做,如 Domain.IUserRepository.Create(用户用户)。该域名不但是定义任何的实现。

That's right, the domain defines what can be done, defining interfaces such as Domain.IUserRepository.Create(User user). The domain does not however define any implementation.

假设你开始使用实体框架,您可以创建一个它实现域中定义的接口持久性组装。因此,从域界面下面就上面我们实现的接口:

Let's say you start out using Entity Framework, you might create a Persistence assembly which implements the interfaces defined in the domain. So following on from the domain interface above we implement the interface:

namespace Persistence
{
    public class UserRepository : Domain.IUserRepository
    {
        public void Create(User user)
        {
           // use Entity Framework to persist a user
        } 
    }
}

让说,例如,你的客户以后告诉你实现一个NHibernate的持久层。幸运的是,我们的域名是独立于现有的持久层 - 这是在域中。所以,你可以很容易地实现现有域接口,而无需改变任何code在你的MVC应用程序 - 为所有知道的是你定义的接口,而不是实体框架执行

Let's say for example that your customer later tells you to implement an NHibernate persistence layer. Luckily our domain is separate to the existing persistence layer - it's in the domain. So you can easily implement the existing domain interfaces without needing to change any code in your MVC application - as all that knows about is the interface you defined, not the Entity Framework implementation.

您IoC容器可配置为解决 IUserRepository 作为无论是实体框架或NHibernate的实现,你的MVC应用程序不关心任何一种方式。

Your IoC container can then be configured to resolve IUserRepository as either the Entity Framework or NHibernate implementation, your MVC application doesn't care either way.

在程序集引用方面,持久性组件有到和<$ C参$ C>域也是理所当然不必持久性

In terms of assembly references, the Persistence assembly has a reference to the Domain, and the Domain quite rightly does not have a reference to Persistence.

这导致的脱钩的设计,很容易测试,并改变去转发,造成易于维护。

This leads to a decoupled design which is easy to test, and change going forwards, resulting in easier maintenance.

我希望帮助。

这篇关于还是失去了库和去耦,ASP.NET MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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