entity framework 4

37
www.seavus.com EF4 through POCO 11.02.2010 Marjan Nikolovski

Upload: marjan-nikolovski

Post on 11-May-2015

1.146 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Entity Framework 4

www.seavus.com

EF4 through POCO

11.02.2010

Marjan Nikolovski

Page 2: Entity Framework 4

Agenda

Page 2Date: 11.02.2010Author: Marjan Nikolovski

• Introduction• Installation• Getting started• Loading entity properties• Tracking changes• Working with proxy enabled data• Serialization of proxy objects for WCF usage• Technology performance• Pros and Cons

Page 3: Entity Framework 4

Introduction

Page 3Date: 11.02.2010Author: Marjan Nikolovski

• Yet Another ORM tool ?• Custom classes as data proxy• POCO acronym• Before and Now

Page 4: Entity Framework 4

Yet Another ORM tool ?

Date Page 4Author

• How it is different from the previous known• POCO vs. nHybernate, nHydrate, LINQ-SQL

Date: 11.02.2010Author: Marjan Nikolovski

POCO nHybernate

nHydrate LINK-SQL

portable

easy extensible

visual mapper

mature

Support LINQ

Page 5: Entity Framework 4

Date Page 5Author

POCO acronym

• Plain Old CLR Object• Acronym for non-persistant data (persistant ignorant)

Date: 11.02.2010Author: Marjan Nikolovski

Page 6: Entity Framework 4

Before

Date Page 6Author

• Unreadable code• Bad programming style• Almost no extensible designs• Lot of bugs• Knotted logic

Date: 11.02.2010Author: Marjan Nikolovski

Page 7: Entity Framework 4

Date Page 7Author

• Forgotten CRUD principles• Security issues

Date: 11.02.2010Author: Marjan Nikolovski

Before

Page 8: Entity Framework 4

Date Page 8Author

• Unclear DDL/BLL logic

Date: 11.02.2010Author: Marjan Nikolovski

Before

Page 9: Entity Framework 4

EF4 POCO now offers you:

Date Page 9Author

• Efficiency

Date: 11.02.2010Author: Marjan Nikolovski

Page 10: Entity Framework 4

Date Page 10Author

• Easy DB switching

Date: 11.02.2010Author: Marjan Nikolovski

EF4 POCO now offers you:

Page 11: Entity Framework 4

Date Page 11Author

• Less time getting nervous with debugging queries.

Date: 11.02.2010Author: Marjan Nikolovski

EF4 POCO now offers you:

Page 12: Entity Framework 4

Installation

Date Page 12Author

• Visual Studio 2010 beta 2• ADO.NET Entity Framework Feature Community

Technology Preview 2

Date: 11.02.2010Author: Marjan Nikolovski

Page 13: Entity Framework 4

Getting started

Date Page 13Author

• Mapping requirements• DB mapping and POCO class generation• POCO code generator• Classes handcrafting

• Data model to physical model

Date: 11.02.2010Author: Marjan Nikolovski

Page 14: Entity Framework 4

Date Page 14Author

Mapping requirementsGetting started

• Custom data class must not be non inheritable, abstract or private • Custom data class must have a public parameter less constructor • For lazy loading custom data class navigational properties must be

defined as overrideable (virtual).• Classes must not have any mapping attributes and must not

implement IEntityWithChangeTracker or IEntityWithRelationships interfaces.

• ProxyCreationEnabled in the ObjectContext’s ContextOptions must be set to true (default is true) for ObjectContext tracking.

Date: 11.02.2010Author: Marjan Nikolovski

Page 15: Entity Framework 4

Date Page 15Author

Mapping requirementsDB mapping and POCO class generation• Add Entity Model• Mapping DB to Model

Date: 11.02.2010Author: Marjan Nikolovski

Page 16: Entity Framework 4

Date Page 16Author

Mapping requirementsDB mapping and POCO class generationRemove custom tool

Date: 11.02.2010Author: Marjan Nikolovski

Page 17: Entity Framework 4

Date Page 17Author

Mapping requirementsDB mapping and POCO class generationClasses handcrafting

Date: 11.02.2010Author: Marjan Nikolovski

Page 18: Entity Framework 4

Date Page 18Author

Mapping requirementsDB mapping and POCO class generationClasses handcraftingCreate DB Context

Date: 11.02.2010Author: Marjan Nikolovski

Page 19: Entity Framework 4

Date Page 19Author

Mapping requirementsDB mapping and POCO class generationClasses handcraftingIn practice

Date: 11.02.2010Author: Marjan Nikolovski

Page 20: Entity Framework 4

Date Page 20Author

Mapping requirementsData model to physical model• Same protocol used descripted above only bottom

