1 / 22

Entity Framework Deep Dive

Entity Framework Deep Dive. Patterns & Architecture. Jeff Derstadt Senior Development Lead Microsoft Corporation jeffders@microsoft.com. Agenda. Repository p attern in MVC Better testability Building N-tier Application Using Stored Procedures Code First Programming.

kolton
Download Presentation

Entity Framework Deep Dive

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Entity Framework Deep Dive Patterns & Architecture Jeff Derstadt Senior Development Lead Microsoft Corporation jeffders@microsoft.com

  2. Agenda • Repository pattern in MVC • Better testability • Building N-tier Application • Using Stored Procedures • Code First Programming

  3. Separation of concerns Centralize and encapsulate data access code Isolate upper layers from data access infrastructure Provide greater control over data access Repository Pattern Goals & Benefits Single Tier Multi Tier Web Silverlight User Interface Business Logic / Controller Data Access Layer Data Access Layer New Data Access Layer Fake Database Database

  4. 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 Repository Pattern Overview public class BlogRepository { public BlogRepository(IUnitOfWorkunitOfWork) { ... } public IEnumerable<Blog> GetAllBlogs() { ... } public void AddPost(Post post) { ... } }

  5. 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 Repository using Entity Framework

  6. Repository using Entity Framework Demo

  7. 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 Entity Framework Testability

  8. Entity Framework Testability Demo

  9. N-Tier Options High ?? Self-Tracking Entities DataSet Data Transfer Objects Interoperability Complexity Low High

  10. 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 Data Transfer Objects in EF 4.0

  11. EF 4.0 introduces self-tracking entities It’s easy to: Get started: Add the entity template to your project Make 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 Achieving Low Complexity

  12. Self Tracking Entities Demo

  13. 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 When to use self-tracking entities

  14. 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 Stored Procedures

  15. Stored Procedures Demo

  16. 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

  17. Code First Demo

  18. 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.) Code First

  19. 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 The Road Ahead

  20. 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 In Summary

  21. Thank you for your Attention! • 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

More Related