Building nTier Applications with Entity Framework Services
dotNetDave Conference DVD!Packed full of:Videos of all sessions from 2010 & 2011(1)!Slide decks from 2011 & 2010!Demo projects from 2011 & 2010!David McCarter’s .NET interview Questions!ExtrasConference Photos from 2010!Surprise videos!Book + DVD $25!Only $15!
Check Out Your Local User Groups!San Diego Cloud Computing User Groupwww.azureusergroup.com/group/sandiegoazureusergroupSan Diego .NET Developers Groupwww.sddotnetdg.orgSan Diego .NET User Groupwww.sandiegodotnet.comSan Diego SQL Server User Groupwww.sdsqlug.org
Agenda
Overview
Where’s Your Data Model?Applications Today…Implicitly Contain the Data ModelLogic and Model IntertwinedConceptual MismatchOften encapsulate in a "Data Access Layer"
Where’s Your Data Model?What Is Really Needed
Applications work with a well Defined Model
Storage Schema Abstraction
Declarative mapping between application and storage models
No brittle, hard-coded mappingUser ExperienceSecurityActive Directory, Card Space,  Windows Identity FoundationUI ComponentsWindows Forms, Windows Presentation Foundation, ASP.NET (AJAX, MVC), Silverlight, Windows MobilePresentation LayerLocal Storage/ CacheCommunications LayerWindows Communication Foundation (WCF), WCF Data Services, Web Services, Sync Services, Azure (Cloud), RIA Services, Workflow ServicesBusiness LayerBusiness EntitiesT4 TemplatesBusiness ComponentsBusiness WorkflowWindows Workflow FoundationData LayerEntity Framework, LINQ to SQL, DataSetsSQL Server
Object Relational Mapping
The DilemmaSoftware Architects & DevelopersDatabase Designers & Administrators
 Object Relational MappingWhat is ORM?Technique for working with relational tables as if they were objects in memoryIntention is to hide away the complexity of the underlying tables and give a uniform way of working with data
 Object Relational MappingWhy use ORM?ProductivityRetain database independenceDatabase portabilityObjects ► Classes ► Entities
