asp.net-mvc - 我的 View 模型应该以我的域实体为洋葱架构的核心吗?

标签 asp.net-mvc dependency-injection n-tier-architecture onion-architecture

或者它们应该位于顶层(客户端 UI)? 当使用洋葱架构(ASP.Net MVC)时,我应该将 View 模型与所有域实体放在核心位置。就像下图一样?

我的问题是 - 如果是,那么当我将 View 模型从我的服务传递给我的客户端时,客户端(顶层)是否不依赖于核心层,核心层向下两层,这不会破坏整个依赖关系,每一层只与它下面的层对话?

当我有一个 View 模型(在表示层中)需要引用域层(向下两层)中的实体时会发生什么,这不会破坏仅引用直接下面的层的洋葱架构。

public class YogaSpaceListViewModel
{
    // YogaSpaceResults is in the domain layer two layers down
    public IPagedList<YogaSpaceResults> YogaSpaces { get; set; }

    public string LocationResults { get; set; }
}


// this is in the domain layer with all my other entities
// this is being filled by entity framework in the DAL, which I'm calling from the service layer.
public class YogaSpaceResults
{
    public string Title { get; set; }
    public string Summary { get; set; }
    public DateTime Date { get; set; }
    public DbGeography LocationPoints { get; set; }
}

enter image description here

最佳答案

它从外到内工作,因此,客户端了解服务,反之亦然,服务了解域模型,反之亦然。

所以你是对的,如果你把 ViewModel 放在客户端,服务将如何知道这一点?嗯..在这种情况下,您谈论的是应用程序服务而不是域服务,因为它们使用通用语言服务于域模型,而 ViewModel 不应该成为其中的一部分。 应用程序服务应该位于客户端和域模型之间的应用程序层,它们可以协调其他服务和存储库之间的操作。这就是为什么我会说你的 ViewModel 应该位于应用程序层。

关于asp.net-mvc - 我的 View 模型应该以我的域实体为洋葱架构的核心吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31995067/

相关文章:

asp.net-mvc - ASP.Net MVC 使用新的模型实例返回 View 而不是使用重定向到操作

authentication - Laravel:依赖注入(inject)身份验证

c# - 在 Controller 的构造函数之外使用依赖注入(inject)

c# - 我应该在我的项目架构中的什么位置放置自定义成员资格提供程序?

c# - 三层架构中的错误处理

javascript - 将 %20 删除为空

asp.net-mvc - 使用 jQuery POST 和 ASP.NET MVC 时 Controller 参数为 NULL

javascript - JQUERY ON ('change' ) 获取复选框值和状态

Spring - 我应该使用@Bean 还是@Component?

.net - 依赖注入(inject)——它是否违反了关注点分离?