New Posts New Posts RSS Feed: Navigation property definitions in DevForce CodeFirst
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

Navigation property definitions in DevForce CodeFirst

 Post Reply Post Reply
Author
WardBell View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 31-Mar-2009
Location: Emeryville, CA,
Posts: 338
Post Options Post Options   Quote WardBell Quote  Post ReplyReply Direct Link To This Post Topic: Navigation property definitions in DevForce CodeFirst
    Posted: 13-Dec-2011 at 11:40am
This note clarifies some details of implementing navigation properties in DevForce CodeFirst as explained in the DRC.
 
DevForce CodeFirst AOP rewrites your navigation properties, replacing your implementation with DevForce navigation logic. 
 
Takeaway: backing fields can by used in simple pesisted data properties but not in navigation properties (reference or collection)
 
Here is an example of a Foo class with a variety of collection navigation properties to Bar objects. These properties are implemented in various ways. Some are valid. Some are invalid; the invalid ones may appear to work but are not doing precisely what you expected.

 

[ProvideEntityAspect]

public class Foo

{

    /*** Works and is the way we recommend ***/

    public RelatedEntityList<Bar> Bars1 { get { return null; } } // no setter ... the way we recommend

 

    /*** Works but prefer Bars1 ***/

    public RelatedEntityList<Bar> Bars2 { getset; } // the way you are tempted to write it

 

    /*** Works but prefer Bars1 ***/

    public ICollection<Bar> Bars3 { getinternal set; } // might be suitable for SL 

  

         /*** DO NOT DO ANY OF THE FOLLOWING ***/

  

    public Foo()

    {

        /*

         * Do not initialize navigation properties as this is neither effective nor necessary.

         *

         * Many who come from EF or NHibernate backgrounds think setting navigation properties

         * makes Foo testable. This is a misunderstanding about the difference between proxy and AOP.

         * DevForce AOP collection navigation properties are implemented and initialized

         * wherever you use them  ... including in testing.

         */

        Bars3 = new RelatedEntityList<Bar>();

 

        /* Backing fields are ignored by DevForce. */

        _bars5 = new List<Bar>(); // typical  EF/NH thinking

  

        /* Backing fields are ignored by DevForce. This won’t work */

        _bars6Inner = new List<Bar>();

        _bars6 = new ReadOnlyCollection<Bar>(_bars6Inner); // typical EF/NH thinking

    }

 

    /*** Does NOT work; DevForce won’t touch private members ***/

    public RelatedEntityList<Bar> Bars4 { getprivate set; }  // private setter

 

    /*** “Works” but DevForce ignores the backing field and it won’t change at runtime ***/

    public ICollection<Bar> Bars5 // because the dev likes a backing field?

    {

        get { return _bars5; }

        set { _bars5 = value; }

    }

    private ICollection<Bar> _bars5;

 

    /*** “Works” but DevForce ignores the backing field and it won’t change at runtime

         and won’t be a ReadOnlyCollection as you hoped ***/

    public ICollection<Bar> Bars6 // observe how this is instantiated in ctor

    {

        get { return _bars6; }

        set { _bars6 = value; }

    }

    private ICollection<Bar> _bars6;

    private readonly IList<Bar> _bars6Inner;

}

  

[ProvideEntityAspect]

public class Bar { /* ... */ }

 



Edited by WardBell - 13-Dec-2011 at 11:44am
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down