Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

ADO.NET Entity Framework & Your Data Access Layer

43,049 views

Published on

This presentation highlights three ways in which to use the ADO.NET Enterprise Framework in conjunction with your DAL: 1) EF == DAL, 2) Encapsulation of EF, and 3) Partial Encapsulation of EF.

Published in: Technology
  • Be the first to comment

ADO.NET Entity Framework & Your Data Access Layer

  1. 1. ADO.NET Entity Framework & Your Data Access Layer<br />Wade Wegner<br />Architect Evangelist<br />Microsoft Corporation<br />wade.wegner@microsoft.com<br />http://www.architectingwith.net/<br />http://www.twitter.com/wadewegner<br />
  2. 2. Agenda<br />What is the ADO.NET Entity Framework<br />How do you structure your DAL?<br />EF == DAL<br />Full Encapsulation<br />Partial Encapsulation<br />ADO.NET Entity Framework 4.0<br />Existing Questions<br />
  3. 3. Object Relational Mapper<br />What does it do?<br />Persistence<br />Querying<br />Manages transactions<br />Manages concurrency<br />It’s a tool<br />CRUD<br />Query model<br />Mapping<br />
  4. 4. Entity Framework<br />What does it do?<br />Persistence<br />Querying<br />Manages transactions<br />Manages concurrency<br />It’s a tool<br />CRUD<br />Query model<br />Mapping<br />
  5. 5. The Entity Framework is an O/RM<br />
  6. 6.
  7. 7. Data Sources<br />User Presentation<br />var apartments = <br /> from a in db.Apartments<br /> where a.State == “IL”<br /> select a;<br />Business Logic<br />Data Access Logic<br />The ultimate question<br />Where does this go?<br />
  8. 8. Separation of Concerns<br />High Cohesion<br />Loose Coupling<br />
  9. 9. What if there was no DAL?<br />Is the EDM our DAL? <br />EF Entities? Are these our business objects? Data Transfer Objects (DTO’s)<br />What if we wrote LINQ queries directly in our business layer?<br />Separation of Concerns?<br />High cohesion?<br />Loose Coupling?<br />
  10. 10. Option 1: Entity Framework as a DAL<br />var bo = new CategoryBusiness(); <br />bo.Load();<br />this.categoryGridView.DataSource = bo.ActiveCategories;<br />UI Presentation<br />var db = new AdventureWorksEntities(); var activeCategories = <br /> from category in db.ProductCategory             where category.Inactive != true             orderby category.Name             select category;<br />Business<br />Data Access<br />SS<br />MSL<br />CS<br />Entities<br />Business Objects<br />Forms<br />ADO.NET Entity Client<br />ADO.NET Object<br />Services<br />
  11. 11. Entity Framework as a DAL<br />Pros<br />Excellent Isolation from DB Schema<br />Independence from RDBMS<br />Object Services: Identity & Change Tracking<br />Full query comprehension over CDM in business logic<br />Cons<br />Pre-.NET 4.0, “Object First” is not well support scenario<br />Pre-.NET 4.0, Entities are not very pure (not POCO)<br />Can’t easily switch ORM<br />Data validation support is limited.<br />
  12. 12. Validation & Business Logic<br />public string Phone<br />{<br /> get<br /> {<br /> return this._Phone;<br /> }<br /> set<br /> {<br />this.OnPhoneChanging(value);<br />this.ReportPropertyChanging(&quot;Phone&quot;);<br />this._Phone = <br />DataClasses.StructuralObject.SetValidValue(value, true);<br />this.ReportPropertyChanged(&quot;Phone&quot;);<br />this.OnPhoneChanged();<br /> }<br />}<br />public partial class Customer<br />{<br /> partial void OnPhoneChanging(string value)<br /> {<br /> if (value.Length &lt; 7)<br />MyCustomErrorHandler.SetError(this.EntityKey, <br /> &quot;Invalid Phone Number&quot;);<br /> }<br />}<br />
  13. 13. Is the Entity Framework the last<br />DAL technology you will use?<br />Encapsulate<br />
  14. 14. Why Encapsulate?<br />Separation of Concerns<br />Testability<br />Embed additional logic<br />Performance/Control<br />Control ObjectContext Lifetime<br />Control SQL Generation<br />Eager Loading<br />How can we do this and still keep the power of the Entity Framework?<br />
  15. 15. Encapsulation Approaches<br />Full Encapsulation<br />EF Entities are merely a DTO used in your data access layer.<br />ObjectContext is a Data Access Component<br />Partial Encapsulation<br />Expose Queryable Objects<br />Encapsulate ObjectContext<br />Hybrid<br />Mix & match as appropriate<br />
  16. 16. Option 2: Full Encapsulation<br />Return an instance/collection of Custom Class<br />Pros<br />Simplified Queries<br />POCO Objects <br />ORM & RDBMS Agnostic, No EF References<br />Discrete set of SQL Queries<br />Cons<br />No change tracking or identity management<br />Additional Object Materialization<br />No query composition: secondary LINQ queries are just enumerating.<br />
  17. 17. Option 3: Partial Encapsulation<br />Three techniques<br />IEnumerable<br />IQueryable<br />ObjectQuery<br />Pros<br />Full benefits of CDM<br />Query composition<br />Simplified queries (Navigation vs. joins)<br />Some independence from ORM<br />Object Identity & Change Tracking<br />Cons<br />Use of EF Entities has implications on Business Layer.<br />What are all my SQL Queries? Hard to answer.<br />
  18. 18. Three DAL Options<br />EF == DAL<br />Full Encapsulation<br />Partial Encapsulation<br />
  19. 19. Entity Framework 4.0<br />Development Approaches<br />Model first development<br />Testability<br />Architectural Concerns<br />Persistent Ignorance (e.g. POCO)<br />Application Patterns (Repository and UnitOfWork patterns)<br />N-Tier applications<br />Improvements<br />Customization of code generation<br />Pluralization and singularization, lazy loading, SPs<br />Customizing queries<br />SQL generation readability & query improvements<br />
  20. 20. More information<br />ADO.NET team blog: http://blogs.msdn.com/adonet/default.aspx<br />MSDN: http://msdn.microsoft.com/en-us/library/bb399572.aspx<br />10-4: http://channel9.msdn.com/shows/10-4/<br />

×