Skip to content

Commit

Permalink
Making sure to get the properties of parent interfaces as well
Browse files Browse the repository at this point in the history
  • Loading branch information
ayende committed Dec 8, 2011
1 parent 56c2b2f commit 29ef9b7
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 1 deletion.
18 changes: 17 additions & 1 deletion Raven.Client.Lightweight/Document/DocumentConvention.cs
Expand Up @@ -227,7 +227,7 @@ public PropertyInfo GetIdentityProperty(Type type)
if (currentIdPropertyCache.TryGetValue(type, out info))
return info;

var identityProperty = type.GetProperties().FirstOrDefault(FindIdentityProperty);
var identityProperty = GetPropertiesForType(type).FirstOrDefault(FindIdentityProperty);

if (identityProperty!= null && identityProperty.DeclaringType != type)
{
Expand All @@ -243,6 +243,22 @@ public PropertyInfo GetIdentityProperty(Type type)
return identityProperty;
}

private static IEnumerable<PropertyInfo> GetPropertiesForType(Type type)
{
foreach (var propertyInfo in type.GetProperties())
{
yield return propertyInfo;
}

foreach (var @interface in type.GetInterfaces())
{
foreach (var propertyInfo in GetPropertiesForType(@interface))
{
yield return propertyInfo;
}
}
}

/// <summary>
/// Gets or sets the function to find the clr type of a document.
/// </summary>
Expand Down
59 changes: 59 additions & 0 deletions Raven.Tests/MailingList/JohanNilsson.cs
@@ -0,0 +1,59 @@
using System.Linq;
using Raven.Client;
using Raven.Client.Document;
using Xunit;

namespace Raven.Tests.MailingList
{
public class JohanNilsson : RavenTest
{
internal interface IEntity
{
string Id2 { get; set; }
}

internal interface IDomainObject : IEntity
{
string ImportantProperty { get; }
}

class DomainObject : IDomainObject
{
public string Id2 { get; set; }
public string ImportantProperty { get; set; }
}

[Fact]
public void WithCustomizedTagNameAndIdentityProperty()
{
var id = string.Empty;
using (var store = NewDocumentStore())
{
var defaultFindIdentityProperty = store.Conventions.FindIdentityProperty;
store.Conventions.FindIdentityProperty = property =>
typeof(IEntity).IsAssignableFrom(property.DeclaringType)
? property.Name == "Id2"
: defaultFindIdentityProperty(property);

store.Conventions.FindTypeTagName = type =>
typeof (IDomainObject).IsAssignableFrom(type)
? "domainobjects"
: DocumentConvention.DefaultTypeTagName(type);

using (var session = store.OpenSession())
{
var domainObject = new DomainObject();
session.Store(domainObject);
var domainObject2 = new DomainObject();
session.Store(domainObject2);
session.SaveChanges();
id = domainObject.Id2;
}
var matchingDomainObjects = store.OpenSession().Query<IDomainObject>().Where(_ => _.Id2 == id).ToList();
Assert.Equal(matchingDomainObjects.Count, 1);
}


}
}
}
1 change: 1 addition & 0 deletions Raven.Tests/Raven.Tests.csproj
Expand Up @@ -561,6 +561,7 @@
<Compile Include="Linq\User.cs" />
<Compile Include="Linq\WhereClause.cs" />
<Compile Include="LocalClientTest.cs" />
<Compile Include="MailingList\JohanNilsson.cs" />
<Compile Include="MailingList\MapReduceIssue\CanPageThroughReduceResults.cs" />
<Compile Include="MailingList\DynamicQueryIndexSelection.cs" />
<Compile Include="MailingList\EnumAsInts.cs" />
Expand Down

0 comments on commit 29ef9b7

Please sign in to comment.