In a previous post I mentioned how we would be using AutoMapper to create Data-Transfer Objects out of Entity Framework entities; the DTOs would then be transmitted over a WCF service. Well, as often happens, a new requirement surfaced recently, and the solution we came up with turned out to be pretty useful, so now I'm sharing it with you all.

The Problem

The issue we had was this: because this app needs to keep performance in mind (and because I really, really hate clutter), we wanted to include a way for the system calling our service to specify if they wanted any additional entities related to the entities they asked for initially.

Let's set this up with a diagram:

So, a League has many Teams, and a Team has many players.

If the service requested to get a Team for a specific ID, we would return a Data-Transfer Object for the Team class that looks something like this:

[DataContract] public class TeamDTO { [DataMember] public int Id { get; set; } [DataMember] public int LeagueId { get; set; } [DataMember] public string Name { get; set; } [DataMember] public DateTime FoundingDate { get; set; } }

But what if the user also wanted the players on that team? We could make them do another call to the service, but then they are making two calls when they could just be making one. Plus, the calling user won't always want the players for the specific team, so we needed a way for them to "opt-in" to selecting the players in addition to the team object.

So, we had two competing problems:

The user will want the related entities sometimes, but not always. The user does not want to have to make multiple calls to the service to retrieve related data.

That's the issue we tackled. How did we do it? Read on to find out!

Getting Active Navigation Properties

Before we started writing any code at all, we decided that for any query against an entity, we would only load "immediately-related" entities. That means that if the user queried for one or more Leagues, s/he could get the related Teams, but not those Teams' players (at least, not during the same call). We figured that if they really want that data, they'll have to make another call.

For reference, we were using Entity Framework Code First for our data layer, and our Team model class looked like this:

[Table("Team")] public partial class Team { public Team() { Players = new HashSet<Player>(); } public int Id { get; set; } public int LeagueID { get; set; } [Required] [StringLength(200)] public string Name { get; set; } public DateTime FoundingDate { get; set; } public virtual League League { get; set; } public virtual ICollection<Player> Players { get; set; } }

We also modified our TeamDTO data-transfer object to include the optional Players and League, as well as a TeamOptions contract that would let the user choose if they wanted the related entities:

[DataContract] public class TeamDTO { [DataMember] public int Id { get; set; } [DataMember] public string Name { get; set; } [DataMember] public DateTime FoundingDate { get; set; } [DataMember] public List<PlayerDTO> Players { get; set; } [DataMember] public LeagueDTO League { get; set; } } [DataContract] public class TeamOptions { [DataMember] public bool IncludePlayers { get; set; } [DataMember] public bool IncludeLeague { get; set; } }

The problem was, how would we translate the boolean values of the properties of TeamOptions into statements we could actually use in LINQ-to-Entities? Reflection turned out to be the answer.

First, we created an attribute called NavigationPropertyAttribute, which allowed us to decorate the properties in TeamOptions with the name of the navigation property they corresponded to:

[AttributeUsage(AttributeTargets.Property)] public class NavigationPropertyAttribute : Attribute { protected string _navigationPropertyName { get; set; } public string NavigationPropertyName { get { return _navigationPropertyName; } set { _navigationPropertyName = value; } } public NavigationPropertyAttribute() { _navigationPropertyName = string.Empty; } public NavigationPropertyAttribute(string name) { _navigationPropertyName = name; } }

We also needed an OptionsBase class that uses Reflection to return a list of the Navigation Property names attached to option properties that are active:

public abstract class OptionsBase { public List<string> GetSelectedOptions() { List<string> names = new List<string>(); var type = GetType(); var properties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance); foreach(PropertyInfo info in properties) { bool value = (bool)info.GetValue(this); if (info.PropertyType == typeof(bool) && value) { NavigationPropertyAttribute attribute = (NavigationPropertyAttribute)info.GetCustomAttribute(typeof(NavigationPropertyAttribute)); names.Add(attribute.NavigationPropertyName); } } return names; } }

That enabled us to modify TeamOptions like so:

[DataContract] public class TeamOptions : OptionsBase { [DataMember] [NavigationProperty("Players")] public bool IncludePlayers { get; set; } [DataMember] [NavigationProperty("League")] public bool IncludeLeague { get; set; } }

If we had an instance of TeamOptions and wanted to get a list of the property names, we could do this:

var options = new TeamOptions() { IncludePlayers = true, IncludeLeague = true } var navProperties = options.GetSelectedOptions(); //returns new List<string>(){"Players", "League"}

Now that we knew which navigation properties we needed to include, we wanted a way to do this simply, preferably in one line of code. That's where this extension method comes in:

public static IQueryable<T> AddIncludeStatements<T>(this IQueryable<T> query, OptionsBase options) { var names = options.GetSelectedOptions(); var dbQuery = (DbQuery<T>)query; foreach (var name in names) { dbQuery = dbQuery.Include(name); } return dbQuery; }

