220 likes | 487 Views
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.
E N D
Entity Framework Deep Dive Patterns & Architecture Jeff Derstadt Senior Development Lead Microsoft Corporation jeffders@microsoft.com
Agenda • Repository pattern in MVC • Better testability • Building N-tier Application • Using Stored Procedures • Code First Programming
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
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) { ... } }
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
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
N-Tier Options High ?? Self-Tracking Entities DataSet Data Transfer Objects Interoperability Complexity Low High
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
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
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
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
Stored Procedures Demo
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
Code First Demo
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
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
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
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