Lerman Adx303 Entity Framework 4 In Aspnet

Julie Lerman
Julie LermanSoftware Developer at Self Employed
ADX303:
Options for Building ASP.NET
Web Sites with Entity Framework
Julie Lerman
www.thedatafarm.com
jlerman@thedatafarm.com
Julie Lerman
website
theDataFarm.com
blog & twitter
theDataFarm.com/blog
@julielermanVT
book web site
LearnEntityFramework.com
consultant/mentor
Microsoft MVP, INETA Speaker,
ASPInsider, MCP, VTdotNET Leader
Agenda
• Architectural Challenges
• EntityDataSource
• Dynamic Data
• ASP.NET MVC
• ObjectDataSource
• Architected n-Tier Web Forms
SaveChanges Creates db Commands
• Insert Command
• From: Current values
EntityState =
Added
• Delete Command
• From: EntityKey & FK values
EntityState=
Deleted
• UpdateCommand
• From: Current vs. Original
EntityState=
Modified
Change Tracking Across Tiers
ObjectContext
Sales
Order #1
Line
Item A
Line
Item B
Sales
Order #2
Line
Item C
Line
Item D
detach
entities
ObjectStateEntry
SO1 EntityKey
Original Values
Current Values
Other ∆ Info
ObjectStateEntry
SO2 EntityKey
Original Values
Current Values
Other ∆ Info
ObjectStateEntry
LIA EntityKey
Original Values
Current Values
Other ∆ Info
ObjectStateEntry
LIB EntityKey
Original Values
Current Values
Other ∆ Info
Original Property
Values
EntityState Values
SaveChanges Fails Across Post Backs
New Page
Class
Create New
ObjectContext
Get Entities
Build HTML using Data
& ASP.NET Markup
Destroy Page and
Dependents
Page Request
New Page
Class
Create New
ObjectContext
Process
Request
Build HTML using Data
& ASP.NET Markup
Destroy Page and
Dependents
Post Back #1
New Page
Class
Create New
ObjectContext
Get Entities
Build HTML using Data
& ASP.NET Markup
Destroy Page and
Dependents
Post Back #2
The ASP.net Spectrum with EF
Entity
Data
Source
Dynamic
Data for
Entities
Object
Data
Source
ADO.NET
Data
Services
WCF
n-Tier
Dev
Architect
MVC
EF4 Changes to the Rescue
• Foreign Keys
● Big help for data-binding related objects
• POCO Classes & T4 Generation
• State Methods
● ApplyCurrentValues, ApplyOriginalValues
● ChangeState
● ChangeRelationshipState (when no FKs)
EntityDataSource Control
• RAD
• Simple, Drag & Drop
• Lots of Event Handlers to tap into
Pros
• Data access tied to the UI
• Constrained by limitations of EDS
• Can’t update graphs
Cons
Dynamic Data Controls
• RAD
• Uses EntityDataSource
• Dynamically created from entities
• Can be customized
Pros
• Uses EntityDataSource
• DataAccess still bound to UICons
ASP.NET Model View Controller
• MVC Pattern using Entities as the model
• MVC Goal
● Separation of concerns
● Testable
• Entities as “Model”
• EF4’s POCO entities for better separation
ObjectDataSource Control
• Use your own business objects
• Repositories to interact with ObjectContext
• Lots of Event Handlers to tap into
Pros
• Classes must comply w ODS rules
• Lose ability to work with graphsCons
Classes
Queries
(Repository B)
Tests
UI
EF Classes in a Repository
System.Data.Entity
EDM &
ObjectContext
(Repository A)
Mock
Context A
Mock
ObjectSet
Mock
Context B
Julie Lerman Oredev 2009
Basic Pattern for ASP.NET n-Tier
Code
Behind
Business
Logic
Repository
Queries Cache
EntityKeys, Scalars
(inc FKs)
Foreign Keys make this
approach a possibility
MVC View
MVC
Controller Model &
ObjectContext
ASPX
N-Tier ASP.NET
• Serious challenges in EFv1
• Help in EF4
● POCO objects & T4 Code Generation
● Caching gets simpler
● Foreign Keys
● New n-Tier methods
• ChangeState, ApplyCurrentValues,
ApplyOriginalValues, ChangeRelationship
Caching Considerations
• SessionState vs ViewState vs Cache
• Cache with Velocity or MemCached
• Cache vs Refresh from DB for Updates?
• Roll your own? Use a provider?
• Anyway you go – it’s still HARD
Summary
• ASP.NET provides many ways to use EF
• New EF4 features expand possibilities
• RAD: Dynamic Data & EntityDataSource
• Business Layer “Lite”: ObjectDataSource
• MVC Benefits from POCOs
• Web Forms n-Tier helped by Foreign Keys
Julie Lerman
website
theDataFarm.com
blog & twitter
theDataFarm.com/blog
@julielermanVT
book web site
LearnEntityFramework.com
consultant/mentor
Microsoft MVP, INETA Speaker,
ASPInsider, MCP, VTdotNET Leader
Resources
• EF Team Blogs (blogs.msdn.com/)
● adonet, efdesign, dsimmons, adjames, more..
• Danny Simmons’ Aug 2009 MSDN Mag
• MS PAG: Data Access Guidance
● dataguidance.codeplex.com
1 of 19

More Related Content

What's hot(9)

Relay: Seamless Syncing for React (VanJS)Relay: Seamless Syncing for React (VanJS)
Relay: Seamless Syncing for React (VanJS)
Brooklyn Zelenka1.5K views
 Intro to GraphQL Intro to GraphQL