Many attemptsTyped DatasetsObjectspaces ‘v1’Objectspaces ‘v2’Microsoft Business FrameworkWinFSLinq to SQLNHibernate Will “RIP” Not Microsoft’s
Entity Framework
The Microsoft Entity Data ModelAn extended relational model with Entity-Relationship Model  conceptsEntity TypesStrong type with IdentityInheritanceScalar/Complex propertiesEntitySetsHold instances of Entity TypesSimilar to relational tablesCan have multiple Entitysets of the same EntityTypesRelationships ("Associations")Named relationships between Entities0..1:*, 0..1:0..1, 1:1, 1:M, M:NNavigation may be exposed as NavigationProperties on EntityTypesAssociationSetsContains instances of associationsMay be queried directlyEntityContainersContains EntitySets, AssociationSetsSalesPersonEmployeeID = 294272LoginID = adamTitle = "Dev Lead"VacationHours = 0…Manager11NReportsSalesPersonSalesPersonEmployeeID = 729742LoginID = peteTitle = "Developer"VacationHours = 0…ExpenseAccount = …CarLicenseNum = ……SalesPersonEmployeeID = 729742LoginID = peteTitle = "Developer"VacationHours = 0…ExpenseAccount = …CarLicenseNum = ……EmployeeID = 729742LoginID = peteTitle = "Developer"VacationHours = 0…ExpenseAccount = true…
EDM & Entity Framework?The Entity Framework (EF) is an Object Relational Modeling tool leveraging the EDMFocus on your domain, not how to persist!EDM is used to describe your model.Allows different rate of change between database and code!EF uses a storage model and mapping to enable this.
IEnumerable<T>LINQ to EntitiesEntity SQL QueryObject ServicesEDMDataReaderEntity SQLConceptual ModelMappingEntity Data ProviderDataReaderSQLStorage ModelADO.NET Data ProviderEF Model
Performance & Security?Connections to database vs. amount of dataYou can work with stored procedures You can work with viewsYou can define how the ADO.NET Entity Framework loads your data to Eager Lazy(Explicit)
Entity Framework in .NET 4Model FirstT4 Templates to control code generationSupporting DDD approach with POCO Persistence IgnoranceRepository patternQuery ImprovementsLazy LoadingDisconnected API’s to enable N-tier
Easily Expose Your EntitiesWCF Data Services
WCF Data Services OverviewFramework from Microsoft that that facilitates the creation of RESTful data services via HTTP endpointsShipped with .NET 3.5 SP1Updates for 3.5 SP1 and .NET 4.0Built on top of WCF4Data representation in ATOM or JSON
WCF Data Services OverviewAutomates the exposure of data as RESTful resourcesAnything that implements IQueryable<T> and IUpdatable<T>Entity Framework3rd party ORMsLINQ to SQL, custom data driven CLR classesCustom URI based querying
HTTPhttp://localhost:1234/MyData.svc/MyEntities(10)Open Data Protocol (OData)Hosting/HTTP ListenerData Services Runtimevar q = from e in MyEntities where id = 10 select e;Data Access LayerEntityFrameworkCustom LINQ ProviderSELECT * FROM MyEntity WHERE id = 10Relational DatabaseOther Sources
Data Services - A REST InterfaceThe underlying model
Entities and associations  resources and linksURL ConventionsAddressing entities and setsPresentation optionsWCF Data Services in .NET 4Data BindingNew DataServiceCollection class provides simplified binding of data service data to (WPF) controls. Includes auto updating.Counting Entities in an Set $count - total number of resources returned by a URI$inlinecount - total resource count together with the resource dataServer-driven Paging Service can be configured to return requested resources as a set of paged responses
WCF Data Services in .NET 4Query ProjectionsQuery results can be modified to include only a subset of properties by using the new $select query option.Custom Data Service ProvidersCan implement a set of new data service provider interfaces, you can use various types of data with a data service, even when the data model changes during execution.Streaming of Binary ResourcesThis enables you to retrieve and save binary large object data independent of the entity to which it belongs.
Summary
ResourcesADO.NET Team Bloghttp://blogs.msdn.com/adonet/default.aspxEF Design Blogblogs.msdn.com/efdesignVisual Studio Data Bloghttp://blogs.msdn.com/vsdata/dnrTV!http://shrinkster.com/1734http://shrinkster.com/1735
ResourcesADO.NET team bloghttp://blogs.msdn.com/adonetWCF Data Services team bloghttp://blogs.msdn.com/astoriateam

