Daniel Simmons Dev Manager Microsoft Session Code: DTL401 Agenda Goals Priority 1: Increase success with the EF now Priority 2: Prepare for what is coming in.
Download ReportTranscript Daniel Simmons Dev Manager Microsoft Session Code: DTL401 Agenda Goals Priority 1: Increase success with the EF now Priority 2: Prepare for what is coming in.
Daniel Simmons Dev Manager Microsoft Session Code: DTL401 Agenda Goals Priority 1: Increase success with the EF now Priority 2: Prepare for what is coming in EF4 Approach: Create an EF N-Tier Web Application Sketch architecture Build EF data access layer and business objects Customize code generation to add OnValidate Create fake repository and web application Add 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 why Tier may have multiple layers Sample mid-tier has DAL, business objects and service Sample web-tier has repository, business objects and UI Sample separates web-tier from mid-tier because… Reduces internal network surface exposed to the DMZ Creates option for web-tier scale out Consider service operations and payload shapes Factors include change rates and trust boundaries Sample uses custom services and shared entities Mid-Tier Persistence Business Objects DAL Hand-Written Entity Framework Entity Partial Classes Context Entities Generated Demo #1: DAL and Business Objects Look at an EF Model based on Northwind Examine modifications for concurrency checks Debug optimistic concurrency in action DAL and Business Objects 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: EF storage/entity mapping generated context Business objects also generated, but extended with custom business logic Pay attention to concurrency Especially in disconnected scenarios T4 Control Code Template Output Text T4 Metadata Generated Code Demo #2: T4 and OnValidate Add T4 template to customize generated code Modify template for OnValidate mechanism Add partial class with validation code Test validation T4 and OnValidate Demo 2 Takeaways T4 makes customizing code generation easy EF4 will use T4 in a big way Register SavingChanges event handler during context construction EF4 makes SaveChanges virtual ObjectStateManager tracks all changes Use LINQ to Objects to interrogate it Avoid modifying other entities in OnValidate Web Tier & Repository Pattern Real Impl Repository Fake Impl Business Objects Web UI Demo #3: Fake Repository & Web UI Add ICustomerRepository interface Create fake implementation Pull in Web UI written using the repository Fake Repository & Web UI Demo 3 Takeaways Mental model of a repository is an in-memory collection Centralizes all data access in one place Easy to understand and tune if needed De-couples rest of application from data access Easily create fake or mock implementations Enables multiple real implementations Did I mention that it de-couples the rest of your application from data access? Cross-tier Communication Mid-tier Web-tier Repository WCF Client WCF Service Persistence Serialized Entities Demo #4: Connecting the Dots Turn ICustomerRepository into service contract Extend context class with AttachAsModified Add implementation of service contract Examine EF disconnected updates Test the implementation Examine web tier auto-proxy repository Run the final application Connecting the Dots Demo 4 Takeaways Concurrency for disconnected operations Maintain concurrency value from first read Change-tracking for disconnected operations Individual property tracking often unnecessary Disconnected graph manipulation Mixed operations quite difficult in 3.5 sp1 State transitions also constrained in 3.5 sp1 Context operations treat graph uniformly Improvements here in EF4 (APIs, Self-Tracking) What Next? Same patterns apply to .net 3.5 sp1 rich clients EF4 POCO support adds many options Completely separate business objects from DAL Serialize business objects to Silverlight, .Net 2.0, … Additional flexibility in business logic and testability Model-first enables generating DB Code-only removes need for separate metadata and mapping info More Resources Code EF: http://msdn.microsoft.com/en-us/data/aa937723.aspx EF FAQ: http://blogs.msdn.com/dsimmons/pages/entity-framework-faq.aspx ASP.Net MVC: http://www.asp.net/mvc Docs Sample: http://code.msdn.microsoft.com/dtl401sample T4 Templates: http://code.msdn.microsoft.com/t4ef35sp1 Blogs Mine: http://blogs.msdn.com/dsimmons EF Design: http://blogs.msdn.com/efdesign ADO.Net Team: http://blogs.msdn.com/adonet Related Content Breakout Sessions DAT308 Design Patterns for Application Architecture with the ADO.NET EF DTL05-INT Surprise! It's the Entity Framework Gotchas DTL312 The ADO.NET Entity Framework: Tips and Tricks Birds of a Feather BOF21 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 Labs DTL20-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 ticket http://www.microsoft.com/visualstudio http://www.microsoft.com/visualstudio/enus/products/teamsystem/default.mspx Please visit us in the TLC blue area Resources www.microsoft.com/teched www.microsoft.com/learning Sessions On-Demand & Community Microsoft Certification & Training Resources http://microsoft.com/technet http://microsoft.com/msdn Resources for IT Professionals Resources for Developers www.microsoft.com/learning Microsoft Certification and Training 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. Name Title Company Session Code: Video Title Customer Title Name Title Company Demo Title Name Title Company Partner Title Name Title Company Announcement Title PowerPoint Template Subtitle 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 left Font Size Requirements Main bullet points must not be smaller than 24pt Do NOT use any font size lower than 20pt Set subhead to 36pt or smaller so it will fit on a single line Turn off Auto Resizing on all text boxes Slide for Showing Software Code Use this layout to show software code The font is Consolas, a monospace font The slide doesn’t use bullets but levels can be indented using the “Increase List Level” icon on the Home menu To use straight quotes " instead of smart quotes ”, do this: 1.Click on the Office Button in the upper left corner 2.At the bottom of the menu, choose PowerPoint Options 3.From the left pane, select Proofing 4.Click on the AutoCorrect Options button 5.Select the AutoFormat As You Type tab, and deselect “Straight quotes” with “smart quotes”. Then Click OK. Table Format Table Title Column 1 Column 2 Column 3 Column 4 Column 5 Bar Chart Example Chart Title Series 1 Series 2 Series 3 5 4.5 4.4 4.3 3.5 3 2.4 2.5 2 Category 1 2 Category 2 2.8 1.8 Category 3 Category 4 Pie Chart Example 10% 23% 9% Chart Title 1st Qtr 58% Resources www.microsoft.com/teched www.microsoft.com/learning Sessions On-Demand & Community Microsoft Certification & Training Resources http://microsoft.com/technet http://microsoft.com/msdn Resources for IT Professionals Resources for Developers www.microsoft.com/learning Microsoft 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.