Added support for embedding lists of IEmbeddedDocument types.

pull/4/head
Keivan Beigi 12 years ago
parent b16d85d0fc
commit 57120c9eeb

@ -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));
}
}
}

@ -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" />

@ -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;
}

@ -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;
}
}
}
Loading…
Cancel
Save