Let's break this method down:

An IQueryable<> is a representation of a query. In this case, the extension method is taking a query that it is going to modify as a this parameter.

is a representation of a query. In this case, the extension method is taking a query that it is going to modify as a parameter. A DbQuery<> is a non-generic instance of a query. Because we cannot use the Include() method on an IQueryable<>, we needed to cast to DbQuery<>

What this method does is take a LINQ-to-Entities query, insert the needed include statements into said query, then return the modified query that can be executed elsewhere.

We use it like this (this is our TeamService class, which provides data access for the Team objects):

public class TeamService { private LeaguesDataContext _context; public TeamService() { _context = new LeaguesDataContext(); } public TeamDTO GetByID(int id, TeamOptions options) { return Mapper.Map<TeamDTO>(_context.Teams.Where(x => x.Id == id).AddIncludeStatements(options).First()); } }

Woohoo! We're done, now we just pull up WCFTestClient to test this and we'll be ready to rock.....

Crap. That's not good. What went wrong?

Tracking Down the Bug

I may be an experienced bug hunter, but this thing stymied me. The error message didn't give any useful details, it just said that a "connection was forcibly closed by the remote host." That's as helpful as a slap to the face.

Eventually, though, I discovered the problem. I'm gonna lay out most of the code (call to create AutoMapper maps excluded); see if you see the issue.

[DataContract] public class TeamDTO { [DataMember] public int Id { get; set; } [DataMember] public string Name { get; set; } [DataMember] public DateTime FoundingDate { get; set; } [DataMember] public List<PlayerDTO> Players { get; set; } [DataMember] public LeagueDTO League { get; set; } } [DataContract] public class PlayerDTO : Player { [DataMember] public int Id { get; set; } [DataMember] public int TeamId { get; set; } [DataMember] public string FirstName { get; set; } [DataMember] public string LastName { get; set; } [DataMember] public DateTime DateOfBirth { get; set; } [DataMember] public TeamDTO Team { get; set; } } public static class AutoMapperConfiguration { public static void Configure() { Mapper.CreateMap<EntityFrameworkModel.League, Contracts.DataContracts.LeagueDTO>(); Mapper.CreateMap<EntityFrameworkModel.Team, Contracts.DataContracts.TeamDTO>(); Mapper.CreateMap<EntityFrameworkModel.Player, Contracts.DataContracts.PlayerDTO>(); } } public class TeamService { private LeaguesDataContext _context; public TeamService() { _context = new LeaguesDataContext(); } public TeamDTO GetByID(int id, TeamOptions options) { return Mapper.Map<TeamDTO>(_context.Teams.Where(x => x.Id == id).AddIncludeStatements(options).First()); } }

See the issue? I sure didn't; not the first ten times, anyway.

The problem was that we defined a circular reference. See how PlayerDTO and TeamDTO each have properties of the other's type? When AutoMapper encountered those properties, it attempted to map them by reading the corresponding EF navigation properties, and EF promptly loaded that data from the database. This caused an infinite loop of loading (load them Team, get the related Players, get their related Teams, get those Teams' related Players, etc.) and eventually the system crashed.

Well, that sucks. I had been hoping to keep the structure nice and clean, but this infinite loop wasn't going to let me. I had to find some way to eliminate the circular references.

We did this by creating two levels of DTOs. Here's the two classes for Team:

namespace WCFNavigationPropertyMapping.Contracts.DataContracts { [DataContract] public class Team { [DataMember] public int Id { get; set; } [DataMember] public string Name { get; set; } [DataMember] public DateTime FoundingDate { get; set; } } [DataContract] public class TeamDTO : Team { [DataMember] public League League { get; set; } [DataMember] public List<Player> Players { get; set; } } }

Note that in TeamDTO, the reference for the two NavigationProperties are for the basic contract classes, not the complete DTO objects. This design will prevent the circular-reference infinite-loop errors. Now if we run this in WCF Test Client, it behaves exactly like we want it to.

Summary

This design allows us to:

Let the user decide which related entities s/he wants on a given call. Let the developers use concise, clear code to describe what the LINQ-to-Entities queries should be doing.

I'm pretty satisfied with this solution; we'll see how it holds up under load. I have, however, noticed a couple of drawbacks:

I don't like needing two data contracts classes. I think there ought to be a cleaner way of designing this, but it's escaping me at the moment.

Reflection and Entity Framework are both reputed (even by me) to be slow compared to other equivalent solutions, so I'm really interested to see if my developer-friendly solution can pull its weight in a production environment.

There's a sample project that demos this solution over on GitHub. I'd be happy to hear any suggestions you dear readers have about how to improve this design.

Happy Coding!