Patterns & Architecture

italiansaucySoftware and s/w Development

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

67 views

Jeff
Derstadt

Senior Development Lead

Microsoft Corporation

jeffders@microsoft.com

Patterns & Architecture

Repository
p
attern in MVC

Better testability

Building N
-
tier Application

Using Stored Procedures

Code First Programming

Multi Tier

Single Tier

Separation
of
concerns

Centralize
and
encapsulate data access code

Isolate upper layers from data access infrastructure

Provide greater control over data
access

User Interface

Data Access Layer

Database

Web

Silverlight

New Data Access Layer

Data Access Layer




Fake Database

Business Logic / Controller

Repository

is a class with data access methods

Encapsulates query and update operations

Exposes entity classes with persistence ignorance

Often used with the
UnitOfWork

pattern

Encapsulates transaction and save operations


public class
BlogRepository

{


public
BlogRepository
(
IUnitOfWork

unitOfWork
)


{


...


}



public
IEnumerable
<Blog>
GetAllBlogs
()


{


...


}



public void
AddPost
(Post post)


{


...


}

}

Best Practices

Wrap the
ObjectContext

Create separate repositories for each aggregate root

Multiple
repositories can share same
ObjectContext

as a unit
of work

Query methods
return
IEnumerable
<T>

Repository
interface is a strong
contract

Common Alternatives

Query methods return
IQueryable
<T>

Repository<T> base class


Goal: swap out the data access code with something
in
-
memory

It’s faster

You have more control

Two approaches

Fake your derived
ObjectContext

Build an interface
BlogContext



IBlogContext

Use
IObjectSet

It’s
IQueryable

It’s updateable (
AddObject
,
DeleteObject
, etc.)

It’s testable because you can implement your own

Fake your Repository

Low

High

Complexity

DataSet

Data
Transfer
Objects

High

Interoperability

??

Self
-
Tracking
Entities

T4 Templates


can create a template that generates
default DTOs for each entity

Foreign Keys

Manage relationships using scalar values

Ability to set current and original values

For the whole entity

ApplyCurrentValues

ApplyOriginalValues

For member values

ChangeObjectState



change the state of an entity

ChangeRelationshipState



change the state of a
relationship


E
F 4.0 introduces
self
-
tracking entities

It’s easy to:

G
et started: Add the entity template to your project

M
ake changes: Just work with your objects

Persist changes: Call one method,
ApplyChanges

Entities have no dependencies on EF

Entities track
their own changes as they happen

Wire
-
format enables services to be used on other platforms
like Silverlight and Java



You are communicating across tiers

You want to persist your change
-
set

You need more control over your entities on the client
and/or the server

You need more control over your service
implementation



Now available in the designer

Can map one or more of

Select

Insert

Update

Delete

Still not there, but planned in the next release

Multiple result sets

TVFs




When

Database is an implementation detail

Models are overhead

Just let me code!

Code First is where you start

Available as a CTP for VS 2010 and .NET 4.0

http://www.microsoft.com/downloads
/



Code First is a work in progress

Available post .NET 4.0

Planned additions

Conventions, conventions, conventions

Primary keys and store generated patterns

Relationship inverses

Foreign key constraints

Reachability

Complex types

Data annotation attribute support (existing and new)

Improved API

Better integration (OData, WCF RIA Services, ASP.NET, etc.)



New features for SQL Server

Enum support

Custom types (UDTs) such as spatial

TVFs

Alternate keys

New features for performance and scalability

Larger model support

Incremental model loading

Scalable design surface

New features for productivity

Enhanced API for Code First

More control over entity shape (type transformations, fields,
factory methods)

More control over change tracking and validation

Built
-
in logging and tracing



Use the Repository pattern and wrap the
ObjectContext to achieve better seperation of concerns

Build better unit tests by faking your ObjectContext
and ObjectSets

Use self
-
tracking entities as a low complexity, higher
interoperable N
-
tier solution

Improved stored procedure support in EF4

CodeFirst is coming and allows you to build models
with code

For more Information please contact


Jeff Derstadt

Senior Development Lead


jeffders@microsoft.com

425
-
706
-
9683






Microsoft Corporation

1 Microsoft Way

Redmond, WA 98052 USA

http://
msdn.microsoft.com/data


Gives
us feedback

http://
blogs.msdn.com/adonet

http
://
blogs.msdn.com/efdesign