up strategy used• Create sql script

Date: 11.02.2010Author: Marjan Nikolovski

Page 21: Entity Framework 4

Loading entity navigational properties

Date Page 21Author

• Navigational properties• Explicit loading• Lazy loading• Eager loading

Date: 11.02.2010Author: Marjan Nikolovski

Page 22: Entity Framework 4

Loading entity navigational propertiesExplicit loading

Date Page 22Author

Date: 11.02.2010Author: Marjan Nikolovski

Lazy loading property in Object Context class must be set to false

Page 23: Entity Framework 4

Loading entity navigational propertiesLazy loading

Date Page 23Author

Date: 11.02.2010Author: Marjan Nikolovski

Lazy loading property in Object Context class must be set to truein order set auto navigation data loading

Page 24: Entity Framework 4

Loading entity navigational propertiesEager loading

Date Page 24Author

Date: 11.02.2010Author: Marjan Nikolovski

Eager loading is done by calling include method in LINQ query

Page 25: Entity Framework 4

Tracking changes

Date Page 25Author

• Proxy enabled mapping• Sharpshooting (self organized tracking)

Date: 11.02.2010Author: Marjan Nikolovski

Page 26: Entity Framework 4

Tracking changesProxy enabled mapping

Date Page 26Author

• Automatic tracking of changes in the Context

Date: 11.02.2010Author: Marjan Nikolovski

Page 27: Entity Framework 4

Tracking changesSharpshooting (self organized tracking)

Date Page 27Author

• Manual tracking of changes in the Context

Date: 11.02.2010Author: Marjan Nikolovski

Page 28: Entity Framework 4

Working with proxy enabled data

Date Page 28Author

• Enabling proxy enabled mapping and data manipulation

Date: 11.02.2010Author: Marjan Nikolovski

Page 29: Entity Framework 4

Working with proxy enabled dataEnabling proxy enabled mapping and data creation

Date Page 29Author

• ProxyCreationEnable property must be set to true• Creation of entity as proxy types

Date: 11.02.2010Author: Marjan Nikolovski

Page 30: Entity Framework 4

Serialization of proxy objects for WCF usage

Date Page 30Author

• What has to be done ?• One side implementation

Date: 11.02.2010Author: Marjan Nikolovski

Page 31: Entity Framework 4

Serialization of proxy objects for WCF usageWhat has to be done ?

Date Page 31Author

• Proxy object are objects of not known type• Defining service behavior where DataContractSerializer must be set

in order proxy data to be resolved to known type

Date: 11.02.2010Author: Marjan Nikolovski

Page 32: Entity Framework 4

Serialization of proxy objects for WCF usageOne side implementation

Date Page 32Author

Date: 11.02.2010Author: Marjan Nikolovski

Page 33: Entity Framework 4

Technology performance

Date Page 33Author

Date: 11.02.2010Author: Marjan Nikolovski

Page 34: Entity Framework 4

Pros and Cons

Date Page 34Author

• Object simplicity and great extensibility• Speeding up DAL design and implementation• Manual context synchronization can be tricky when comes to

business logic• Class construction overhead and memory overflow may occur when

no proper handling is used

Date: 11.02.2010Author: Marjan Nikolovski

Page 35: Entity Framework 4

Page 35Date: 11.02.2010Author: Marjan Nikolovski

Q&A

Page 36: Entity Framework 4

Page 36Date: 11.02.2010Author: Marjan Nikolovski

References

• http://thedatafarm.com/blog/data-access/agile-entity-framework-4-repository-part-1-model-and-poco-classes/

• http://www.code-magazine.com/Article.aspx?quickid=0909081• http://byatool.com/tag/entity-framework/• http://msdn.microsoft.com/en-us/magazine/ee236639.aspx• http://blogs.msdn.com/adonet/pages/feature-ctp-walkthrough-self-tracking-entities-for-the-entity-

framework.aspx• http://blogs.msdn.com/adonet/pages/feature-ctp-walkthrough-poco-templates-for-the-entity-

framework.aspx• http://blogs.msdn.com/adonet/archive/2009/05/21/poco-in-the-entity-framework-part-1-the-

experience.aspx• http://blogs.msdn.com/adonet/archive/2009/06/10/poco-in-the-entity-framework-part-3-change-

tracking-with-poco.aspx• http://blogs.msdn.com/adonet/archive/2009/05/19/sneak-peek-using-code-generation-templates-

with-the-entity--framework-4-0.aspx• http://blogger.forgottenskies.com/?p=518• http://msdn.microsoft.com/en-us/magazine/ee335715.aspx• http://blogs.msdn.com/efdesign/

Page 37: Entity Framework 4

www.seavus.com

Thank You