Building nTier Applications with Entity Framework Services

  • 1.
    Building nTier Applicationswith Entity Framework Services
  • 3.
    dotNetDave Conference DVD!Packedfull of:Videos of all sessions from 2010 & 2011(1)!Slide decks from 2011 & 2010!Demo projects from 2011 & 2010!David McCarter’s .NET interview Questions!ExtrasConference Photos from 2010!Surprise videos!Book + DVD $25!Only $15!
  • 4.
    Check Out YourLocal User Groups!San Diego Cloud Computing User Groupwww.azureusergroup.com/group/sandiegoazureusergroupSan Diego .NET Developers Groupwww.sddotnetdg.orgSan Diego .NET User Groupwww.sandiegodotnet.comSan Diego SQL Server User Groupwww.sdsqlug.org
  • 5.
  • 6.
  • 7.
    Where’s Your DataModel?Applications Today…Implicitly Contain the Data ModelLogic and Model IntertwinedConceptual MismatchOften encapsulate in a "Data Access Layer"
  • 8.
    Where’s Your DataModel?What Is Really Needed
  • 9.
    Applications work witha well Defined Model
  • 10.
  • 11.
    Declarative mapping betweenapplication and storage models
  • 12.
    No brittle, hard-codedmappingUser ExperienceSecurityActive Directory, Card Space, Windows Identity FoundationUI ComponentsWindows Forms, Windows Presentation Foundation, ASP.NET (AJAX, MVC), Silverlight, Windows MobilePresentation LayerLocal Storage/ CacheCommunications LayerWindows Communication Foundation (WCF), WCF Data Services, Web Services, Sync Services, Azure (Cloud), RIA Services, Workflow ServicesBusiness LayerBusiness EntitiesT4 TemplatesBusiness ComponentsBusiness WorkflowWindows Workflow FoundationData LayerEntity Framework, LINQ to SQL, DataSetsSQL Server
  • 13.
  • 14.
    The DilemmaSoftware Architects& DevelopersDatabase Designers & Administrators
  • 15.
    Object RelationalMappingWhat is ORM?Technique for working with relational tables as if they were objects in memoryIntention is to hide away the complexity of the underlying tables and give a uniform way of working with data
  • 16.
    Object RelationalMappingWhy use ORM?ProductivityRetain database independenceDatabase portabilityObjects ► Classes ► Entities
  • 17.
    Many attemptsTyped DatasetsObjectspaces‘v1’Objectspaces ‘v2’Microsoft Business FrameworkWinFSLinq to SQLNHibernate Will “RIP” Not Microsoft’s
  • 18.
  • 19.
    The Microsoft EntityData ModelAn extended relational model with Entity-Relationship Model conceptsEntity TypesStrong type with IdentityInheritanceScalar/Complex propertiesEntitySetsHold instances of Entity TypesSimilar to relational tablesCan have multiple Entitysets of the same EntityTypesRelationships ("Associations")Named relationships between Entities0..1:*, 0..1:0..1, 1:1, 1:M, M:NNavigation may be exposed as NavigationProperties on EntityTypesAssociationSetsContains instances of associationsMay be queried directlyEntityContainersContains EntitySets, AssociationSetsSalesPersonEmployeeID = 294272LoginID = adamTitle = "Dev Lead"VacationHours = 0…Manager11NReportsSalesPersonSalesPersonEmployeeID = 729742LoginID = peteTitle = "Developer"VacationHours = 0…ExpenseAccount = …CarLicenseNum = ……SalesPersonEmployeeID = 729742LoginID = peteTitle = "Developer"VacationHours = 0…ExpenseAccount = …CarLicenseNum = ……EmployeeID = 729742LoginID = peteTitle = "Developer"VacationHours = 0…ExpenseAccount = true…
  • 20.
    EDM & EntityFramework?The Entity Framework (EF) is an Object Relational Modeling tool leveraging the EDMFocus on your domain, not how to persist!EDM is used to describe your model.Allows different rate of change between database and code!EF uses a storage model and mapping to enable this.
  • 21.
    IEnumerable<T>LINQ to EntitiesEntitySQL QueryObject ServicesEDMDataReaderEntity SQLConceptual ModelMappingEntity Data ProviderDataReaderSQLStorage ModelADO.NET Data ProviderEF Model
  • 22.
    Performance & Security?Connectionsto database vs. amount of dataYou can work with stored procedures You can work with viewsYou can define how the ADO.NET Entity Framework loads your data to Eager Lazy(Explicit)
  • 25.
    Entity Framework in.NET 4Model FirstT4 Templates to control code generationSupporting DDD approach with POCO Persistence IgnoranceRepository patternQuery ImprovementsLazy LoadingDisconnected API’s to enable N-tier
  • 26.
    Easily Expose YourEntitiesWCF Data Services
  • 27.
    WCF Data ServicesOverviewFramework from Microsoft that that facilitates the creation of RESTful data services via HTTP endpointsShipped with .NET 3.5 SP1Updates for 3.5 SP1 and .NET 4.0Built on top of WCF4Data representation in ATOM or JSON
  • 28.
    WCF Data ServicesOverviewAutomates the exposure of data as RESTful resourcesAnything that implements IQueryable<T> and IUpdatable<T>Entity Framework3rd party ORMsLINQ to SQL, custom data driven CLR classesCustom URI based querying
  • 29.
    HTTPhttp://localhost:1234/MyData.svc/MyEntities(10)Open Data Protocol(OData)Hosting/HTTP ListenerData Services Runtimevar q = from e in MyEntities where id = 10 select e;Data Access LayerEntityFrameworkCustom LINQ ProviderSELECT * FROM MyEntity WHERE id = 10Relational DatabaseOther Sources
  • 30.
    Data Services -A REST InterfaceThe underlying model
  • 31.
    Entities and associations resources and linksURL ConventionsAddressing entities and setsPresentation optionsWCF Data Services in .NET 4Data BindingNew DataServiceCollection class provides simplified binding of data service data to (WPF) controls. Includes auto updating.Counting Entities in an Set $count - total number of resources returned by a URI$inlinecount - total resource count together with the resource dataServer-driven Paging Service can be configured to return requested resources as a set of paged responses
  • 32.
    WCF Data Servicesin .NET 4Query ProjectionsQuery results can be modified to include only a subset of properties by using the new $select query option.Custom Data Service ProvidersCan implement a set of new data service provider interfaces, you can use various types of data with a data service, even when the data model changes during execution.Streaming of Binary ResourcesThis enables you to retrieve and save binary large object data independent of the entity to which it belongs.
  • 35.
  • 36.
    ResourcesADO.NET Team Bloghttp://blogs.msdn.com/adonet/default.aspxEFDesign Blogblogs.msdn.com/efdesignVisual Studio Data Bloghttp://blogs.msdn.com/vsdata/dnrTV!http://shrinkster.com/1734http://shrinkster.com/1735
  • 37.
    ResourcesADO.NET team bloghttp://blogs.msdn.com/adonetWCFData Services team bloghttp://blogs.msdn.com/astoriateam
  • 38.

