在 DDD 中,表示层可以同时使用 Repository 和 Service 类吗? [英] In DDD can the presentation layer use both the Repository and Service classes?

查看:40
本文介绍了在 DDD 中,表示层可以同时使用 Repository 和 Service 类吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果表示层只应该使用服务,那么服务类必须公开存储库已经实现的相同方法,以使它们可用于表示层.

If the presentation layer is only supposed to use services then, then service classes must expose the same methods that are already implemented by repositories just to make them available to the presentation layer.

这似乎是错误的.有人可以帮我澄清一下吗?

This seems wrong. Can someone clarify it for me?

推荐答案

我敢打赌,这似乎是错误的,因为您实际上并不需要这种抽象级别.

My bet is that it seems wrong because You don't actually need this level of abstraction.

应用服务是facades.糟糕的外观是一种增加的复杂性而不是它解决的问题.像这样:

Application services are facades. Bad facade is one that adds more complexity than it resolves. Something like this:

public int Increment(int v){ v=v+1;return v;}

除非它解决了足够的复杂性,或者您明确希望所有内容都经过额外的层以尽可能多地解耦客户端 - 这是无用的.

Unless it resolves complexity enough or You explicitly want everything to go through additional layer in order to decouple client as much as possible - it's useless.

就我个人而言,我会把这些东西放在控制器中(如果使用 MVC 模式):

Personally, I would just stick these things in controller (if MVC pattern is used):

public ActionResult ViewBlogPost(int id){
  //I like to name repositories as collections
  var blog=_blogs.Find(id);

  blog.IsBeingViewedBy(_currentViewer); 
  return View(blog);
}

这篇关于在 DDD 中,表示层可以同时使用 Repository 和 Service 类吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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