Presentation is loading. Please wait.

Presentation is loading. Please wait.

Jeff Derstadt Senior Development Lead Microsoft Corporation Patterns & Architecture.

Similar presentations


Presentation on theme: "Jeff Derstadt Senior Development Lead Microsoft Corporation Patterns & Architecture."— Presentation transcript:

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

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

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

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 public class BlogRepository { public BlogRepository(IUnitOfWork unitOfWork) {... } public IEnumerable GetAllBlogs() {... } public void AddPost(Post post) {... } public class BlogRepository { public BlogRepository(IUnitOfWork unitOfWork) {... } public IEnumerable 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 Repository interface is a strong contract Common Alternatives Query methods return IQueryable Repository base class

6

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

8

9 LowHigh Complexity DataSet Data Transfer Objects High Interoperability ?? Self-Tracking Entities

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

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

12

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

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

15

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/

17

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

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

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

21 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

22

23 Text Level 0 Text Level 1 Text Level 2 Text Level 3


Download ppt "Jeff Derstadt Senior Development Lead Microsoft Corporation Patterns & Architecture."

Similar presentations


Ads by Google