From e9b9e63f7ae9680f578de97c900e5f978754ae2c Mon Sep 17 00:00:00 2001 From: "kay.one" Date: Tue, 26 Mar 2013 01:00:46 -0700 Subject: [PATCH] RelationshipExtensions has more type enforcement. --- .../Datastore/RelationshipExtensions.cs | 20 +++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/NzbDrone.Core/Datastore/RelationshipExtensions.cs b/NzbDrone.Core/Datastore/RelationshipExtensions.cs index e5338c836..3de895cc6 100644 --- a/NzbDrone.Core/Datastore/RelationshipExtensions.cs +++ b/NzbDrone.Core/Datastore/RelationshipExtensions.cs @@ -8,26 +8,38 @@ namespace NzbDrone.Core.Datastore { public static class RelationshipExtensions { - public static RelationshipBuilder HasOne(this ColumnMapBuilder columnMapBuilder, Expression> portalExpression, Func childIdSelector) + public static RelationshipBuilder HasOne(this ColumnMapBuilder columnMapBuilder, Expression>> portalExpression, Func childIdSelector) where TParent : ModelBase where TChild : ModelBase { return columnMapBuilder.Relationships.AutoMapComplexTypeProperties() - .For(portalExpression) + .For(portalExpression.GetMemberName()) .LazyLoad((db, parent) => db.Query().Single(c => c.Id == childIdSelector(parent))); } - public static RelationshipBuilder HasMany(this ColumnMapBuilder columnMapBuilder, Expression> portalExpression, Func childIdSelector) + public static RelationshipBuilder HasMany(this ColumnMapBuilder columnMapBuilder, Expression>> portalExpression, Func childIdSelector) where TParent : ModelBase where TChild : ModelBase { return columnMapBuilder.Relationships.AutoMapComplexTypeProperties() - .For(portalExpression) + .For(portalExpression.GetMemberName()) .LazyLoad((db, parent) => db.Query().Where(c => c.Id == childIdSelector(parent)).ToList()); } + + private static string GetMemberName(this Expression> member) + { + var expression = member.Body as MemberExpression; + + if (expression == null) + { + expression = (MemberExpression)((UnaryExpression)member.Body).Operand; + } + + return expression.Member.Name; + } } } \ No newline at end of file