New Posts New Posts RSS Feed: options.Include of navigation property collections
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

options.Include of navigation property collections

 Post Reply Post Reply
Author
stephenmcd1 View Drop Down
DevForce MVP
DevForce MVP


Joined: 27-Oct-2009
Location: Los Angeles, CA
Posts: 166
Post Options Post Options   Quote stephenmcd1 Quote  Post ReplyReply Direct Link To This Post Topic: options.Include of navigation property collections
    Posted: 15-May-2013 at 2:41pm
I agree - magic strings are asking for trouble.

I've update the code sample to include the MIT license which as far as I know means you can use it in just about any way you'd like.  I'm not an expert on licensing so if that doesn't work for you, let me know.

Glad the code will work for you!
Back to Top
KitKat View Drop Down
Newbie
Newbie
Avatar

Joined: 01-Nov-2012
Location: Florida
Posts: 23
Post Options Post Options   Quote KitKat Quote  Post ReplyReply Direct Link To This Post Posted: 15-May-2013 at 1:12pm
Stephen,
Do you have a license for the code you linked, or is it public domain and free for me to use?

Thank you,
Katerina
Back to Top
KitKat View Drop Down
Newbie
Newbie
Avatar

Joined: 01-Nov-2012
Location: Florida
Posts: 23
Post Options Post Options   Quote KitKat Quote  Post ReplyReply Direct Link To This Post Posted: 15-May-2013 at 1:09pm
Thank you all for your responses.

Stephan, this is exactly what I was looking for.  I like to stay as far away from Magic Strings as I can.

Thank you all again,
Katerina
Back to Top
stephenmcd1 View Drop Down
DevForce MVP
DevForce MVP


Joined: 27-Oct-2009
Location: Los Angeles, CA
Posts: 166
Post Options Post Options   Quote stephenmcd1 Quote  Post ReplyReply Direct Link To This Post Posted: 15-May-2013 at 12:09pm
I've built a helper class that builds strings such as 'Employee.Jobs.LwHrJob' using strongly typed lambdas.  It's not always the easiest to use but it gets the job done.  And for us, a little bit of annoyance/verbosity when first writing the code is worth it if it saves of from running into problems if the model changes.  An example usage of the helper class would be:
    [TestMethod]
    public void TestBuildPath()
    {
        var actualPath = IncludeHelper
            .BuildPathFor<User>()
            .Include(user => user.Employee)
            .IncludeMany(employee => employee.Jobs)
            .Include(job => job.Detail)
            .ToString();

        Assert.AreEqual("Employee.Jobs.Detail", actualPath);

    }

In this case, the IncludeMany method is the one that is really giving us value because it lets us 'flatten' the collection.

You can get the full code from here:  https://gist.github.com/stephenmcd1/5586462
Back to Top
mgood View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 18-Nov-2010
Location: Emeryville, CA
Posts: 583
Post Options Post Options   Quote mgood Quote  Post ReplyReply Direct Link To This Post Posted: 15-May-2013 at 11:05am
The extension method is a good idea. Other than that you can't strongly type it because Employee.Jobs.LwHrJob would mean that LwHrJob is a property of the Jobs collection when in fact it is a property of each item in the Jobs collection. You won't get this past the compiler. 
Back to Top
smi-mark View Drop Down
DevForce MVP
DevForce MVP
Avatar

Joined: 24-Feb-2009
Location: Dallas, Texas
Posts: 343
Post Options Post Options   Quote smi-mark Quote  Post ReplyReply Direct Link To This Post Posted: 15-May-2013 at 9:45am
i wrote an extension method for queries, you could do the same for the options


    public static class EntityQueryExtensions
    {
        public static IEntityQuery<T> IncludePath<T>(this IEntityQuery<T> query, params string[] propertyPaths)
        {
            return query.Include(propertyPaths.Aggregate((a, b) => a + "." + b));
        }
    }



something like this:

.IncludePath(Employee.EntityPropertyNames.Jobs, Job.EntityPropertyNames.LwHrJob)



Back to Top
KitKat View Drop Down
Newbie
Newbie
Avatar

Joined: 01-Nov-2012
Location: Florida
Posts: 23
Post Options Post Options   Quote KitKat Quote  Post ReplyReply Direct Link To This Post Posted: 15-May-2013 at 7:05am
How can you accomplish the strongly typed include when you are navigating past a collection of related entities.

In the example below I have had to define one of my includes as a string since Jobs is a RelatedEntityList, and not just a single entity.  Is there some function I am missing that would allow me to strongly type this?

Example:
            var punchUser =
                (await _punchUser
                       .FindAsync(e => e.RecordId == punchId 
                           && e.PunchPin == punchPin, 
                           null, 
                           options => 
                               options
                                //.Include(user => user.Employee.Jobs)
                                .Include("Employee.Jobs.LwHrJob")
                                .Include(user => user.Employee.OrgLevel1)
                                .Include(user => user.Employee.OrgLevel2)
                                .Include(user => user.Employee.OrgLevel3)
                                .Include(user => user.Employee.OrgLevel4)
                                .Include(user => user.Employee.OrgLevel5)
                                )).FirstOrDefault();

Thank you,
Kat
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down