Editor's Notes

  • #7 Goals:To show a new way of serving up dateGet you thinking to learn more when you leave this presentation
  • #12 Maintainabity, security, efficiency andscalability let a DBA decideto:Splitdata of oneentityintoseveraltablesPutting data forseveral types of entitiesintosametableSupply projectionsthatlooseentity concepts thatmight have been present in tablestructures
  • #13 Talk about the separation between database developer and programmer.
  • #14 Talk about the separation between database developer and programmer.
  • #15 Typed Datasets – shippedObjectspaces ‘v1’ – never shippedObjectspaces ‘v2’ – never shippedMicrosoft Business Framework – never shippedWinFS – never shippedLinq to SQL – shipped, won’t be discontinuedLinq to Entities – shippedEntity Framework- shipped 
  • #18 Applications can work in terms of a more application-centric conceptual model, including types with inheritance, complex members, and relationships.Applications are freed from hard-coded dependencies on a particular data engine or storage schema.Mappings between the conceptual model and the storage-specific schema can change without changing the application code.Developers can work with a consistent application object model that can be mapped to various storage schemas, possibly implemented in different database management systems.Multiple conceptual models can be mapped to a single storage schema.Language-integrated query (LINQ) support provides compile-time syntax validation for queries against a conceptual model.
  • #19 Explain the inner workings of the entity framework Three types of commands: LINQ to Entities against Object Services Materialization to Conceptual model classes or anonymous classes if projected Entity Sqlagaints Object Services Materialization if no projection + streamed data on row level Call model defines functions or specific Entity SQL functions. Entity Sql against Entity Client No materialization + streamed data on column levelExplain the path of commands…
  • #20 http://www.robbagby.com/entity-framework/is-lazy-loading-in-ef-4-evil-or-the-second-coming/
  • #21 Demo: AdventureWorks.DataDemo: AdvertureWorks.ExampleDemo: EntityDesignerDiagram.png
  • #32 Demo: AdvertureWorksServiceDemo: AdventureWorks.Client