enterprise design patterns intro
TRANSCRIPT
![Page 1: Enterprise Design Patterns Intro](https://reader036.vdocument.in/reader036/viewer/2022062503/58a3d9921a28ab7f0b8b513b/html5/thumbnails/1.jpg)
Enterprise Design Patterns IntroDesigning your app the correct way – SSW Module 1725
![Page 2: Enterprise Design Patterns Intro](https://reader036.vdocument.in/reader036/viewer/2022062503/58a3d9921a28ab7f0b8b513b/html5/thumbnails/2.jpg)
Module Overview1. Intro to Dependency Injection2. Intro to the Onion Architecture3. Intro to Enterprise Data
Accessa) Repository Patternb) Unit of Work Pattern
4. View Models
![Page 3: Enterprise Design Patterns Intro](https://reader036.vdocument.in/reader036/viewer/2022062503/58a3d9921a28ab7f0b8b513b/html5/thumbnails/3.jpg)
Are you using Dependency Injection? Tried it? Like it? Used it in a project?
![Page 4: Enterprise Design Patterns Intro](https://reader036.vdocument.in/reader036/viewer/2022062503/58a3d9921a28ab7f0b8b513b/html5/thumbnails/4.jpg)
Dependencies
![Page 5: Enterprise Design Patterns Intro](https://reader036.vdocument.in/reader036/viewer/2022062503/58a3d9921a28ab7f0b8b513b/html5/thumbnails/5.jpg)
![Page 6: Enterprise Design Patterns Intro](https://reader036.vdocument.in/reader036/viewer/2022062503/58a3d9921a28ab7f0b8b513b/html5/thumbnails/6.jpg)
![Page 7: Enterprise Design Patterns Intro](https://reader036.vdocument.in/reader036/viewer/2022062503/58a3d9921a28ab7f0b8b513b/html5/thumbnails/7.jpg)
Click icon to add picture
The answer:
Dependency InjectionLoosely coupled classes
Increased code reuse
Maintainable code
Testable methods
All dependencies are specified in one place
Class dependencies are clearly visible in the constructor
![Page 8: Enterprise Design Patterns Intro](https://reader036.vdocument.in/reader036/viewer/2022062503/58a3d9921a28ab7f0b8b513b/html5/thumbnails/8.jpg)
Lets look at the OrderProcessor. Spot the Code Smell ?
![Page 9: Enterprise Design Patterns Intro](https://reader036.vdocument.in/reader036/viewer/2022062503/58a3d9921a28ab7f0b8b513b/html5/thumbnails/9.jpg)
Look Mum. No Dependencies !
![Page 10: Enterprise Design Patterns Intro](https://reader036.vdocument.in/reader036/viewer/2022062503/58a3d9921a28ab7f0b8b513b/html5/thumbnails/10.jpg)
Are you using Dependency Injection? Tried it? Like it? Used for new project? Refactored existing
code? Implemented
everywhere?
![Page 11: Enterprise Design Patterns Intro](https://reader036.vdocument.in/reader036/viewer/2022062503/58a3d9921a28ab7f0b8b513b/html5/thumbnails/11.jpg)
Onion Architecture
![Page 12: Enterprise Design Patterns Intro](https://reader036.vdocument.in/reader036/viewer/2022062503/58a3d9921a28ab7f0b8b513b/html5/thumbnails/12.jpg)
Enterprise Data Access Patterns
![Page 13: Enterprise Design Patterns Intro](https://reader036.vdocument.in/reader036/viewer/2022062503/58a3d9921a28ab7f0b8b513b/html5/thumbnails/13.jpg)
The Provided Solution Structure
Our Solution Structure
![Page 14: Enterprise Design Patterns Intro](https://reader036.vdocument.in/reader036/viewer/2022062503/58a3d9921a28ab7f0b8b513b/html5/thumbnails/14.jpg)
Repository
![Page 15: Enterprise Design Patterns Intro](https://reader036.vdocument.in/reader036/viewer/2022062503/58a3d9921a28ab7f0b8b513b/html5/thumbnails/15.jpg)
Directly Accessing Data
Duplicated code / No code re-use
Hard to centralize data-related policies (i.e. caching)
Hard to write unit tests
![Page 16: Enterprise Design Patterns Intro](https://reader036.vdocument.in/reader036/viewer/2022062503/58a3d9921a28ab7f0b8b513b/html5/thumbnails/16.jpg)
IIS
Controller
EF DBContext
Database
No RepositoryDirect access to the databasefrom the controller
IIS
Controller
EF Repository: IRepository
Database
With RepositoryAbstraction layer between controller and database context. Unit tests can use a custom persistence later to facilitate testing
Test Project
Controller
Mock Repository : IRepository
Mock Persistenc
e
![Page 17: Enterprise Design Patterns Intro](https://reader036.vdocument.in/reader036/viewer/2022062503/58a3d9921a28ab7f0b8b513b/html5/thumbnails/17.jpg)
Use the Repository Pattern
Isolates the data layer better unit testing support
Centralised, consistent access rules, logic and caching
Maintainability improved code re-use
Readability e.g. GetTopSellingItemsByCategory – Simple method name, complex SQL Query
http://blog.damianbrady.com.au/2012/03/07/a-generic-crud-repository-for-entity-framework/http://rules.ssw.com.au/SoftwareDevelopment/RulestobetterArchitectureandCodeReview/Pages/RepositoryPattern.aspx
![Page 18: Enterprise Design Patterns Intro](https://reader036.vdocument.in/reader036/viewer/2022062503/58a3d9921a28ab7f0b8b513b/html5/thumbnails/18.jpg)
IIS
Controller
LEGACY DATABASE:
IRepository
Database
Use Repositories to abstract legacy data access
Test Project
Controller
Mock Repository : IRepository
Mock Persistenc
e
IIS
Controller
EF Repository: IRepository
Database
New Functionality
Repositories – Migrating to modern data access
![Page 19: Enterprise Design Patterns Intro](https://reader036.vdocument.in/reader036/viewer/2022062503/58a3d9921a28ab7f0b8b513b/html5/thumbnails/19.jpg)
Implementing the Repository Pattern with Code First
![Page 20: Enterprise Design Patterns Intro](https://reader036.vdocument.in/reader036/viewer/2022062503/58a3d9921a28ab7f0b8b513b/html5/thumbnails/20.jpg)
Repository Interfaces
![Page 21: Enterprise Design Patterns Intro](https://reader036.vdocument.in/reader036/viewer/2022062503/58a3d9921a28ab7f0b8b513b/html5/thumbnails/21.jpg)
Generic Repository - Overview
![Page 22: Enterprise Design Patterns Intro](https://reader036.vdocument.in/reader036/viewer/2022062503/58a3d9921a28ab7f0b8b513b/html5/thumbnails/22.jpg)
Generic Repository – Db Context
![Page 23: Enterprise Design Patterns Intro](https://reader036.vdocument.in/reader036/viewer/2022062503/58a3d9921a28ab7f0b8b513b/html5/thumbnails/23.jpg)
Generic Repository - Implementation
![Page 24: Enterprise Design Patterns Intro](https://reader036.vdocument.in/reader036/viewer/2022062503/58a3d9921a28ab7f0b8b513b/html5/thumbnails/24.jpg)
The Repositories
![Page 25: Enterprise Design Patterns Intro](https://reader036.vdocument.in/reader036/viewer/2022062503/58a3d9921a28ab7f0b8b513b/html5/thumbnails/25.jpg)
Repositories in the store controller
![Page 26: Enterprise Design Patterns Intro](https://reader036.vdocument.in/reader036/viewer/2022062503/58a3d9921a28ab7f0b8b513b/html5/thumbnails/26.jpg)
Unit of Work Coordinates multiple
repositories with a single db context
UOW.SaveChanges() commits the changes made to all of the repositories
![Page 27: Enterprise Design Patterns Intro](https://reader036.vdocument.in/reader036/viewer/2022062503/58a3d9921a28ab7f0b8b513b/html5/thumbnails/27.jpg)
IIS
Controller
Unit of Work
Database
Unit of WorkEnsures that multiple repositories share a single database context
Test Project
Controller
Mock Unit Of Work
Mock Persistenc
e
Repository
MockRepositor
yRepositor
yMock
Repository
DbContext
![Page 28: Enterprise Design Patterns Intro](https://reader036.vdocument.in/reader036/viewer/2022062503/58a3d9921a28ab7f0b8b513b/html5/thumbnails/28.jpg)
Implementing the Unit Of Work Pattern
![Page 29: Enterprise Design Patterns Intro](https://reader036.vdocument.in/reader036/viewer/2022062503/58a3d9921a28ab7f0b8b513b/html5/thumbnails/29.jpg)
![Page 30: Enterprise Design Patterns Intro](https://reader036.vdocument.in/reader036/viewer/2022062503/58a3d9921a28ab7f0b8b513b/html5/thumbnails/30.jpg)
Where Is My Context?
![Page 31: Enterprise Design Patterns Intro](https://reader036.vdocument.in/reader036/viewer/2022062503/58a3d9921a28ab7f0b8b513b/html5/thumbnails/31.jpg)
Where the Magic Happens…..Our Dependency Injection Container !
![Page 32: Enterprise Design Patterns Intro](https://reader036.vdocument.in/reader036/viewer/2022062503/58a3d9921a28ab7f0b8b513b/html5/thumbnails/32.jpg)
Implementing the Repository Pattern with EDMX
![Page 33: Enterprise Design Patterns Intro](https://reader036.vdocument.in/reader036/viewer/2022062503/58a3d9921a28ab7f0b8b513b/html5/thumbnails/33.jpg)
I Need Some Context
![Page 34: Enterprise Design Patterns Intro](https://reader036.vdocument.in/reader036/viewer/2022062503/58a3d9921a28ab7f0b8b513b/html5/thumbnails/34.jpg)
Code First Context vs Edmx Generated Context
![Page 35: Enterprise Design Patterns Intro](https://reader036.vdocument.in/reader036/viewer/2022062503/58a3d9921a28ab7f0b8b513b/html5/thumbnails/35.jpg)
Generating Entities In My Domain ProjectFrom the EDMX
![Page 36: Enterprise Design Patterns Intro](https://reader036.vdocument.in/reader036/viewer/2022062503/58a3d9921a28ab7f0b8b513b/html5/thumbnails/36.jpg)
Update the T4 template to point to the EDMX
![Page 37: Enterprise Design Patterns Intro](https://reader036.vdocument.in/reader036/viewer/2022062503/58a3d9921a28ab7f0b8b513b/html5/thumbnails/37.jpg)
DB First - Repository + Unit of Work Entity Framework 4 instructions: http://
blogs.msdn.com/b/adonet/archive/2009/05/21/poco-in-the-entity-framework-part-1-the-experience.aspx
![Page 38: Enterprise Design Patterns Intro](https://reader036.vdocument.in/reader036/viewer/2022062503/58a3d9921a28ab7f0b8b513b/html5/thumbnails/38.jpg)
View Models models that describe a view pass data from a controller to a view. provides a convenient way of
representing complex view data
![Page 39: Enterprise Design Patterns Intro](https://reader036.vdocument.in/reader036/viewer/2022062503/58a3d9921a28ab7f0b8b513b/html5/thumbnails/39.jpg)
Benefits of View Models Remove logic from your views – viewmodels are
easily unit tested Security – remove properties from models you
don’t want in the view Loose coupling –views are not coupled to the
domain DataAnnotations describe the model
![Page 40: Enterprise Design Patterns Intro](https://reader036.vdocument.in/reader036/viewer/2022062503/58a3d9921a28ab7f0b8b513b/html5/thumbnails/40.jpg)
![Page 41: Enterprise Design Patterns Intro](https://reader036.vdocument.in/reader036/viewer/2022062503/58a3d9921a28ab7f0b8b513b/html5/thumbnails/41.jpg)
Summary1. Intro to Dependency Injection2. Intro to the Onion Architecture3. Intro to Enterprise Data
Accessa) Repository Patternb) Unit of Work Pattern
4. View Models