This seemingly simple problem has been driving me crazy. I want to
exclude some entities from the Auto Mapper. I don't want to automap
the User class or it's subclasses. I added the filter to the
GetAutoMappingFilter delegate that is passed into the
AutoPersistenceModel.Where filter method, but it still gets mapped. I
can debug and see that this method return False for my desired
exclusions, but they are mapped anyway.
/// <summary>
/// Provides a filter for only including types which inherit
from the IEntityWithTypedId interface.
/// </summary>
private bool GetAutoMappingFilter(Type t)
{
bool isUser = t.BaseType == typeof(User) ||
t.IsAssignableFrom(typeof(Teacher));
bool isIEntity = t.GetInterfaces().Any(x =>
x.IsGenericType && x.GetGenericTypeDefinition() ==
typeof(IEntityWithTypedId<>)
);
return !isUser && isIEntity;
}
Here's my entire AutoPersistenceModelGenerator class:
public class AutoPersistenceModelGenerator :
IAutoPersistenceModelGenerator
{
public AutoPersistenceModel Generate()
{
AutoPersistenceModel mappings = AutoPersistenceModel
// If you delete the default class, simply point the
following line to an entity within the .Core layer
.MapEntitiesFromAssemblyOf<Celebration>()
.Where(GetAutoMappingFilter)
.ConventionDiscovery.Setup(GetConventions())
.WithSetup(GetSetup())
.UseOverridesFromAssemblyOf<AutoPersistenceModelGenerator>
()
;
return mappings;
}
private Action<AutoMappingExpressions> GetSetup()
{
return c =>
{
c.FindIdentity = type => type.Name == "Id";
c.IsBaseType = IsBaseTypeConvention;
c.IsComponentType = type => type.IsSubclassOf(typeof
(ValueObject));
};
}
private Action<IConventionFinder> GetConventions()
{
return c =>
{
c.Add<PrimaryKeyConvention>();
c.Add<ReferenceConvention>();
c.Add<HasManyConvention>();
c.Add<TableNameConvention>();
};
}
/// <summary>
/// Provides a filter for only including types which inherit
from the IEntityWithTypedId interface.
/// </summary>
private bool GetAutoMappingFilter(Type t)
{
bool isUser = t.BaseType == typeof(User) ||
t.IsAssignableFrom(typeof(Teacher));
bool isIEntity = t.GetInterfaces().Any(x =>
x.IsGenericType && x.GetGenericTypeDefinition() ==
typeof(IEntityWithTypedId<>)
);
return !isUser && isIEntity;
}
private bool IsBaseTypeConvention(Type arg)
{
bool derivesFromEntity = arg == typeof(Entity);
bool derivesFromEntityWithTypedId = arg.IsGenericType &&
(arg.GetGenericTypeDefinition() == typeof
(EntityWithTypedId<>));
return (derivesFromEntity ||
derivesFromEntityWithTypedId);
}
}
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Fluent NHibernate" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/fluent-nhibernate?hl=en
-~----------~----~----~----~------~----~------~--~---