原位地址:http://jeffreypalermo.com/blog/the-onion-architecture-part-2/

In part 1, I introduced an architectural pattern that I have named "Onion Architecture".  The object-oriented design concepts are not new, but I'm pulling together a lot of techniques and conventions into a single pattern and giving it a name.  My hope is that the industry can use this name to communicate the architectural approach where appropriate.

Part 2:  Practical example:

CodeCampServer uses the Onion Architecture.  If you are looking for a full, working application as an example, please have a look.  The practical example I put before you is taken directly from CodeCampServer.  It is a narrow, vertical slice of an example.  I'm keeping the scope small as to be digestible.  I'll start with a diagram so you can understand where all the code resides within the layers of the onion.

CodeCampServer uses the ASP.NET MVC Framework, so SpeakerController is part of the user interface.  This controller is coupled to the ASP.NET MVC Framework, and there is no getting around that.  SpeakerController depends on IConferenceRepository and IUserSession (and IClock, but we'll omit that).  The controller only depends on interfaces, which are defined in the application core.  Remember that all dependencies are toward the center.

Turn your attention to the ConferenceRepository and UserSession classes.  Notice that they are in layers outside of the application core, and they depend on the interfaces as well, so that they can implement them.  These two classes each implement an interface closer to the center than itself.  At runtime, our Inversion of Control container will look at its registry and construct the proper classes to satisfy the constructor dependencies of SpeakerController, which is the following:

 public SpeakerController(IConferenceRepository conferenceRepository,
IUserSession userSession, IClock clock)
: base(userSession)
{
_conferenceRepository = conferenceRepository;
_clock = clock;
_userSession = userSession;
}

At runtime, the IoC container will resolve the classes that implement interfaces and pass them into the SpeakerController constructor.  At this point in time, the SpeakerController can do its job.

Based on the rules of the Onion Architecture, the SpeakerController _could_ use UserSession directly since it's in the same layer, but it cannot use ConferenceRepository directly.  It must rely on something external passing in an instance of IConferenceRepository.  This pattern is used throughout, and the IoC container makes this process seamless.

At the end of this series, I plan on publishing a full working system that adheres to the Onion Architecture pattern.  The systems we build for clients use this approach, but I'm not at liberty to discuss that code, so I will craft a reference application for those of you who prefer a concrete Visual Studio solution to digest.

架构:The Onion Architecture : part 2(洋葱架构:第二篇)(转载)的更多相关文章

  1. 企业架构研究总结(38)——TOGAF架构能力框架之架构能力建设和架构治理

    为了确保架构功能在企业中能够被成功地运用,企业需要通过建立适当的组织结构.流程.角色.责任和技能来实现其自身的企业架构能力,而这也正是TOGAF的架构能力框架(Architecture Capabil ...

  2. 架构:The Onion Architecture : part 3(洋葱架构:第三篇)(转载)

    In my previous installments, I described what has become my approach to defining the architecture fo ...

  3. 架构:The Onion Architecture : part 1(洋葱架构:第一篇)(转载)

    原文地址:http://jeffreypalermo.com/blog/the-onion-architecture-part-1/. I've spoken several times about ...

  4. 干净的架构The Clean Architecture

    干净的架构The Clean Architecture 这是著名软件大师Bob大叔提出的一种架构,也是当前各种语言开发架构.干净架构提出了一种单向依赖关系,从而从逻辑上形成一种向上的抽象系统. 我们经 ...

  5. 企业架构(Enterprise Architecture)

    ylbtech-Miscellaneos: 企业架构(Enterprise Architecture) A,返回顶部 1, 简称EA.是指对企业事业信息管理系统中具有体系的.普遍性的问题而提供的通用解 ...

  6. [Architecture Design] 3-Layer基础架构

    [Architecture Design] 3-Layer基础架构 三层式体系结构 只要是软件从业人员,不管是不是本科系出身的,相信对于三层式体系结构一定都不陌生.在三层式体系结构中,将软件开发所产出 ...

  7. 清晰架构(Clean Architecture)的Go微服务: 程序结构

    我使用Go和gRPC创建了一个微服务,并试图找出最佳的程序结构,它可以用作我未来程序的模板. 我有Java背景,并发现自己在Java和Go之间挣扎,它们之间的编程理念完全不同.我写了一系列关于在项目工 ...

  8. 清晰架构(Clean Architecture)的Go微服务: 设计原则

    我最近写了一个Go微服务应用程序,这个程序的设计来自三个灵感: 清晰架构"Clean Architecture"¹ and SOLID (面向对象设计)² 设计 原则³ Sprin ...

  9. 清晰架构(Clean Architecture)的Go微服务: 程序容器(Application Container)

    清晰架构(Clean Architecture)的一个理念是隔离程序的框架,使框架不会接管你的应用程序,而是由你决定何时何地使用它们.在本程序中,我特意不在开始时使用任何框架,因此我可以更好地控制程序 ...

随机推荐

  1. c语言字符串函数大全(转)

    函数名: stpcpy 功 能: 拷贝一个字符串到另一个 用 法: char *stpcpy(char *destin, char *source); 程序例: #include <stdio. ...

  2. 墨刀 vs Axure RP

    https://www.jianshu.com/p/b4b9c1f15304 墨刀https://modao.cc/ Axure RP https://www.axure.com/https://ww ...

  3. Java第三阶段学习(五、流的操作规律、Properties流、序列化流与反序列化流、打印流、commons-IO jar包)

    一.流的操作规律 四个明确: 明确一:明确要操作的数据是数据源还是数据目的地 源:InputStream   Reader 目的地:OutputStream Writer 先根据需求明确是要读还是写 ...

  4. 后缀自动机(SAM)速成手册!

    正好写这个博客和我的某个别的需求重合了...我就来讲一讲SAM啦qwq 后缀自动机,也就是SAM,是一种极其有用的处理字符串的数据结构,可以用于处理几乎任何有关于子串的问题,但以学起来异常困难著称(在 ...

  5. Codeforces Round #475 (Div. 2) C - Alternating Sum

    等比数列求和一定要分类讨论!!!!!!!!!!!! #include<bits/stdc++.h> #define LL long long #define fi first #defin ...

  6. 关于调用&&传址

    关于调用&&传址//数组int d1[];Function(int *d){} int main(){Function(d1);} //指针int *p;Function(int *q ...

  7. codeforces-1080C

    title: codeforces-1080C date: 2018-11-25 14:23:53 tags: acm 刷题 categories: Codeforces https://www.cn ...

  8. forof循环

    一.语法 1.遍历数组 let myArr=[1,2,3,4,5]; for (let ele of myArr) { console.log(ele); } let myArr=[1,2,3,4,5 ...

  9. Plant Simulation常用命令

    Plant Simulation 是面向对象的三维离散事件仿真软件,使您能够快速.直观地构建逼真的物流模型. 您还可以使用高级统计工具执行复杂的生产分析.以下介绍该软件的一些常用命令. 1. clea ...

  10. Go语言笔记:struct结构遍历

    package main import ( "fmt" "reflect" ) type User struct { Id int Name string // ...