Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Onion Architecture

9,222 views

Published on

Published in: Technology
  • Be the first to comment

Onion Architecture

  1. 1. Matt Hidinger<br />Blog: http://matthidinger.com<br />Twitter: @MattHidinger<br />
  2. 2. Putting business value back to the core of our applications<br />Onion Architecture<br />
  3. 3. Traditional 3-layer Architecture<br />UI<br />Utilities / Common / Shared<br />ASP.NET<br />AutoMapper<br />Core / BLL<br />StructureMap<br />Inftrastructure / DAL<br />nHibernate /LINQ to SQL<br />Log4Net<br />DB<br />
  4. 4. Very easy for developers over time to put more and more business logic in the UI layer<br />Counter-productive to build your application on top of a specific technology that is sure to change over time<br />Logic is easily scattered all over, locating code becomes a major effort.<br />Developers over time struggle to determine where code should go… DAL? BLL? Utilities?<br />Business logic has many tentacles extending from it (directly and indirectly)<br />Library explosion: Makes it easy take a dependency without putting much thought into it, and now it’s littered all over the code base<br />Problems with Traditional Architecture<br />
  5. 5. Inverted Dependencies<br />UI<br />Utilities / Common / Shared<br />ASP.NET<br />Core / BLL<br />AutoMapper<br />Infrastructure / DAL<br />nHibernate /LINQ to SQL<br />StructureMap<br />Log4Net<br />DB<br />
  6. 6. Onion Architecture with Relative Sizes<br />ASP.NET<br />UI<br />Dependency Resolution (Utilities)<br />Core(BLL)<br />AutoMapper<br />StructureMap<br />nHibernate /LINQ to SQL<br />Infrastructure (DAL)<br />DB<br />Log4Net<br />
  7. 7. True loose coupling between layers/components<br />Limit re-write necessity over time, business value-adding logic is entirely self-contained<br />Application becomes very portable – next version of .NET or an entirely new language/platform<br />Business logic has no dependency tentacles (aside from your platform dependencies)<br />Architecture is more easily sustained over time, developers know exactly where components go, a lot of guesswork is removed<br />Infrastructure is free to use any data access library or external web services to do its work<br />UI and Data Access “layers” become much smaller, deal strictly with technology-related code<br />No more need for Common/Shared/Utilities project <br />Compiler enforces dependencies boundaries, impossible for Core to reference Infrastructure<br />Benefits of Onion Architecture<br />
  8. 8. ProductsControllerBrowser hits: /Products/List<br />IUserSession<br />User Interface<br />Application Services<br />HttpUserSession<<class>><br />Domain Services<br />IProductRepository<br />Dependency Resolution<br />ProductRepository<<class>><br />Domain Model<br />Application Core<br />DB<br />Infrastructure<br />
  9. 9.
  10. 10. Draw application/system using concentric circles, can only take dependency on something provided in an inner layer<br />Take ownership of your interfaces<br />Declare the API of the dependency (abstraction) in the inner layer, push implementation outward<br />Direction of dependency is toward the center<br />Externalize all technology related code<br />Push complexity as far outward as possible<br />Guiding Principles<br />User Interface<br />Application Services<br />Domain Services<br />Dependency Resolution<br />Domain Model<br />Application Core<br />DB<br />Infrastructure<br />
  11. 11. Show us some code already…<br />Demo!<br />

×