enterprise design patterns intro

41
Enterprise Design Patterns Intro Designing your app the correct way – SSW Module 1725

Upload: adam-stephensen

Post on 15-Feb-2017

41 views

Category:

Software


0 download

TRANSCRIPT

Page 1: Enterprise Design Patterns Intro

Enterprise Design Patterns IntroDesigning your app the correct way – SSW Module 1725

Page 2: Enterprise Design Patterns Intro

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

Are you using Dependency Injection? Tried it? Like it? Used it in a project?

Page 4: Enterprise Design Patterns Intro

Dependencies

Page 5: Enterprise Design Patterns Intro
Page 6: Enterprise Design Patterns Intro
Page 7: Enterprise Design Patterns Intro

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

Lets look at the OrderProcessor. Spot the Code Smell ?

Page 9: Enterprise Design Patterns Intro

Look Mum. No Dependencies !

Page 10: Enterprise Design Patterns Intro

Are you using Dependency Injection? Tried it? Like it? Used for new project? Refactored existing

code? Implemented

everywhere?

Page 11: Enterprise Design Patterns Intro

Onion Architecture

Page 12: Enterprise Design Patterns Intro

Enterprise Data Access Patterns

Page 13: Enterprise Design Patterns Intro

The Provided Solution Structure

Our Solution Structure

Page 14: Enterprise Design Patterns Intro

Repository

Page 15: Enterprise Design Patterns Intro

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

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

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

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

Implementing the Repository Pattern with Code First

Page 20: Enterprise Design Patterns Intro

Repository Interfaces

Page 21: Enterprise Design Patterns Intro

Generic Repository - Overview

Page 22: Enterprise Design Patterns Intro

Generic Repository – Db Context

Page 23: Enterprise Design Patterns Intro

Generic Repository - Implementation

Page 24: Enterprise Design Patterns Intro

The Repositories

Page 25: Enterprise Design Patterns Intro

Repositories in the store controller

Page 26: Enterprise Design Patterns Intro

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

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

Implementing the Unit Of Work Pattern

Page 29: Enterprise Design Patterns Intro
Page 30: Enterprise Design Patterns Intro

Where Is My Context?

Page 31: Enterprise Design Patterns Intro

Where the Magic Happens…..Our Dependency Injection Container !

Page 32: Enterprise Design Patterns Intro

Implementing the Repository Pattern with EDMX

Page 33: Enterprise Design Patterns Intro

I Need Some Context

Page 34: Enterprise Design Patterns Intro

Code First Context vs Edmx Generated Context

Page 35: Enterprise Design Patterns Intro

Generating Entities In My Domain ProjectFrom the EDMX

Page 36: Enterprise Design Patterns Intro

Update the T4 template to point to the EDMX

Page 38: Enterprise Design Patterns Intro

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

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
Page 41: Enterprise Design Patterns Intro

Summary1. Intro to Dependency Injection2. Intro to the Onion Architecture3. Intro to Enterprise Data

Accessa) Repository Patternb) Unit of Work Pattern

4. View Models