diff --git a/NzbDrone.Core/Datastore/MappingExtensions.cs b/NzbDrone.Core/Datastore/MappingExtensions.cs new file mode 100644 index 000000000..bb5cfb3e8 --- /dev/null +++ b/NzbDrone.Core/Datastore/MappingExtensions.cs @@ -0,0 +1,49 @@ +using System; +using System.Linq; +using System.Reflection; +using Marr.Data.Mapping; + +namespace NzbDrone.Core.Datastore +{ + public static class MappingExtensions + { + public static ColumnMapBuilder RegisterModel(this FluentMappings.MappingsFluentEntity mapBuilder, string tableName) where T : ModelBase + { + return mapBuilder.Table.MapTable(tableName) + .Columns + .AutoMapPropertiesWhere(IsMappableProperty) + .For(c => c.Id) + .SetPrimaryKey() + .SetReturnValue() + .SetAutoIncrement(); + } + + public static bool IsMappableProperty(MemberInfo memberInfo) + { + var propertyInfo = memberInfo as PropertyInfo; + + if (propertyInfo == null) return false; + + if (propertyInfo.PropertyType.GetInterfaces().Any(i => i == typeof(IEmbeddedDocument))) + { + return true; + } + + return propertyInfo.CanRead && propertyInfo.CanWrite && IsSimpleType(propertyInfo.PropertyType); + } + + public static bool IsSimpleType(Type type) + { + if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>)) + { + type = type.GetGenericArguments()[0]; + } + + return type.IsPrimitive + || type.IsEnum + || type == typeof(string) + || type == typeof(DateTime) + || type == typeof(Decimal); + } + } +} \ No newline at end of file diff --git a/NzbDrone.Core/Datastore/RelationshipExtensions.cs b/NzbDrone.Core/Datastore/RelationshipExtensions.cs new file mode 100644 index 000000000..e5338c836 --- /dev/null +++ b/NzbDrone.Core/Datastore/RelationshipExtensions.cs @@ -0,0 +1,33 @@ +using System; +using System.Linq; +using System.Linq.Expressions; +using Marr.Data; +using Marr.Data.Mapping; + +namespace NzbDrone.Core.Datastore +{ + public static class RelationshipExtensions + { + public static RelationshipBuilder HasOne(this ColumnMapBuilder columnMapBuilder, Expression> portalExpression, Func childIdSelector) + where TParent : ModelBase + where TChild : ModelBase + { + return columnMapBuilder.Relationships.AutoMapComplexTypeProperties() + .For(portalExpression) + .LazyLoad((db, parent) => db.Query().Single(c => c.Id == childIdSelector(parent))); + + + } + + public static RelationshipBuilder HasMany(this ColumnMapBuilder columnMapBuilder, Expression> portalExpression, Func childIdSelector) + where TParent : ModelBase + where TChild : ModelBase + { + return columnMapBuilder.Relationships.AutoMapComplexTypeProperties() + .For(portalExpression) + .LazyLoad((db, parent) => db.Query().Where(c => c.Id == childIdSelector(parent)).ToList()); + + + } + } +} \ No newline at end of file diff --git a/NzbDrone.Core/Datastore/TableMapping.cs b/NzbDrone.Core/Datastore/TableMapping.cs index 489a73ba2..e06bc8bd4 100644 --- a/NzbDrone.Core/Datastore/TableMapping.cs +++ b/NzbDrone.Core/Datastore/TableMapping.cs @@ -1,4 +1,5 @@ using System; +using System.Linq.Expressions; using System.Reflection; using Marr.Data; using Marr.Data.Mapping; @@ -38,9 +39,7 @@ public static void Map() Mapper.Entity().RegisterModel("SceneMappings"); Mapper.Entity().RegisterModel("History") - .Relationships.AutoMapComplexTypeProperties() - .For(c => c.Episode) - .LazyLoad((db, history) => db.Query().Single(ep => ep.Id == history.EpisodeId)); + .HasOne(h => h.Episode, h => h.EpisodeId); Mapper.Entity().RegisterModel("Series") .Relationships.AutoMapComplexTypeProperties() @@ -60,6 +59,7 @@ public static void Map() } + private static void RegisterMappers() { MapRepository.Instance.RegisterTypeConverter(typeof(Int32), new Int32Converter()); @@ -67,45 +67,5 @@ private static void RegisterMappers() MapRepository.Instance.RegisterTypeConverter(typeof(Enum), new EnumIntConverter()); MapRepository.Instance.RegisterTypeConverter(typeof(QualityModel), new EmbeddedDocumentConverter()); } - - - private static ColumnMapBuilder RegisterModel(this FluentMappings.MappingsFluentEntity mapBuilder, string tableName) where T : ModelBase - { - return mapBuilder.Table.MapTable(tableName) - .Columns - .AutoMapPropertiesWhere(IsMappableProperty) - .For(c => c.Id) - .SetPrimaryKey() - .SetReturnValue() - .SetAutoIncrement(); - } - - private static bool IsMappableProperty(MemberInfo memberInfo) - { - var propertyInfo = memberInfo as PropertyInfo; - - if (propertyInfo == null) return false; - - if (propertyInfo.PropertyType.GetInterfaces().Any(i => i == typeof(IEmbeddedDocument))) - { - return true; - } - - return propertyInfo.CanRead && propertyInfo.CanWrite && IsSimpleType(propertyInfo.PropertyType); - } - - private static bool IsSimpleType(Type type) - { - if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>)) - { - type = type.GetGenericArguments()[0]; - } - - return type.IsPrimitive - || type.IsEnum - || type == typeof(string) - || type == typeof(DateTime) - || type == typeof(Decimal); - } } } \ No newline at end of file diff --git a/NzbDrone.Core/NzbDrone.Core.csproj b/NzbDrone.Core/NzbDrone.Core.csproj index 21292072c..89e6a0ddc 100644 --- a/NzbDrone.Core/NzbDrone.Core.csproj +++ b/NzbDrone.Core/NzbDrone.Core.csproj @@ -203,6 +203,7 @@ + @@ -211,6 +212,7 @@ +