47

Daniel Simmons Dev Manager Microsoft Session Code: DTL401

Embed Size (px)

Citation preview

Page 1: Daniel Simmons Dev Manager Microsoft Session Code: DTL401
Page 2: Daniel Simmons Dev Manager Microsoft Session Code: DTL401

Applied Entity FrameworkDaniel SimmonsDev ManagerMicrosoftSession Code: DTL401

Page 3: Daniel Simmons Dev Manager Microsoft Session Code: DTL401

Agenda

GoalsPriority 1: Increase success with the EF nowPriority 2: Prepare for what is coming in EF4

Approach: Create an EF N-Tier Web ApplicationSketch architectureBuild EF data access layer and business objectsCustomize code generation to add OnValidateCreate fake repository and web applicationAdd WCF service and real repository

Page 4: Daniel Simmons Dev Manager Microsoft Session Code: DTL401

Target Architecture

DB & Service Tier• Data Access Layer• Business Objects• WCF Service

Web Server• WCF Client• Business Objects• Web UI

Browser

Page 5: Daniel Simmons Dev Manager Microsoft Session Code: DTL401

Architectural Considerations

Know what tiers are needed and whyTier may have multiple layers

Sample mid-tier has DAL, business objects and serviceSample web-tier has repository, business objects and UI

Sample separates web-tier from mid-tier because…Reduces internal network surface exposed to the DMZCreates option for web-tier scale out

Consider service operations and payload shapesFactors include change rates and trust boundariesSample uses custom services and shared entities

Page 6: Daniel Simmons Dev Manager Microsoft Session Code: DTL401

Mid-Tier Persistence

Entity Partial Classes

EntityFramework

Context Entities

DAL Business Objects

Generated

Hand-Written

Page 7: Daniel Simmons Dev Manager Microsoft Session Code: DTL401

Demo #1: DAL and Business Objects

Look at an EF Model based on NorthwindExamine modifications for concurrency checksDebug optimistic concurrency in action

Page 8: Daniel Simmons Dev Manager Microsoft Session Code: DTL401

DAL and Business Objectsdemo

Page 9: Daniel Simmons Dev Manager Microsoft Session Code: DTL401

Demo 1 Takeaways

EF enables separate DAL and business object layers, but there is much less code to write

Hand-written DAL is replaced by combination of:EFstorage/entity mappinggenerated context

Business objects also generated, but extended with custom business logic

Pay attention to concurrencyEspecially in disconnected scenarios

Page 10: Daniel Simmons Dev Manager Microsoft Session Code: DTL401

T4

Output Text

Control CodeTemplate

Metadata

GeneratedCodeT4

Page 11: Daniel Simmons Dev Manager Microsoft Session Code: DTL401

Demo #2: T4 and OnValidate

Add T4 template to customize generated codeModify template for OnValidate mechanismAdd partial class with validation codeTest validation

Page 12: Daniel Simmons Dev Manager Microsoft Session Code: DTL401

T4 and OnValidatedemo

Page 13: Daniel Simmons Dev Manager Microsoft Session Code: DTL401

Demo 2 Takeaways

T4 makes customizing code generation easyEF4 will use T4 in a big way

Register SavingChanges event handler during context construction

EF4 makes SaveChanges virtualObjectStateManager tracks all changes

Use LINQ to Objects to interrogate itAvoid modifying other entities in OnValidate

Page 14: Daniel Simmons Dev Manager Microsoft Session Code: DTL401

Web Tier & Repository Pattern

Repository

Fake Impl

Real Impl

Web UIBusinessObjects

Page 15: Daniel Simmons Dev Manager Microsoft Session Code: DTL401

Demo #3: Fake Repository & Web UI

Add ICustomerRepository interfaceCreate fake implementationPull in Web UI written using the repository

Page 16: Daniel Simmons Dev Manager Microsoft Session Code: DTL401

Fake Repository & Web UIdemo

Page 17: Daniel Simmons Dev Manager Microsoft Session Code: DTL401

Demo 3 Takeaways

