Print Page | Close Window

Navigation property definitions in DevForce CodeFirst

Printed From: IdeaBlade
Category: DevForce
Forum Name: DevForce 2010
Forum Discription: For .NET 4.0
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=3156
Printed Date: 13-May-2026 at 4:26pm


Topic: Navigation property definitions in DevForce CodeFirst
Posted By: WardBell
Subject: Navigation property definitions in DevForce CodeFirst
Date Posted: 13-Dec-2011 at 11:40am
This note clarifies some details of implementing http://drc.ideablade.com/xwiki/bin/view/Documentation/code-first-entity-classes#HPersistednavigationproperties - 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 { /* ... */ }

 




Print Page | Close Window