1
0
Fork 0
mirror of https://github.com/Radarr/Radarr synced 2024-12-25 17:27:59 +00:00

created HasOne, HasMany relationship extension.

This commit is contained in:
kay.one 2013-03-26 00:50:18 -07:00
parent a2b91b469e
commit a9ad739745
4 changed files with 87 additions and 43 deletions

View file

@ -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<T> RegisterModel<T>(this FluentMappings.MappingsFluentEntity<T> 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);
}
}
}

View file

@ -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<TParent> HasOne<TParent, TChild>(this ColumnMapBuilder<TParent> columnMapBuilder, Expression<Func<TParent, object>> portalExpression, Func<TParent, int> childIdSelector)
where TParent : ModelBase
where TChild : ModelBase
{
return columnMapBuilder.Relationships.AutoMapComplexTypeProperties<ILazyLoaded>()
.For(portalExpression)
.LazyLoad((db, parent) => db.Query<TChild>().Single(c => c.Id == childIdSelector(parent)));
}
public static RelationshipBuilder<TParent> HasMany<TParent, TChild>(this ColumnMapBuilder<TParent> columnMapBuilder, Expression<Func<TParent, object>> portalExpression, Func<TParent, int> childIdSelector)
where TParent : ModelBase
where TChild : ModelBase
{
return columnMapBuilder.Relationships.AutoMapComplexTypeProperties<ILazyLoaded>()
.For(portalExpression)
.LazyLoad((db, parent) => db.Query<TChild>().Where(c => c.Id == childIdSelector(parent)).ToList());
}
}
}

View file

@ -1,4 +1,5 @@
using System; using System;
using System.Linq.Expressions;
using System.Reflection; using System.Reflection;
using Marr.Data; using Marr.Data;
using Marr.Data.Mapping; using Marr.Data.Mapping;
@ -38,9 +39,7 @@ public static void Map()
Mapper.Entity<SceneMapping>().RegisterModel("SceneMappings"); Mapper.Entity<SceneMapping>().RegisterModel("SceneMappings");
Mapper.Entity<History.History>().RegisterModel("History") Mapper.Entity<History.History>().RegisterModel("History")
.Relationships.AutoMapComplexTypeProperties<ILazyLoaded>() .HasOne<History.History, Episode>(h => h.Episode, h => h.EpisodeId);
.For(c => c.Episode)
.LazyLoad((db, history) => db.Query<Episode>().Single(ep => ep.Id == history.EpisodeId));
Mapper.Entity<Series>().RegisterModel("Series") Mapper.Entity<Series>().RegisterModel("Series")
.Relationships.AutoMapComplexTypeProperties<ILazyLoaded>() .Relationships.AutoMapComplexTypeProperties<ILazyLoaded>()
@ -60,6 +59,7 @@ public static void Map()
} }
private static void RegisterMappers() private static void RegisterMappers()
{ {
MapRepository.Instance.RegisterTypeConverter(typeof(Int32), new Int32Converter()); 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(Enum), new EnumIntConverter());
MapRepository.Instance.RegisterTypeConverter(typeof(QualityModel), new EmbeddedDocumentConverter()); MapRepository.Instance.RegisterTypeConverter(typeof(QualityModel), new EmbeddedDocumentConverter());
} }
private static ColumnMapBuilder<T> RegisterModel<T>(this FluentMappings.MappingsFluentEntity<T> 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);
}
} }
} }

View file

@ -203,6 +203,7 @@
<Compile Include="Datastore\EnumIntConverter.cs" /> <Compile Include="Datastore\EnumIntConverter.cs" />
<Compile Include="Datastore\IEmbeddedDocument.cs" /> <Compile Include="Datastore\IEmbeddedDocument.cs" />
<Compile Include="Datastore\LazyList.cs" /> <Compile Include="Datastore\LazyList.cs" />
<Compile Include="Datastore\MappingExtensions.cs" />
<Compile Include="Datastore\Migration\Framework\MigrationController.cs" /> <Compile Include="Datastore\Migration\Framework\MigrationController.cs" />
<Compile Include="Datastore\Migration\Framework\MigrationOptions.cs" /> <Compile Include="Datastore\Migration\Framework\MigrationOptions.cs" />
<Compile Include="Datastore\Migration\Framework\NlogAnnouncer.cs" /> <Compile Include="Datastore\Migration\Framework\NlogAnnouncer.cs" />
@ -211,6 +212,7 @@
<Compile Include="Datastore\MigrationType.cs" /> <Compile Include="Datastore\MigrationType.cs" />
<Compile Include="Datastore\ModelBase.cs" /> <Compile Include="Datastore\ModelBase.cs" />
<Compile Include="Datastore\BasicRepository.cs" /> <Compile Include="Datastore\BasicRepository.cs" />
<Compile Include="Datastore\RelationshipExtensions.cs" />
<Compile Include="Datastore\TableMapping.cs" /> <Compile Include="Datastore\TableMapping.cs" />
<Compile Include="DecisionEngine\DownloadDecision.cs" /> <Compile Include="DecisionEngine\DownloadDecision.cs" />
<Compile Include="DecisionEngine\IFetchableSpecification.cs" /> <Compile Include="DecisionEngine\IFetchableSpecification.cs" />