geeks track 2 - ado.net and entity framework best practices - andri yadi

Upload: muhammad-sholihin

Post on 29-May-2018

229 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/9/2019 Geeks Track 2 - ADO.net and Entity Framework Best Practices - Andri Yadi

    1/39

    ENTITY FRAMEWORK V2BEST PRACTICES

    , ,Microsoft Innovation Day 2010 May 11 2010

    ndri Yadi .| a@dycode com, ,CEO DyCode | MVP VSTO

  • 8/9/2019 Geeks Track 2 - ADO.net and Entity Framework Best Practices - Andri Yadi

    2/39

    :// . / /http itunes com apps movreak

  • 8/9/2019 Geeks Track 2 - ADO.net and Entity Framework Best Practices - Andri Yadi

    3/39

    Entity FrameworkEntity Framework

    SQL Server 2008

    WCF Data Services WCF Data Services/HTTP REST

    JSONSON

    -Real world Scenario

  • 8/9/2019 Geeks Track 2 - ADO.net and Entity Framework Best Practices - Andri Yadi

    4/39

    . .Dr EF Codd

  • 8/9/2019 Geeks Track 2 - ADO.net and Entity Framework Best Practices - Andri Yadi

    5/39

  • 8/9/2019 Geeks Track 2 - ADO.net and Entity Framework Best Practices - Andri Yadi

    6/39

    DELETEDELETE

    SELECTSELECT

    COUNTCOUNT

    LEFTLEFTUNIONUNION /MIN MAX/MIN MAX

    JOINJOIN

    HAVINGHAVINGORDER BYORDER BY

    RIGHTRIGHT

    INSERTINSERT

    WHEREWHERE

    UPDATEUPDATE

    Then we code in SQL

    Then we code in SQL

  • 8/9/2019 Geeks Track 2 - ADO.net and Entity Framework Best Practices - Andri Yadi

    7/39

    ;SqlCommand oCmd;SqlDataReader oDR

    = .string connString ConfigurationManager

    [" "].ConnectionStrings OracleConnString;ConnectionString

    =SqlConnection conn new( );SqlConnection connString

    ;string selectQuery

    = ;int _returnValue 0

    = ( ) selectQuery SELECT COUNT OrderID from Orders+ = ;where EmployeeID @EmpID

    . ();conn Open= ( , );oCmd new SqlCommand selectQuery conn. . ( , . );oCmd Parameters Add @EmpID SqlDbType Int. [ ]. = ;oCmd Parameters @EmpID Value employeeID= . ();oDR oCmd ExecuteReader

    ( . ())if oDR Read= . ( );_returnValue oDR GetInt32 0

    . ();oDR Close. ();conn Close

    .ADO NET code

  • 8/9/2019 Geeks Track 2 - ADO.net and Entity Framework Best Practices - Andri Yadi

    8/39

    The Dilemma

  • 8/9/2019 Geeks Track 2 - ADO.net and Entity Framework Best Practices - Andri Yadi

    9/39

    .ADO NETADO( )1996

    DAO( )1992

    RDO

  • 8/9/2019 Geeks Track 2 - ADO.net and Entity Framework Best Practices - Andri Yadi

    10/39

    ! !Enough We need ORM

  • 8/9/2019 Geeks Track 2 - ADO.net and Entity Framework Best Practices - Andri Yadi

    11/39

    O

    bjectR elationalMapping

    Technique for working withrelational tables as if

    they were objects in memory Hide away the complexity of

    the underlying tables andgive a uniform way of

    working with data

  • 8/9/2019 Geeks Track 2 - ADO.net and Entity Framework Best Practices - Andri Yadi

    12/39

    Why ORM? Development productivity Database independence Database portability

  • 8/9/2019 Geeks Track 2 - ADO.net and Entity Framework Best Practices - Andri Yadi

    13/39

    Many attempts

    Typed Datasets Objectspaces v1 Objectspaces v2 Microsoft BusinessFramework WinFS

    Linq to SQL NHibernate Will RIP Not Microsoft s

  • 8/9/2019 Geeks Track 2 - ADO.net and Entity Framework Best Practices - Andri Yadi

    14/39

    .ADO NET EntityFramework

    Microsoft s strategictechnology

    Used in other Microsoft(technologies reporting

    )services . .V2 released with NET 4 0

  • 8/9/2019 Geeks Track 2 - ADO.net and Entity Framework Best Practices - Andri Yadi

    15/39

    Entity DataModel

    ,Invented in the 1970s by.Dr Peter Chen

    ERM

    Conceptual Layer Mapping Layer

    Storage Layer

    : .Now EDM in ADO NETEntity Framework

  • 8/9/2019 Geeks Track 2 - ADO.net and Entity Framework Best Practices - Andri Yadi

    16/39

    Entity Framework vs LinqToSQL

  • 8/9/2019 Geeks Track 2 - ADO.net and Entity Framework Best Practices - Andri Yadi

    17/39

    ET S GO DEEPER ON EF2

  • 8/9/2019 Geeks Track 2 - ADO.net and Entity Framework Best Practices - Andri Yadi

    18/39

    EDM

    Entity FrameworkEntity Framework

    Conceptual ModelConceptual Model

    Entity FrameworkEntity Framework

    < >IEnumerable T

    EntityDataReader

    LINQ to EntitiesEntity SQL Query

    Command TreeEntity SQL Query

    Entity FrameworkEntity Framework

    EntityDataReaderCommand TreeStorage ModelStorage Model

    Mapping

  • 8/9/2019 Geeks Track 2 - ADO.net and Entity Framework Best Practices - Andri Yadi

    19/39

    DEMO-Model first

  • 8/9/2019 Geeks Track 2 - ADO.net and Entity Framework Best Practices - Andri Yadi

    20/39

    Eager Loading

    Structure the initial queryin such a way that all ofthe required objects are

    returned in the initialquery

    from c in.nw Customers. (" ")nclude Orders

    ;select c

  • 8/9/2019 Geeks Track 2 - ADO.net and Entity Framework Best Practices - Andri Yadi

    21/39

    Explicit Loading

    Explicitly request to load

    the related objects . . ();customer Orders Load

  • 8/9/2019 Geeks Track 2 - ADO.net and Entity Framework Best Practices - Andri Yadi

    22/39

    Lazy Loading

    Related objects are loadedautomatically for you whenyou access them

  • 8/9/2019 Geeks Track 2 - ADO.net and Entity Framework Best Practices - Andri Yadi

    23/39

    POCO Support

    Plain Old CLR Object User your own POCO objects

    with no EF attributes Code your POCO classes Code Entity Framework

    Context

    Or use T4 POCO entitygenerator by VS2010

  • 8/9/2019 Geeks Track 2 - ADO.net and Entity Framework Best Practices - Andri Yadi

    24/39

    Questions onPOCO

    Do I still need an EDM? Yes How is metadata mapped to

    POCO entities?onvention basedmapping , ,Entity Type Property and

    Complex Types names mustmatch those defined by in

    EDM

    /Is deferred lazy loadingsupported? Yes

  • 8/9/2019 Geeks Track 2 - ADO.net and Entity Framework Best Practices - Andri Yadi

    25/39

    Lazy Loading onPOCO

    -Declare lazy loaded propertyas virtual

    Make sure to enable.ContextOptions LazyLoadingEnabled

    What s under the hood?

  • 8/9/2019 Geeks Track 2 - ADO.net and Entity Framework Best Practices - Andri Yadi

    26/39

    POCO Changetracking

    -Snapshot based -Proxy based

  • 8/9/2019 Geeks Track 2 - ADO.net and Entity Framework Best Practices - Andri Yadi

    27/39

    DEMOPOCO

  • 8/9/2019 Geeks Track 2 - ADO.net and Entity Framework Best Practices - Andri Yadi

    28/39

    DEMO Generate EDM from database

  • 8/9/2019 Geeks Track 2 - ADO.net and Entity Framework Best Practices - Andri Yadi

    29/39

    &Performancesecurity?

    Connections to database

    .vs amount of data You can work with stored

    procedures You can work with views You can define how the

    .ADO NET Entity Frameworkloads your data to

    Eager Lazy ( )Explicit

  • 8/9/2019 Geeks Track 2 - ADO.net and Entity Framework Best Practices - Andri Yadi

    30/39

    F V2 IN DISTRIBUTED SYSTEM

  • 8/9/2019 Geeks Track 2 - ADO.net and Entity Framework Best Practices - Andri Yadi

    31/39

    Entity FrameworkEntity Framework

    SQL Server 2008

    A WCF ServiceA WCF Service

    Distributed system WCF service

    Pr

    oxy

    Pr

    oxy

    Da

    ta

    Bi

    ndi

    ng

    Da

    ta

    Bi

    ndi

    ng

    ObjectContextavailable

    ObjectContext notavailable

  • 8/9/2019 Geeks Track 2 - ADO.net and Entity Framework Best Practices - Andri Yadi

    32/39

    Entity FrameworkEntity Framework

    SQL Server 2008

    WCF Data Service WCF Data Service/HTTP REST

    Distributed system WCF Dataservice

    /HTTP REST

    Da

    ta

    Se

    rv

    ice

    Co

    nt

    ext

    Da

    ta

    Se

    rv

    ice

    Co

    nt

    ex

    t

    Da

    ta

    Bi

    ndi

    ng

    Da

    ta

    Bi

    ndi

    ng

    ObjectContextavailable

    ObjectContext notavailable

    /SON/SONXMLML

  • 8/9/2019 Geeks Track 2 - ADO.net and Entity Framework Best Practices - Andri Yadi

    33/39

    , O WHAT S NEW IN EF2?

  • 8/9/2019 Geeks Track 2 - ADO.net and Entity Framework Best Practices - Andri Yadi

    34/39

    Entityenhancements

    -Foreign keys supported inthe conceptual model

    Testability enhancements < >IObjectSet T and

    < >ObjectSet T Easier to mock data

    context and dataentities for tests

    Lazy loading for relatedobjects Options now for explicit

    or implicit loading

  • 8/9/2019 Geeks Track 2 - ADO.net and Entity Framework Best Practices - Andri Yadi

    35/39

    Support forpersistence

    ignorant objects

    Persistence ignorant objects POCO objects with no EF

    , .attributes etc Mapped to conceptual modelvia convention

    Change tracking possiblewith generated proxies or

    snapshot

    -Managing types in n tierapplications /Easier to add attach objects

    to a context More control over object

    state

    Issues you should consider You still need the Entity( )Data Model edmx To use objects with WCF

    useProxyDataContractResolver

  • 8/9/2019 Geeks Track 2 - ADO.net and Entity Framework Best Practices - Andri Yadi

    36/39

    Designerenhancements

    Support for complex types &Singularization

    pluralization

    Model first development Creates DDL for database

    based on your model

    Designer extensibility Influence the EDMX

    generation

    Add visuals Influence DDL creation

  • 8/9/2019 Geeks Track 2 - ADO.net and Entity Framework Best Practices - Andri Yadi

    37/39

    Code generationcustomization

    Based on T4 templates Included as of VS 2008 ,Runtime support but not

    much design support

    T4 and Entity Framework T4 used to generate code

    from model Create new T4 templates

    to use instead

    Add validation logic Create POCO objects

  • 8/9/2019 Geeks Track 2 - ADO.net and Entity Framework Best Practices - Andri Yadi

    38/39

    References

    . ADO NET team blog keep up withnew features in Entity

    framework

    :// . . /http blogs msdn com adonet

    . #ADO NET C POCO Entity Generator . #DO NET C Web Site POCO Entity WCF Data Services team blog

    :// . . /http blogs msdn com astoriateam

    http://visualstudiogallery.msdn.microsoft.com/en-us/23df0450-5677-4926-96cc-173d02752313http://visualstudiogallery.msdn.microsoft.com/en-us/23df0450-5677-4926-96cc-173d02752313http://visualstudiogallery.msdn.microsoft.com/en-us/fe568da5-aa1a-4178-a2a5-48813c707a7fhttp://visualstudiogallery.msdn.microsoft.com/en-us/fe568da5-aa1a-4178-a2a5-48813c707a7fhttp://visualstudiogallery.msdn.microsoft.com/en-us/fe568da5-aa1a-4178-a2a5-48813c707a7fhttp://visualstudiogallery.msdn.microsoft.com/en-us/23df0450-5677-4926-96cc-173d02752313
  • 8/9/2019 Geeks Track 2 - ADO.net and Entity Framework Best Practices - Andri Yadi

    39/39

    DyCode. . .www dycode com | office@dycode comDynamic IT Solutions for Optimal Business

    Value