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

pull/2/head
kay.one 12 years ago
parent bdcdc7c0f6
commit c1fba9093d

@ -38,7 +38,7 @@ namespace Marr.Data
internal Dictionary<Type, string> Tables { get; set; } internal Dictionary<Type, string> Tables { get; set; }
internal Dictionary<Type, ColumnMapCollection> Columns { get; set; } internal Dictionary<Type, ColumnMapCollection> Columns { get; set; }
internal Dictionary<Type, RelationshipCollection> Relationships { 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 // Explicit static constructor to tell C# compiler
// not to mark type as beforefieldinit // not to mark type as beforefieldinit
@ -203,14 +203,7 @@ namespace Marr.Data
/// <param name="converter">An IConverter object that will handle the data conversion.</param> /// <param name="converter">An IConverter object that will handle the data conversion.</param>
public void RegisterTypeConverter(Type type, IConverter converter) public void RegisterTypeConverter(Type type, IConverter converter)
{ {
if (TypeConverters.ContainsKey(type)) TypeConverters[type] = converter;
{
TypeConverters[type] = converter;
}
else
{
TypeConverters.Add(type, converter);
}
} }
/// <summary> /// <summary>

@ -1,8 +1,10 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using FluentAssertions; using FluentAssertions;
using Marr.Data;
using NUnit.Framework; using NUnit.Framework;
using NzbDrone.Core.Datastore; using NzbDrone.Core.Datastore;
using NzbDrone.Core.Datastore.Converters;
using NzbDrone.Core.Tv; using NzbDrone.Core.Tv;
namespace NzbDrone.Core.Test.Datastore namespace NzbDrone.Core.Test.Datastore
@ -35,6 +37,15 @@ namespace NzbDrone.Core.Test.Datastore
public int WriteOnly { private get; set; } 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] [Test]
public void test_mappable_types() public void test_mappable_types()

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

@ -1,7 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq.Expressions; using System.Linq;
using System.Reflection;
using Marr.Data; using Marr.Data;
using Marr.Data.Mapping; using Marr.Data.Mapping;
using NzbDrone.Core.Configuration; using NzbDrone.Core.Configuration;
@ -16,7 +15,6 @@ using NzbDrone.Core.ReferenceData;
using NzbDrone.Core.RootFolders; using NzbDrone.Core.RootFolders;
using NzbDrone.Core.Tv; using NzbDrone.Core.Tv;
using BooleanIntConverter = NzbDrone.Core.Datastore.Converters.BooleanIntConverter; using BooleanIntConverter = NzbDrone.Core.Datastore.Converters.BooleanIntConverter;
using System.Linq;
namespace NzbDrone.Core.Datastore namespace NzbDrone.Core.Datastore
{ {
@ -56,12 +54,28 @@ namespace NzbDrone.Core.Datastore
private static void RegisterMappers() private static void RegisterMappers()
{ {
RegisterEmbeddedConverter();
MapRepository.Instance.RegisterTypeConverter(typeof(Int32), new Int32Converter()); MapRepository.Instance.RegisterTypeConverter(typeof(Int32), new Int32Converter());
MapRepository.Instance.RegisterTypeConverter(typeof(Boolean), new BooleanIntConverter()); MapRepository.Instance.RegisterTypeConverter(typeof(Boolean), new BooleanIntConverter());
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(List<MediaCover.MediaCover>), new EmbeddedDocumentConverter());
MapRepository.Instance.RegisterTypeConverter(typeof(Quality), new QualityIntConverter()); 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);
}
}
} }
} }

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

Loading…
Cancel
Save