关于javascript:分层架构UIEntity和Repository

35次阅读

共计 15273 个字符,预计需要花费 39 分钟才能阅读完成。

在明确了如何分层之后,咱们就能够把咱们的代码进行移动,把他们放到适合的层里去了。

依然以“注册”为例,咱们梳理一下它蕴含的性能,各自应该放到哪一层:

UI 层
应独立实现:
出现页面,接管用户输出
验证用户输出是否无效(比方:不能为空,最小字符串长度等)
注册胜利后跳转到“之前页面”

public class RegisterController:Controller
{public ActionResult Index(IndexModel model)
    {if (ModelState.IsValid)
        {//...}

        //...

        return Redirect("prepage");

留神:MVC 是一种“架构”,但不是咱们这里说的三层架构,Model View Controller 都在 UI 一层当中。

晚期的 web 开发(比方 ASP/JSP/PHP)什么层都没有,就 Html 外面嵌套逻辑(和.cshtml 里的代码块相似)就 OK 了,连贯数据库啥的都能够写在这些代码块里……

此外,还须要依赖

Repository
实现:用户名是否反复。

    public ActionResult Index(IndexModel model)
    {
        // 用户名是否已被应用
        UserRepository repository = new UserRepository();
        if (repository.GetByName(model.UserName) == null)
        {

咱们曾经大量的应用了各种 Repository 了,他们能够进行保留 / 读取 / 删除各种对象。

@想一想 @:增删改查,和数据库交互……repository 不是应该放在 DAL 层?

隐没的 DAL
因为 EF 等 ORM 工具的大量应用,很多同学会把 Repoistory 当做 DAL 应用。

@想一想 @:为什么不应认为 Repository 是 DAL?

留神:DAL 返回的不能是 BLL 的对象

反证,如果 Repository 是 DAL

它就应该由 Entity 调用……
它就不能返回 Entity,因为只能是 BLL 援用 DAL,不能 DAL(Rrepository)援用 BLL(Entity)啊!
实际上,Repository 应该属于 BLL。用 ADO.NET 能够更分明的看出它应该如何调用 DAL:

public User GetByName(string name)
{QueryHelper query = new QueryHelper();
    DbDataReader reader = query.GetBy(@"SELECT * FROM [User] WHERE [Name] = @name", 
        new SqlParameter("@name", name));

    User user = new User();
    reader.Read();
    user.UserName = reader["Name"].ToString();
    user.Password = reader["Password"].ToString();
    return user;
}

由此可见:

DAL 返回的不能是 BLL 的对象,只能是 DataReader/DataSet 之类的数据库封装对象。
ORM 超额完成了 DAL 的性能,^_^,更靠近于 Repository。温习:DbContext 的正文阐明
DbContext is a combination of the Unit Of Work and Repository patterns

Entity
须要被长久化(放到数据库)的类。

DTO 和充血模式
DTO(Data Transfer Object):用于数据传递的对象.

Entity 必定是 DTO(将数据从 BLL 传到 UI),但除此以外,它还能有其余作用么?

贫血模式:Entity 就只能存储数据,所以只能有属性,不能有办法。
充血模式:Entity 除了存储数据还应该实现业务逻辑,所以不光有属性,还要有办法。
咱们抉择支流的“充血”模式。

业务逻辑实现
比方,给新用户(掉落)随机数量的帮帮币:

public int BMoney {get; set;}
public void Register()
{BMoney += new Random().Next(10);
}

在 Controller 外面的调用代码如下所示:

User user = new User
{
    UserName = model.UserName,
    Password = model.Password,
};
user.Register();    // 和数据库无关
repository.Add(user);

留神:Repository 模式和 Entity 业务逻辑的配合,是体现 BLL 价值(面向对象)的要害。

如果将业务逻辑简化成数据库的 UPDATE 操作(或者更简单的存储过程),就会造成“面向数据库”而不是“面向对象”的代码格调,BLL 层也就没有存在的价值。

没有 Repository
另外,因为 Entity 被 Repository 援用,所以 Entity 就不能再援用 Repository(两个 project 之间不能互相援用)。

这给咱们带来的益处是:

Entity 不依赖于特定的长久化形式,即它既能够被 SQL 存储,也能够被 Nosql 存储(如果需要的话)
Entity 可能不便的被单元测试
然而,也可能给咱们的开发带来不便。(如何不便,一言难尽,碰到了就晓得……简略的说,就是一个对象只能通过援用取得关联对象,无奈取得通过 Repository 查问失去的对象)

所以,也有由 Entity 援用 Repository,把 Repository 作为 Entity 汇合应用的(不举荐)。

https://github.com/hdfgfgh546…
https://www.github.com/hdfgfg…
http://github.com/hdfgfgh546/…
https://github.com/hdfgfgh546…
https://www.github.com/hdfgfg…
http://github.com/hdfgfgh546/…
https://github.com/hdfgfgh546…
https://www.github.com/hdfgfg…
http://github.com/hdfgfgh546/…
https://github.com/hdfgfgh546…
https://www.github.com/hdfgfg…
http://github.com/hdfgfgh546/…
https://github.com/hdfgfgh546…
https://www.github.com/hdfgfg…
http://github.com/hdfgfgh546/…
https://github.com/hdfgfgh546…
https://www.github.com/hdfgfg…
http://github.com/hdfgfgh546/…
https://github.com/hdfgfgh546…
https://www.github.com/hdfgfg…
http://github.com/hdfgfgh546/…
https://github.com/hdfgfgh546…
https://www.github.com/hdfgfg…
http://github.com/hdfgfgh546/…
https://github.com/hdfgfgh546…
https://www.github.com/hdfgfg…
http://github.com/hdfgfgh546/…
https://github.com/hdfgfgh546…
https://www.github.com/hdfgfg…
http://github.com/hdfgfgh546/…
https://github.com/hdfgfgh546…
https://www.github.com/hdfgfg…
http://github.com/hdfgfgh546/…
https://github.com/hdfgfgh546…
https://www.github.com/hdfgfg…
http://github.com/hdfgfgh546/…
https://github.com/hdfgfgh546…
https://www.github.com/hdfgfg…
http://github.com/hdfgfgh546/…
https://github.com/hdfgfgh546…
https://www.github.com/hdfgfg…
http://github.com/hdfgfgh546/…
https://github.com/hdfgfgh546…
https://www.github.com/hdfgfg…
http://github.com/hdfgfgh546/…
https://github.com/hdfgfgh546…
https://www.github.com/hdfgfg…
http://github.com/hdfgfgh546/…
https://github.com/hdfgfgh546…
https://www.github.com/hdfgfg…
http://github.com/hdfgfgh546/…
https://github.com/hdfgfgh546…
https://www.github.com/hdfgfg…
http://github.com/hdfgfgh546/…
https://github.com/hdfgfgh546…
https://www.github.com/hdfgfg…
http://github.com/hdfgfgh546/…
https://github.com/hdfgfgh546…
https://www.github.com/hdfgfg…
http://github.com/hdfgfgh546/…
https://github.com/hdfgfgh546…
https://www.github.com/hdfgfg…
http://github.com/hdfgfgh546/…
https://github.com/hdfgfgh546…
https://www.github.com/hdfgfg…
http://github.com/hdfgfgh546/…
https://github.com/hdfgfgh546…
https://www.github.com/hdfgfg…
http://github.com/hdfgfgh546/…
https://github.com/hdfgfgh546…
https://www.github.com/hdfgfg…
http://github.com/hdfgfgh546/…
https://github.com/hdfgfgh546…
https://www.github.com/hdfgfg…
http://github.com/hdfgfgh546/…
https://github.com/hdfgfgh546…
https://www.github.com/hdfgfg…
http://github.com/hdfgfgh546/…
https://github.com/hdfgfgh546…
https://www.github.com/hdfgfg…
http://github.com/hdfgfgh546/…
https://github.com/hdfgfgh546…
https://www.github.com/hdfgfg…
http://github.com/hdfgfgh546/…
https://github.com/hdfgfgh546…
https://www.github.com/hdfgfg…
http://github.com/hdfgfgh546/…
https://github.com/hdfgfgh546…
https://www.github.com/hdfgfg…
http://github.com/hdfgfgh546/…
https://github.com/hdfgfgh546…
https://www.github.com/hdfgfg…
http://github.com/hdfgfgh546/…
https://github.com/hdfgfgh546…
https://www.github.com/hdfgfg…
http://github.com/hdfgfgh546/…
https://github.com/hdfgfgh546…
https://www.github.com/hdfgfg…
http://github.com/hdfgfgh546/…
https://github.com/hdfgfgh546…
https://www.github.com/hdfgfg…
http://github.com/hdfgfgh546/…
https://github.com/hdfgfgh546…
https://www.github.com/hdfgfg…
http://github.com/hdfgfgh546/…
https://github.com/hdfgfgh546…
https://www.github.com/hdfgfg…
http://github.com/hdfgfgh546/…
https://github.com/hdfgfgh546…
https://www.github.com/hdfgfg…
http://github.com/hdfgfgh546/…
https://github.com/hdfgfgh546…
https://www.github.com/hdfgfg…
http://github.com/hdfgfgh546/…
https://github.com/hdfgfgh546…
https://www.github.com/hdfgfg…
http://github.com/hdfgfgh546/…
https://github.com/hdfgfgh546…
https://www.github.com/hdfgfg…
http://github.com/hdfgfgh546/…
https://github.com/hdfgfgh546…
https://www.github.com/hdfgfg…
http://github.com/hdfgfgh546/…
https://github.com/hdfgfgh546…
https://www.github.com/hdfgfg…
http://github.com/hdfgfgh546/…
https://github.com/hdfgfgh546…
https://www.github.com/hdfgfg…
http://github.com/hdfgfgh546/…
https://github.com/hdfgfgh546…
https://www.github.com/hdfgfg…
http://github.com/hdfgfgh546/…
https://github.com/hdfgfgh546…
https://www.github.com/hdfgfg…
http://github.com/hdfgfgh546/…
https://github.com/hdfgfgh546…
https://www.github.com/hdfgfg…
http://github.com/hdfgfgh546/…
https://github.com/hdfgfgh546…
https://www.github.com/hdfgfg…
http://github.com/hdfgfgh546/…
https://github.com/hdfgfgh546…
https://www.github.com/hdfgfg…
http://github.com/hdfgfgh546/…
https://github.com/hdfgfgh546…
https://www.github.com/hdfgfg…
http://github.com/hdfgfgh546/…
https://github.com/hdfgfgh546…
https://www.github.com/hdfgfg…
http://github.com/hdfgfgh546/…
https://github.com/hdfgfgh546…
https://www.github.com/hdfgfg…
http://github.com/hdfgfgh546/…
https://github.com/hdfgfgh546…
https://www.github.com/hdfgfg…
http://github.com/hdfgfgh546/…
https://github.com/hdfgfgh546…
https://www.github.com/hdfgfg…
http://github.com/hdfgfgh546/…
https://github.com/hdfgfgh546…
https://www.github.com/hdfgfg…
http://github.com/hdfgfgh546/…
https://github.com/hdfgfgh546…
https://www.github.com/hdfgfg…
http://github.com/hdfgfgh546/…
https://github.com/hdfgfgh546…
https://www.github.com/hdfgfg…
http://github.com/hdfgfgh546/…
https://github.com/hdfgfgh546…
https://www.github.com/hdfgfg…
http://github.com/hdfgfgh546/…
https://github.com/hdfgfgh546…
https://www.github.com/hdfgfg…
http://github.com/hdfgfgh546/…
https://github.com/hdfgfgh546…
https://www.github.com/hdfgfg…
http://github.com/hdfgfgh546/…
https://github.com/hdfgfgh546…
https://www.github.com/hdfgfg…
http://github.com/hdfgfgh546/…
https://github.com/hdfgfgh546…
https://www.github.com/hdfgfg…
http://github.com/hdfgfgh546/…
https://github.com/hdfgfgh546…
https://www.github.com/hdfgfg…
http://github.com/hdfgfgh546/…
https://github.com/hdfgfgh546…
https://www.github.com/hdfgfg…
http://github.com/hdfgfgh546/…
https://github.com/hdfgfgh546…
https://www.github.com/hdfgfg…
http://github.com/hdfgfgh546/…
https://github.com/hdfgfgh546…
https://www.github.com/hdfgfg…
http://github.com/hdfgfgh546/…
https://github.com/hdfgfgh546…
https://www.github.com/hdfgfg…
http://github.com/hdfgfgh546/…
https://github.com/hdfgfgh546…
https://www.github.com/hdfgfg…
http://github.com/hdfgfgh546/…
https://github.com/hdfgfgh546…
https://www.github.com/hdfgfg…
http://github.com/hdfgfgh546/…
https://github.com/hdfgfgh546…
https://www.github.com/hdfgfg…
http://github.com/hdfgfgh546/…
https://github.com/hdfgfgh546…
https://www.github.com/hdfgfg…
http://github.com/hdfgfgh546/…
https://github.com/hdfgfgh546…
https://www.github.com/hdfgfg…
http://github.com/hdfgfgh546/…
https://github.com/hdfgfgh546…
https://www.github.com/hdfgfg…
http://github.com/hdfgfgh546/…
https://github.com/hdfgfgh546…
https://www.github.com/hdfgfg…
http://github.com/hdfgfgh546/…
https://github.com/hdfgfgh546…
https://www.github.com/hdfgfg…
http://github.com/hdfgfgh546/…
https://github.com/hdfgfgh546…
https://www.github.com/hdfgfg…
http://github.com/hdfgfgh546/…
https://github.com/hdfgfgh546…
https://www.github.com/hdfgfg…
http://github.com/hdfgfgh546/…
https://github.com/hdfgfgh546…
https://www.github.com/hdfgfg…
http://github.com/hdfgfgh546/…
https://github.com/hdfgfgh546…
https://www.github.com/hdfgfg…
http://github.com/hdfgfgh546/…
https://github.com/hdfgfgh546…
https://www.github.com/hdfgfg…
http://github.com/hdfgfgh546/…
https://github.com/hdfgfgh546…
https://www.github.com/hdfgfg…
http://github.com/hdfgfgh546/…
https://github.com/hdfgfgh546…
https://www.github.com/hdfgfg…
http://github.com/hdfgfgh546/…
https://github.com/hdfgfgh546…
https://www.github.com/hdfgfg…
http://github.com/hdfgfgh546/…
https://github.com/hdfgfgh546…
https://www.github.com/hdfgfg…
http://github.com/hdfgfgh546/…
https://github.com/hdfgfgh546…
https://www.github.com/hdfgfg…
http://github.com/hdfgfgh546/…
https://github.com/hdfgfgh546…
https://www.github.com/hdfgfg…
http://github.com/hdfgfgh546/…
https://github.com/hdfgfgh546…
https://www.github.com/hdfgfg…
http://github.com/hdfgfgh546/…
https://github.com/hdfgfgh546…
https://www.github.com/hdfgfg…
http://github.com/hdfgfgh546/…
https://github.com/hdfgfgh546…
https://www.github.com/hdfgfg…
http://github.com/hdfgfgh546/…
https://github.com/hdfgfgh546…
https://www.github.com/hdfgfg…
http://github.com/hdfgfgh546/…
https://github.com/hdfgfgh546…
https://www.github.com/hdfgfg…
http://github.com/hdfgfgh546/…
https://github.com/hdfgfgh546…
https://www.github.com/hdfgfg…
http://github.com/hdfgfgh546/…
https://github.com/hdfgfgh546…
https://www.github.com/hdfgfg…
http://github.com/hdfgfgh546/…
https://github.com/hdfgfgh546…
https://www.github.com/hdfgfg…
http://github.com/hdfgfgh546/…
https://github.com/hdfgfgh546…
https://www.github.com/hdfgfg…
http://github.com/hdfgfgh546/…
https://github.com/hdfgfgh546…
https://www.github.com/hdfgfg…
http://github.com/hdfgfgh546/…
https://github.com/hdfgfgh546…
https://www.github.com/hdfgfg…
http://github.com/hdfgfgh546/…
https://github.com/hdfgfgh546…
https://www.github.com/hdfgfg…
http://github.com/hdfgfgh546/…
https://github.com/hdfgfgh546…
https://www.github.com/hdfgfg…
http://github.com/hdfgfgh546/…
https://github.com/hdfgfgh546…
https://www.github.com/hdfgfg…
http://github.com/hdfgfgh546/…
https://github.com/hdfgfgh546…
https://www.github.com/hdfgfg…
http://github.com/hdfgfgh546/…
https://github.com/hdfgfgh546…
https://www.github.com/hdfgfg…
http://github.com/hdfgfgh546/…
https://github.com/hdfgfgh546…
https://www.github.com/hdfgfg…
http://github.com/hdfgfgh546/…
https://github.com/hdfgfgh546…
https://www.github.com/hdfgfg…
http://github.com/hdfgfgh546/…
https://github.com/hdfgfgh546…
https://www.github.com/hdfgfg…
http://github.com/hdfgfgh546/…
https://github.com/hdfgfgh546…
https://www.github.com/hdfgfg…
http://github.com/hdfgfgh546/…
https://github.com/hdfgfgh546…
https://www.github.com/hdfgfg…
http://github.com/hdfgfgh546/…
https://github.com/hdfgfgh546…
https://www.github.com/hdfgfg…
http://github.com/hdfgfgh546/…
https://github.com/hdfgfgh546…
https://www.github.com/hdfgfg…
http://github.com/hdfgfgh546/…
https://github.com/hdfgfgh546…
https://www.github.com/hdfgfg…
http://github.com/hdfgfgh546/…
https://github.com/hdfgfgh546…
https://www.github.com/hdfgfg…
http://github.com/hdfgfgh546/…
https://github.com/hdfgfgh546…
https://www.github.com/hdfgfg…
http://github.com/hdfgfgh546/…
https://github.com/hdfgfgh546…
https://www.github.com/hdfgfg…
http://github.com/hdfgfgh546/…
https://github.com/hdfgfgh546…
https://www.github.com/hdfgfg…
http://github.com/hdfgfgh546/…
https://github.com/hdfgfgh546…
https://www.github.com/hdfgfg…
http://github.com/hdfgfgh546/…
https://github.com/hdfgfgh546…
https://www.github.com/hdfgfg…
http://github.com/hdfgfgh546/…
https://github.com/hdfgfgh546…
https://www.github.com/hdfgfg…
http://github.com/hdfgfgh546/…
https://github.com/hdfgfgh546…
https://www.github.com/hdfgfg…
http://github.com/hdfgfgh546/…
https://github.com/hdfgfgh546…
https://www.github.com/hdfgfg…
http://github.com/hdfgfgh546/…
https://github.com/hdfgfgh546…
https://www.github.com/hdfgfg…
http://github.com/hdfgfgh546/…
https://github.com/hdfgfgh546…
https://www.github.com/hdfgfg…
http://github.com/hdfgfgh546/…
https://github.com/hdfgfgh546…
https://www.github.com/hdfgfg…
http://github.com/hdfgfgh546/…
https://github.com/hdfgfgh546…
https://www.github.com/hdfgfg…
http://github.com/hdfgfgh546/…
https://github.com/hdfgfgh546…
https://www.github.com/hdfgfg…
http://github.com/hdfgfgh546/…
https://github.com/hdfgfgh546…
https://www.github.com/hdfgfg…
http://github.com/hdfgfgh546/…
https://github.com/hdfgfgh546…
https://www.github.com/hdfgfg…
http://github.com/hdfgfgh546/…
https://github.com/hdfgfgh546…
https://www.github.com/hdfgfg…
http://github.com/hdfgfgh546/…
https://github.com/hdfgfgh546…
https://www.github.com/hdfgfg…
http://github.com/hdfgfgh546/…
https://github.com/hdfgfgh546…
https://www.github.com/hdfgfg…
http://github.com/hdfgfgh546/…
https://github.com/hdfgfgh546…
https://www.github.com/hdfgfg…
http://github.com/hdfgfgh546/…
https://github.com/hdfgfgh546…
https://www.github.com/hdfgfg…
http://github.com/hdfgfgh546/…
https://github.com/hdfgfgh546…
https://www.github.com/hdfgfg…
http://github.com/hdfgfgh546/…
https://github.com/hdfgfgh546…
https://www.github.com/hdfgfg…
http://github.com/hdfgfgh546/…
https://github.com/hdfgfgh546…
https://www.github.com/hdfgfg…
http://github.com/hdfgfgh546/…
https://github.com/hdfgfgh546…
https://www.github.com/hdfgfg…
http://github.com/hdfgfgh546/…
https://github.com/hdfgfgh546…
https://www.github.com/hdfgfg…
http://github.com/hdfgfgh546/…
https://github.com/hdfgfgh546…
https://www.github.com/hdfgfg…
@想一想 @:能不能够间接应用 Enity 作为 MVC 外面的 Model?

正文完
 0