ability to use collection attribute on base interfaces

This commit is contained in:
2020-05-28 01:14:26 +02:00
parent ec1d409f3f
commit 65f7421ef4
4 changed files with 34 additions and 46 deletions
@@ -21,13 +21,16 @@ namespace zero.Core.Extensions
{
Type[] polymorphTypes = new Type[2] { typeof(SpaceContent), typeof(Page) };
Type dbConventionType = typeof(IZeroDbConventions);
store.Conventions.IdentityPartsSeparator = ".";
store.Conventions.FindCollectionName = type =>
{
Type finalType = type;
// do not alter non-internal entities
if (!typeof(IZeroDbConventions).IsAssignableFrom(type))
if (!dbConventionType.IsAssignableFrom(type))
{
return DocumentConventions.DefaultGetCollectionName(type);
}
@@ -39,9 +42,26 @@ namespace zero.Core.Extensions
return collection.Name;
}
// use base interface if available
Type interfaceBaseType = type.GetInterfaces().FirstOrDefault(x => x.IsInterface && dbConventionType.IsAssignableFrom(x) && x.Name != dbConventionType.Name);
if (interfaceBaseType != null)
{
// use name from attribute if available
collection = interfaceBaseType.GetCustomAttribute<CollectionAttribute>(true);
if (collection != null)
{
return options.Raven.CollectionPrefix + collection.Name;
}
}
// use base type for polymorphism
Type polymorphBaseType = polymorphTypes.FirstOrDefault(x => type.IsSubclassOf(x));
Type finalType = polymorphBaseType ?? type;
if (polymorphBaseType != null)
{
finalType = polymorphBaseType;
}
return options.Raven.CollectionPrefix + DocumentConventions.DefaultGetCollectionName(finalType);
};