Mental model of a repository is an in-memory collectionCentralizes all data access in one place

Easy to understand and tune if neededDe-couples rest of application from data access

Easily create fake or mock implementationsEnables multiple real implementations

Did I mention that it de-couples the rest of your application from data access?

Page 18: Daniel Simmons Dev Manager Microsoft Session Code: DTL401

Cross-tier Communication

Persistence

WCF

Service

Web-tier

WCF Client

Repository

Mid-tier

SerializedEntities

Page 19: Daniel Simmons Dev Manager Microsoft Session Code: DTL401

Demo #4: Connecting the Dots

Turn ICustomerRepository into service contractExtend context class with AttachAsModifiedAdd implementation of service contractExamine EF disconnected updatesTest the implementationExamine web tier auto-proxy repositoryRun the final application

Page 20: Daniel Simmons Dev Manager Microsoft Session Code: DTL401

Connecting the Dotsdemo

Page 21: Daniel Simmons Dev Manager Microsoft Session Code: DTL401

Demo 4 Takeaways

Concurrency for disconnected operationsMaintain concurrency value from first read

Change-tracking for disconnected operationsIndividual property tracking often unnecessary

Disconnected graph manipulationMixed operations quite difficult in 3.5 sp1State transitions also constrained in 3.5 sp1Context operations treat graph uniformly

Improvements here in EF4 (APIs, Self-Tracking)

Page 22: Daniel Simmons Dev Manager Microsoft Session Code: DTL401

What Next?

Same patterns apply to .net 3.5 sp1 rich clientsEF4 POCO support adds many options

Completely separate business objects from DALSerialize business objects to Silverlight, .Net 2.0, …Additional flexibility in business logic and testability

Model-first enables generating DBCode-only removes need for separate metadata and mapping info

Page 23: Daniel Simmons Dev Manager Microsoft Session Code: DTL401

question & answer

Page 24: Daniel Simmons Dev Manager Microsoft Session Code: DTL401

More ResourcesCodeEF: http://msdn.microsoft.com/en-us/data/aa937723.aspx EF FAQ: http://blogs.msdn.com/dsimmons/pages/entity-framework-faq.aspxASP.Net MVC: http://www.asp.net/mvc

DocsSample: http://code.msdn.microsoft.com/dtl401sample T4 Templates: http://code.msdn.microsoft.com/t4ef35sp1

BlogsMine: http://blogs.msdn.com/dsimmons EF Design: http://blogs.msdn.com/efdesign ADO.Net Team: http://blogs.msdn.com/adonet

Page 25: Daniel Simmons Dev Manager Microsoft Session Code: DTL401

Related ContentBreakout SessionsDAT308 Design Patterns for Application Architecture with the ADO.NET EFDTL05-INT Surprise! It's the Entity Framework GotchasDTL312 The ADO.NET Entity Framework: Tips and Tricks

Birds of a FeatherBOF21 Solving the Unforeseen Challenges of the ADO.NET Entity Framework: How Can .NET Developers Get Better Prepared?BOF65 Implementing the Entity Framework in an n-Tier World

Hands-on LabsDTL20-HOL Application Development with the ADO.NET Entity Framework in the Microsoft .NET Framework 4.0

Page 26: Daniel Simmons Dev Manager Microsoft Session Code: DTL401

Track Resources

Visit the DPR TLC for a chance to win a copy of Visual Studio Team Suite. Daily drawing occurs every day in the TLC at 4:15pm. Stop by for a raffle tickethttp://www.microsoft.com/visualstudio http://www.microsoft.com/visualstudio/en-us/products/teamsystem/default.mspxPlease visit us in the TLC blue area

Page 27: Daniel Simmons Dev Manager Microsoft Session Code: DTL401

www.microsoft.com/teched

Sessions On-Demand & Community

http://microsoft.com/technet

Resources for IT Professionals

http://microsoft.com/msdn

Resources for Developers

www.microsoft.com/learningMicrosoft Certification and Training Resources

www.microsoft.com/learning

Microsoft Certification & Training Resources

Resources

