zoukankan      html  css  js  c++  java
  • 架构:The Onion Architecture : part 2(洋葱架构:第二篇)(转载)

    原位地址: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:

    1 public SpeakerController(IConferenceRepository conferenceRepository,
    2                          IUserSession userSession, IClock clock)
    3     : base(userSession)
    4 {
    5     _conferenceRepository = conferenceRepository;
    6     _clock = clock;
    7     _userSession = userSession;
    8 }

    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.

  • 相关阅读:
    python3.4+pyspider爬58同城(二)
    pyspider安装后,点击run,报pyhton has stop working或python已停止运行的错误
    PermissionError: [WinError 32] 另一个程序正在使用此文件,进程无法访问。: 'C:\Users\video\AppData\Local\Temp\tmpfipzk8ma'--问题解决
    使用firefoxprofile,selenium设置firefox,初始化firefox
    排序算法讲解
    Java寫聊天小程序
    csproj项目工程文件的脚本/动态链接库设置
    常见的内存加密防破解及安全方案
    Animator直接引用FBX下的AnimClip与直接引用单独的AnimClip的对比
    Jupyter多内核的手动配置(Python多版本)
  • 原文地址:https://www.cnblogs.com/happyframework/p/3633173.html
Copyright © 2011-2022 走看看