MVC3设计 - 库模式和服务层 [英] MVC3 Design - repository pattern and services layer

查看:116
本文介绍了MVC3设计 - 库模式和服务层的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看过一对夫妇对MVC的书籍和文章,并遇到存储库模式和服务层。

I have read a couple of books and articles on MVC, and come across the repository pattern and the services layer.

应的控制器能够通过库图案来获得的实体,或者必须将其从在服务层中检索数据?

Should a controller be able to get entities through the repository pattern, or must it retrieve data from the services layer?

编辑:我有看起来像这样的服务层code

I have code in the services layer that looks like this

public UserInfo GetModel(int userInfoID)
{
    return userInfoRepo.Get(userInfoID);
}

public UserInfo GetUserByPortalID(string portalID)
{
    return userInfoRepo.GetByPortalID(portalID);
}

public UserInfo GetModelByUserName(string username)
{
    return userInfoRepo.GetByUserName(username);
}

如果在服务的方法只在库调用另一个方法,就是有必要让控制器通过服务?

If a method in a service only calls another method in repository, is it necessary to have the controller go through the service?

推荐答案

在分层的应用程序体系结构有这么说,你的绝不能绕过层的基本规则。如果您从您的控制器直接查询您的仓库,你就违反了这一规则。

In layered application architecture there's a fundamental rule that says that you must never bypass a layer. If you query your repository straight from your controller, you would be violating that rule.

还等什么?的你可能会说。的如果有什么服务层没有任何价值?的嘛,它可能在未来...

So what? you may say. What if the service layer adds no value? Well, it might in the future...

您可以选择突破的规则,但,然后它不会是一个分层的应用程序的任何更多。这可能是好的太 - 还有其他的好(甚至更好)应用架构,但我认为首先应该做出关于整体架构的决定,然后你棒该决定。否则,你会拥有的意大利面条code - 我们称之为的烤宽面条的当它是一个分层应用:)

You can choose to break the rule, but then it wouldn't be a layered application any more. That may be okay too - there are other good (even better) application architectures available, but I think that first you should make a decision regarding the overall architecture, and then you stick with that decision. Otherwise you'll end up with spaghetti code - we call it lasagna when it's a layered application :)

这篇关于MVC3设计 - 库模式和服务层的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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