Added support for embedding lists of IEmbeddedDocument types.

This commit is contained in:
Keivan Beigi 2013-03-26 15:17:13 -07:00
parent b16d85d0fc
commit 57120c9eeb
4 changed files with 75 additions and 6 deletions

View File

@ -0,0 +1,55 @@
using System;
using System.Collections.Generic;
using FluentAssertions;
using NUnit.Framework;
using NzbDrone.Core.Datastore;
using NzbDrone.Core.Tv;
namespace NzbDrone.Core.Test.Datastore
{
[TestFixture]
public class MappingExtensionFixture
{
public class EmbeddedType : IEmbeddedDocument
{
}
public class TypeWithAllMappableProperties
{
public string PropString { get; set; }
public int PropInt { get; set; }
public bool PropBool { get; set; }
public Nullable<int> PropNullable { get; set; }
public EmbeddedType Embedded { get; set; }
public List<EmbeddedType> EmbeddedList { get; set; }
}
public class TypeWithNoMappableProperties
{
public Series Series { get; set; }
public List<string> ListOfStrings { get; set; }
public int ReadOnly { get; private set; }
public int WriteOnly { private get; set; }
}
[Test]
public void test_mappable_types()
{
var properties = typeof(TypeWithAllMappableProperties).GetProperties();
properties.Should().NotBeEmpty();
properties.Should().OnlyContain(c => MappingExtensions.IsMappableProperty(c));
}
[Test]
public void test_un_mappable_types()
{
var properties = typeof(TypeWithNoMappableProperties).GetProperties();
properties.Should().NotBeEmpty();
properties.Should().NotContain(c => MappingExtensions.IsMappableProperty(c));
}
}
}

View File

@ -139,6 +139,7 @@
<ItemGroup>
<Compile Include="Datastore\BasicRepositoryFixture.cs" />
<Compile Include="Datastore\DatabaseRelationshipFixture.cs" />
<Compile Include="Datastore\MappingExtentionFixture.cs" />
<Compile Include="Datastore\ObjectDatabaseFixture.cs" />
<Compile Include="Framework\CoreTest.cs" />
<Compile Include="Framework\DbTest.cs" />

View File

@ -28,11 +28,6 @@ namespace NzbDrone.Core.Datastore.Converters
{
if (clrValue == null) return null;
if (clrValue as IEmbeddedDocument == null)
{
throw new InvalidOperationException("Attempted to embedded an object not marked with IEmbeddedDocument");
}
var json = JsonConvert.SerializeObject(clrValue);
return json;
}

View File

@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Marr.Data.Mapping;
@ -29,7 +30,14 @@ namespace NzbDrone.Core.Datastore
return true;
}
return propertyInfo.CanRead && propertyInfo.CanWrite && IsSimpleType(propertyInfo.PropertyType);
if (typeof(IEnumerable<IEmbeddedDocument>).IsAssignableFrom(propertyInfo.PropertyType))
{
return true;
}
var result = propertyInfo.IsReadable() && propertyInfo.IsWritable() && IsSimpleType(propertyInfo.PropertyType);
return result;
}
public static bool IsSimpleType(Type type)
@ -45,5 +53,15 @@ namespace NzbDrone.Core.Datastore
|| type == typeof(DateTime)
|| type == typeof(Decimal);
}
private static bool IsReadable(this PropertyInfo propertyInfo)
{
return propertyInfo.CanRead && propertyInfo.GetGetMethod(false) != null;
}
private static bool IsWritable(this PropertyInfo propertyInfo)
{
return propertyInfo.CanWrite && propertyInfo.GetSetMethod(false) != null;
}
}
}