enterprise design patterns intro
TRANSCRIPT
Enterprise Design Patterns IntroDesigning your app the correct way – SSW Module 1725
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
Are you using Dependency Injection? Tried it? Like it? Used it in a project?
Dependencies
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
Lets look at the OrderProcessor. Spot the Code Smell ?
Look Mum. No Dependencies !
Are you using Dependency Injection? Tried it? Like it? Used for new project? Refactored existing
code? Implemented
everywhere?
Onion Architecture
Enterprise Data Access Patterns
The Provided Solution Structure
Our Solution Structure
Repository
Directly Accessing Data
Duplicated code / No code re-use
Hard to centralize data-related policies (i.e. caching)
Hard to write unit tests
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
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
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
Implementing the Repository Pattern with Code First
Repository Interfaces
Generic Repository - Overview
Generic Repository – Db Context
Generic Repository - Implementation
The Repositories
Repositories in the store controller
Unit of Work Coordinates multiple
repositories with a single db context
UOW.SaveChanges() commits the changes made to all of the repositories
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
Implementing the Unit Of Work Pattern
Where Is My Context?
Where the Magic Happens…..Our Dependency Injection Container !
Implementing the Repository Pattern with EDMX
I Need Some Context
Code First Context vs Edmx Generated Context
Generating Entities In My Domain ProjectFrom the EDMX
Update the T4 template to point to the EDMX
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
View Models models that describe a view pass data from a controller to a view. provides a convenient way of
representing complex view data
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
Summary1. Intro to Dependency Injection2. Intro to the Onion Architecture3. Intro to Enterprise Data
Accessa) Repository Patternb) Unit of Work Pattern
4. View Models