Upload
michael-skinner
View
218
Download
0
Tags:
Embed Size (px)
Citation preview
Applied Entity FrameworkDaniel SimmonsDev ManagerMicrosoftSession 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
Target Architecture
DB & Service Tier• Data Access Layer• Business Objects• WCF Service
Web Server• WCF Client• Business Objects• Web UI
Browser
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
Mid-Tier Persistence
Entity Partial Classes
EntityFramework
Context Entities
DAL Business Objects
Generated
Hand-Written
Demo #1: DAL and Business Objects
Look at an EF Model based on NorthwindExamine modifications for concurrency checksDebug optimistic concurrency in action
DAL and Business Objectsdemo
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
T4
Output Text
Control CodeTemplate
Metadata
GeneratedCodeT4
Demo #2: T4 and OnValidate
Add T4 template to customize generated codeModify template for OnValidate mechanismAdd partial class with validation codeTest validation
T4 and OnValidatedemo
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
Web Tier & Repository Pattern
Repository
Fake Impl
Real Impl
Web UIBusinessObjects
Demo #3: Fake Repository & Web UI
Add ICustomerRepository interfaceCreate fake implementationPull in Web UI written using the repository
Fake Repository & Web UIdemo
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?
Cross-tier Communication
Persistence
WCF
Service
Web-tier
WCF Client
Repository
Mid-tier
SerializedEntities
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
Connecting the Dotsdemo
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)
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
question & answer
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
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
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
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
Complete an evaluation on CommNet and enter to win!
© 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.
Title of PresentationNameTitleCompanySession Code:
Video Titlevideo
Customer TitleNameTitleCompany
customer
Demo TitleNameTitleCompany
demo
Partner TitleNameTitleCompany
partner
Announcement Titleannouncing
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
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.
Table Format
Table TitleColumn 1 Column 2 Column 3 Column 4 Column 5
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
Pie Chart Example
59%23%
10%9%
Chart Title
1st Qtr2nd Qtr3rd Qtr4th Qtr
question & answer
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
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)
Track Resources
Resource 1
Resource 2
Resource 3
Resource 4
Complete an evaluation on CommNet and enter to win!
© 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.