1
0
mirror of https://github.com/Radarr/Radarr.git synced 2024-11-09 04:22:30 +01:00

embedded type convector is now registered for all embedded types automatically.

This commit is contained in:
kay.one 2013-03-31 17:20:01 -07:00
parent bdcdc7c0f6
commit c1fba9093d
5 changed files with 42 additions and 24 deletions

View File

@ -38,7 +38,7 @@ public class MapRepository
internal Dictionary<Type, string> Tables { get; set; }
internal Dictionary<Type, ColumnMapCollection> Columns { get; set; }
internal Dictionary<Type, RelationshipCollection> Relationships { get; set; }
internal Dictionary<Type, IConverter> TypeConverters { get; set; }
public Dictionary<Type, IConverter> TypeConverters { get; private set; }
// Explicit static constructor to tell C# compiler
// not to mark type as beforefieldinit
@ -202,16 +202,9 @@ public RelationshipCollection GetRelationships(Type type)
/// <param name="type">The CLR data type that will be converted.</param>
/// <param name="converter">An IConverter object that will handle the data conversion.</param>
public void RegisterTypeConverter(Type type, IConverter converter)
{
if (TypeConverters.ContainsKey(type))
{
TypeConverters[type] = converter;
}
else
{
TypeConverters.Add(type, converter);
}
}
/// <summary>
/// Checks for a type converter (if one exists).

View File

@ -1,8 +1,10 @@
using System;
using System.Collections.Generic;
using FluentAssertions;
using Marr.Data;
using NUnit.Framework;
using NzbDrone.Core.Datastore;
using NzbDrone.Core.Datastore.Converters;
using NzbDrone.Core.Tv;
namespace NzbDrone.Core.Test.Datastore
@ -35,6 +37,15 @@ public class TypeWithNoMappableProperties
public int WriteOnly { private get; set; }
}
[SetUp]
public void Setup()
{
MapRepository.Instance.RegisterTypeConverter(typeof(List<EmbeddedType>), new EmbeddedDocumentConverter());
MapRepository.Instance.RegisterTypeConverter(typeof(EmbeddedType), new EmbeddedDocumentConverter());
MapRepository.Instance.RegisterTypeConverter(typeof(Int32), new Int32Converter());
}
[Test]
public void test_mappable_types()

View File

@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Marr.Data;
using Marr.Data.Mapping;
namespace NzbDrone.Core.Datastore
@ -25,19 +26,18 @@ public static bool IsMappableProperty(MemberInfo memberInfo)
if (propertyInfo == null) return false;
if (propertyInfo.PropertyType.GetInterfaces().Any(i => i == typeof(IEmbeddedDocument)))
if (!propertyInfo.IsReadable() || !propertyInfo.IsWritable())
{
return false;
}
if (IsSimpleType(propertyInfo.PropertyType) || MapRepository.Instance.TypeConverters.ContainsKey(propertyInfo.PropertyType))
{
return true;
}
if (typeof(IEnumerable<IEmbeddedDocument>).IsAssignableFrom(propertyInfo.PropertyType))
{
return true;
}
var result = propertyInfo.IsReadable() && propertyInfo.IsWritable() && IsSimpleType(propertyInfo.PropertyType);
return result;
return false;
}
public static bool IsSimpleType(Type type)

View File

@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Reflection;
using System.Linq;
using Marr.Data;
using Marr.Data.Mapping;
using NzbDrone.Core.Configuration;
@ -16,7 +15,6 @@
using NzbDrone.Core.RootFolders;
using NzbDrone.Core.Tv;
using BooleanIntConverter = NzbDrone.Core.Datastore.Converters.BooleanIntConverter;
using System.Linq;
namespace NzbDrone.Core.Datastore
{
@ -56,12 +54,28 @@ public static void Map()
private static void RegisterMappers()
{
RegisterEmbeddedConverter();
MapRepository.Instance.RegisterTypeConverter(typeof(Int32), new Int32Converter());
MapRepository.Instance.RegisterTypeConverter(typeof(Boolean), new BooleanIntConverter());
MapRepository.Instance.RegisterTypeConverter(typeof(Enum), new EnumIntConverter());
MapRepository.Instance.RegisterTypeConverter(typeof(QualityModel), new EmbeddedDocumentConverter());
MapRepository.Instance.RegisterTypeConverter(typeof(List<MediaCover.MediaCover>), new EmbeddedDocumentConverter());
MapRepository.Instance.RegisterTypeConverter(typeof(Quality), new QualityIntConverter());
}
private static void RegisterEmbeddedConverter()
{
var embeddedTypes = typeof(IEmbeddedDocument).Assembly.GetTypes()
.Where(c => c.GetInterfaces().Any(i => i == typeof(IEmbeddedDocument)));
var embeddedConvertor = new EmbeddedDocumentConverter();
var genericListDefinition = typeof(List<>).GetGenericTypeDefinition();
foreach (var embeddedType in embeddedTypes)
{
var embeddedListType = genericListDefinition.MakeGenericType(embeddedType);
MapRepository.Instance.RegisterTypeConverter(embeddedType, embeddedConvertor);
MapRepository.Instance.RegisterTypeConverter(embeddedListType, embeddedConvertor);
}
}
}
}

View File

@ -5,7 +5,7 @@
namespace NzbDrone.Core.Qualities
{
public class Quality : IComparable<Quality>, IEmbeddedDocument
public class Quality : IComparable<Quality>
{
public int Id { get; set; }
public string Name { get; set; }