Page 28: Daniel Simmons Dev Manager Microsoft Session Code: DTL401

Complete an evaluation on CommNet and enter to win!

Page 29: Daniel Simmons Dev Manager Microsoft Session Code: DTL401

© 2009 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries.The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS,

IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

Page 30: Daniel Simmons Dev Manager Microsoft Session Code: DTL401
Page 31: Daniel Simmons Dev Manager Microsoft Session Code: DTL401

Title of PresentationNameTitleCompanySession Code:

Page 32: Daniel Simmons Dev Manager Microsoft Session Code: DTL401

Video Titlevideo

Page 33: Daniel Simmons Dev Manager Microsoft Session Code: DTL401

Customer TitleNameTitleCompany

customer

Page 34: Daniel Simmons Dev Manager Microsoft Session Code: DTL401

Demo TitleNameTitleCompany

demo

Page 35: Daniel Simmons Dev Manager Microsoft Session Code: DTL401

Partner TitleNameTitleCompany

partner

Page 36: Daniel Simmons Dev Manager Microsoft Session Code: DTL401

Announcement Titleannouncing

Page 37: Daniel Simmons Dev Manager Microsoft Session Code: DTL401

PowerPoint TemplateSubtitle color

Set the slide title in “title case” and subheads in “sentence case”The subhead color is defined for this template as the fourth font color from the leftFont Size Requirements

Main bullet points must not be smaller than 24ptDo NOT use any font size lower than 20ptSet subhead to 36pt or smaller so it will fit on a single lineTurn off Auto Resizing on all text boxes

Page 38: Daniel Simmons Dev Manager Microsoft Session Code: DTL401

Slide for Showing Software Code

Use this layout to show software codeThe font is Consolas, a monospace fontThe slide doesn’t use bullets but levels can be indented using the “Increase List Level” icon on the Home menuTo use straight quotes " instead of smart quotes ”, do this:1.Click on the Office Button in the upper left corner2.At the bottom of the menu, choose PowerPoint Options3.From the left pane, select Proofing4.Click on the AutoCorrect Options button5.Select the AutoFormat As You Type tab, and deselect “Straight quotes” with “smart quotes”. Then Click OK.

Page 39: Daniel Simmons Dev Manager Microsoft Session Code: DTL401

Table Format

Table TitleColumn 1 Column 2 Column 3 Column 4 Column 5

Page 40: Daniel Simmons Dev Manager Microsoft Session Code: DTL401

Bar Chart Example

Category 1 Category 2 Category 3 Category 4

4.3

2.5

3.5

4.5

2.4

4.4

1.8

2.8

2 2

3

5

Chart Title

Series 1 Series 2 Series 3

Page 41: Daniel Simmons Dev Manager Microsoft Session Code: DTL401

Pie Chart Example

59%23%

10%9%

Chart Title

1st Qtr2nd Qtr3rd Qtr4th Qtr

Page 42: Daniel Simmons Dev Manager Microsoft Session Code: DTL401

question & answer

Page 43: Daniel Simmons Dev Manager Microsoft Session Code: DTL401

www.microsoft.com/teched Sessions On-Demand & Community

http://microsoft.com/technet Resources for IT Professionals

http://microsoft.com/msdn Resources for Developers

www.microsoft.com/learning Microsoft Certification & Training Resources

Resources

www.microsoft.com/learningMicrosoft Certification and Training Resources

Page 44: Daniel Simmons Dev Manager Microsoft Session Code: DTL401

Related Content

Breakout Sessions (session codes and titles)

Interactive Theater Sessions (session codes and titles)

Hands-on Labs (session codes and titles)

Hands-on Labs (session codes and titles)

Page 45: Daniel Simmons Dev Manager Microsoft Session Code: DTL401

Track Resources

Resource 1

Resource 2

Resource 3

Resource 4

Page 46: Daniel Simmons Dev Manager Microsoft Session Code: DTL401

Complete an evaluation on CommNet and enter to win!

Page 47: Daniel Simmons Dev Manager Microsoft Session Code: DTL401

© 2009 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries.The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS,

IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.