I'm working on a sharp-architecture project that mixes fluent mappings and auto mappings. A basic sharp-architecture project already has a method (AutoMappingConfiguration.ShouldMap) that determines if a type should be automatically mapped or not. Mine currently looks like this:
public override bool ShouldMap(System.Type type)
{
if (type == typeof(ActiveUser))
return false;
return type.GetInterfaces().Any(x =>
x.IsGenericType && x.GetGenericTypeDefinition() == typeof(IEntityWithTypedId<>));
}
Essentially the type ActiveUser is mapped using a fluent mapping and everything else is mapped using auto mapping, except for the generic base classes of course. I'm at the point where I will be adding more fluently mapped classes and really don't want to keep adding if statements to this method to exclude them. I basically need the method
bool ShouldMap(System.Type type)
to return true if the generic class
ClassMap<type>
exists.
Any suggestions?
You can create the generic type using Type.MakeGenericType so assuming you have an assembly which contains all the mappings you can do:
public bool ShouldMap(Assembly mappingAssembly, Type type)
{
Type classMapType = typeof(ClassMap<>).MakeGenericType(type);
return mappingAssembly.GetTypes().Any(t => t.IsSubclassOf(classMapType));
}
您是否考虑过映射所有类,但是对要显式映射的类(而不是常规的流利映射)使用Mapping * Override * ?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.