Entity Framework: Beyond Demo-Ware - DevIntersection.com

italiansaucySoftware and s/w Development

Dec 13, 2013 (3 years and 7 months ago)

141 views

DEVintersection

Session VS18


Entity Framework in the Enterprise

Architecting the Data Layer

Julie Lerman

@
julielerman

www.thedatafarm.com

2

©
DEVintersection
. All rights reserved.

http://www.DEVintersection.com

consultant/mentor

You can hire me to come
help your team!

Microsoft MVP, INETA Speaker,

ASPInsider
, MCP,
VTdotNET

Leader

contact

jlerman@theDataFarm.com

www.thedatafarm.com

blog

theDataFarm.com/blog

twitter

@
julielerman

book web site

LearnEntityFramework.com

Julie
Lerman

3

©
DEVintersection
. All rights reserved.

http://www.DEVintersection.com

Goals



Big Picture EF

& Architecture

DDD Bounded
Context



Testing w EF

Repository
Pattern(s
)



Unit of
Work

Database

UI


LINQ

Logic

5

©
DEVintersection
. All rights reserved.

http://www.DEVintersection.com

Separation of Concerns



Extensible



Adaptable



Testable



Maintainable



Sensible!

Business/Domain Layer


Repository/Unit of Work

Infrastructure/Data
Access


ORM/Entity Framework

Database

UI


Service Layer

LINQ

Tests

Tests

Considering the domain

Customers

Sales

Orders

Products

Payments

Shipments

Shippers

Promotions

SalesPeople

Employees

SalaryHistory

Returns

Typical Model/DbContext

8

©
DEVintersection
. All rights reserved.

http://www.DEVintersection.com

Delimits the applicability
of a particular model.


BOUNDING
CONTEXTS gives team
members

a
clear and shared understanding of

what
has to be
consistent

and

what
can develop independently
.

-

Domain Driven Design, Eric Evans 2003

“bounded context


-

Domain Driven Design, Eric Evans 2003

10

©
DEVintersection
. All rights reserved.

http://www.DEVintersection.com

DDD Bounded Contexts

for Application Subsystems

Customer Service

Order Taking

Billing

Returns

Marketing

Human Resources

Shipping

11

©
DEVintersection
. All rights reserved.

http://www.DEVintersection.com

Focused
DbContexts

Products

Promotions

Customers

SalesPeople

Employees

SalaryHistory

Returns

Orders

Customers

Payments

Orders

Customers

Customer Service

Orders

Customers

Billing

Returns

Marketing

Human Resources

Shipments

Shippers

Shipping

Orders

DB

DB

Order Taking

12

©
DEVintersection
. All rights reserved.

http://www.DEVintersection.com

Focused
DbContexts

Products

Promotions

SalesPeople

Employees

SalaryHistory

Returns

Orders

Payments

Orders

Customer Service

Orders

Billing

Returns

Marketing

Human Resources

Shipments

Shippers

Shipping

Orders

Customers

Customers

Customers

Customers

DB

DB

Order Taking

Repository 101

Encapsulate

Redundant

Data Access

Code

Business/Domain Layer


Repository/Unit of Work

Infrastructure/Data
Access


ORM/Entity Framework

Database

UI


Service Layer

LINQ

Tests

Tests

15

©
DEVintersection
. All rights reserved.

http://www.DEVintersection.com

Basic Repository




Get

Store

Send
to DB

Remove

GetCustomerById

AddCustomer

RemoveCustomer

GetAllCustomers

GetCurrentCustomers

GetLocalCustomers

GetFilteredCustomers

Save

16

©
DEVintersection
. All rights reserved.

http://www.DEVintersection.com

Many variations…

One repo
per
type?

Read
repos?

Write
repos?

One repo
per
object
graph?

One repo
per
context?

17

©
DEVintersection
. All rights reserved.

http://www.DEVintersection.com




Get

Store

Send
to
DB

Rem
ove




Get

Store

Send
to DB

Remove

Custom
er

Repo

Sales Repo




Get

Store

Send
to DB

Re

move

Product

Repo

Order

LineItems

Shipping

Payment

Customer

Addresses

Aggregate

}

Aggregate Root

18

©
DEVintersection
. All rights reserved.

http://www.DEVintersection.com

Testing with EF in the Mix


EF only or Database?


Using database:


DropCreateDatabaseAlways


Integration/

Interaction
Tests


No EF involved: Inconsequential


EF in the way:


Abstraction/Interfaces

Unit Tests

19

©
DEVintersection
. All rights reserved.

http://www.DEVintersection.com

Mocks or Fakes/Stubs to Emulate EF Functions

Mocking Frameworks


Learning Curve


Magic


DbSets

are not always supported

Roll Your Own Fakes


Learning
Curve


More coding effort


Educational

EF6 More Easily Supports
Mocks


DbSet has new features


Chose not to enhance
IDbSet

to match


Instead, DbSet is more
mockable


Protected constructor has no ties to DbContext


20

©
DEVintersection
. All rights reserved.

http://www.DEVintersection.com

Unit of Work

Classes

UI

Testability

Tests

System.Data

.
Entity

Metadata
&

DbContexts

Fake

Context &
DataA

Fake

DbSet

Fake

Context & Data B

ositories

ositories

ositories

Repositories

Classes

Classes

Classes

Mocks or Fakes

21

©
DEVintersection
. All rights reserved.

http://www.DEVintersection.com

consultant/mentor

You can hire me to come
help your team!

Microsoft MVP, INETA Speaker,

ASPInsider
, MCP,
VTdotNET

Leader

contact

jlerman@theDataFarm.com

www.thedatafarm.com

blog

theDataFarm.com/blog

twitter

@
julielerman

book web site

LearnEntityFramework.com

Julie
Lerman

23

©
DEVintersection
. All rights reserved.

http://www.DEVintersection.com

Resources


Pluralsight

On
-
Demand Training: pluralsight.com


MSDN Developer Center: msdn.com/data/
ef


EF Team: blogs.msdn.com/
adonet


LearnEntityFramework.com


Programming Entity Framework:
DbContext


b
y Julie
Lerman

and Rowan Miller, O’Reilly Media, Feb 22 2012


Domain Driven Design


By Eric Evans, Addison
-
Wesley, 2004


http
://domaindrivendesign.org
/


“Implementing Domain Driven Design”,

Vaughn Vernon, 2013


Separation of Concerns
, MSDN
Magazine,
http://juliel.me/Jhp6Pp