created HasOne, HasMany relationship extension.

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

View file

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