Intro to GraphQL
Rakuten Group, Inc.1.6K views
AngularAngular
Angular
Vinod Kumar Kayartaya84 views
Laravel 5 and SOLIDLaravel 5 and SOLID
Laravel 5 and SOLID
Igor Talevski10.2K views
Angular directives and pipesAngular directives and pipes
Angular directives and pipes
Knoldus Inc.396 views
From Legacy to Hexagonal (An Unexpected Android Journey)From Legacy to Hexagonal (An Unexpected Android Journey)
From Legacy to Hexagonal (An Unexpected Android Journey)
Jose Manuel Pereira Garcia3.8K views
Angular 4 and TypeScriptAngular 4 and TypeScript
Angular 4 and TypeScript
Ahmed El-Kady1K views

More from Julie Lerman(20)

Lerman Adx303 Entity Framework 4 In Aspnet

  • 1. ADX303: Options for Building ASP.NET Web Sites with Entity Framework Julie Lerman www.thedatafarm.com jlerman@thedatafarm.com
  • 2. Julie Lerman website theDataFarm.com blog & twitter theDataFarm.com/blog @julielermanVT book web site LearnEntityFramework.com consultant/mentor Microsoft MVP, INETA Speaker, ASPInsider, MCP, VTdotNET Leader
  • 3. Agenda • Architectural Challenges • EntityDataSource • Dynamic Data • ASP.NET MVC • ObjectDataSource • Architected n-Tier Web Forms
  • 4. SaveChanges Creates db Commands • Insert Command • From: Current values EntityState = Added • Delete Command • From: EntityKey & FK values EntityState= Deleted • UpdateCommand • From: Current vs. Original EntityState= Modified
  • 5. Change Tracking Across Tiers ObjectContext Sales Order #1 Line Item A Line Item B Sales Order #2 Line Item C Line Item D detach entities ObjectStateEntry SO1 EntityKey Original Values Current Values Other ∆ Info ObjectStateEntry SO2 EntityKey Original Values Current Values Other ∆ Info ObjectStateEntry LIA EntityKey Original Values Current Values Other ∆ Info ObjectStateEntry LIB EntityKey Original Values Current Values Other ∆ Info Original Property Values EntityState Values
  • 6. SaveChanges Fails Across Post Backs New Page Class Create New ObjectContext Get Entities Build HTML using Data & ASP.NET Markup Destroy Page and Dependents Page Request New Page Class Create New ObjectContext Process Request Build HTML using Data & ASP.NET Markup Destroy Page and Dependents Post Back #1 New Page Class Create New ObjectContext Get Entities Build HTML using Data & ASP.NET Markup Destroy Page and Dependents Post Back #2
  • 7. The ASP.net Spectrum with EF Entity Data Source Dynamic Data for Entities Object Data Source ADO.NET Data Services WCF n-Tier Dev Architect MVC
  • 8. EF4 Changes to the Rescue • Foreign Keys ● Big help for data-binding related objects • POCO Classes & T4 Generation • State Methods ● ApplyCurrentValues, ApplyOriginalValues ● ChangeState ● ChangeRelationshipState (when no FKs)
  • 9. EntityDataSource Control • RAD • Simple, Drag & Drop • Lots of Event Handlers to tap into Pros • Data access tied to the UI • Constrained by limitations of EDS • Can’t update graphs Cons
  • 10. Dynamic Data Controls • RAD • Uses EntityDataSource • Dynamically created from entities • Can be customized Pros • Uses EntityDataSource • DataAccess still bound to UICons
  • 11. ASP.NET Model View Controller • MVC Pattern using Entities as the model • MVC Goal ● Separation of concerns ● Testable • Entities as “Model” • EF4’s POCO entities for better separation
  • 12. ObjectDataSource Control • Use your own business objects • Repositories to interact with ObjectContext • Lots of Event Handlers to tap into Pros • Classes must comply w ODS rules • Lose ability to work with graphsCons
  • 13. Classes Queries (Repository B) Tests UI EF Classes in a Repository System.Data.Entity EDM & ObjectContext (Repository A) Mock Context A Mock ObjectSet Mock Context B Julie Lerman Oredev 2009
  • 14. Basic Pattern for ASP.NET n-Tier Code Behind Business Logic Repository Queries Cache EntityKeys, Scalars (inc FKs) Foreign Keys make this approach a possibility MVC View MVC Controller Model & ObjectContext ASPX
  • 15. N-Tier ASP.NET • Serious challenges in EFv1 • Help in EF4 ● POCO objects & T4 Code Generation ● Caching gets simpler ● Foreign Keys ● New n-Tier methods • ChangeState, ApplyCurrentValues, ApplyOriginalValues, ChangeRelationship
  • 16. Caching Considerations • SessionState vs ViewState vs Cache • Cache with Velocity or MemCached • Cache vs Refresh from DB for Updates? • Roll your own? Use a provider? • Anyway you go – it’s still HARD
  • 17. Summary • ASP.NET provides many ways to use EF • New EF4 features expand possibilities • RAD: Dynamic Data & EntityDataSource • Business Layer “Lite”: ObjectDataSource • MVC Benefits from POCOs • Web Forms n-Tier helped by Foreign Keys
  • 18. Julie Lerman website theDataFarm.com blog & twitter theDataFarm.com/blog @julielermanVT book web site LearnEntityFramework.com consultant/mentor Microsoft MVP, INETA Speaker, ASPInsider, MCP, VTdotNET Leader
  • 19. Resources • EF Team Blogs (blogs.msdn.com/) ● adonet, efdesign, dsimmons, adjames, more.. • Danny Simmons’ Aug 2009 MSDN Mag • MS PAG: Data Access Guidance ● dataguidance.codeplex.com