Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Map generic EntityBase<TEntity> class with FluentNHibernate

I have a base class for all my entity types which is like

public abstract class EntityBase<TEntityType> : IEntityBase where TEntityType : EntityBase<TEntityType>
    {
        private List<IBusinessRule> _brokenRules = new List<IBusinessRule>();

        private int? _hashCode;

        public int ID { private set; get; }

and in my mappings i would like to use table-per-class strategy but how to map this EntityBase class? I tryed public class EntityBaseMap:ClassMap but it doesnt work. So how could i map this class? The reason why I want that is I dont want to write the repetetive stuff with Id(c=c.ID).Not.Null .... etc but have it in one mapping class.

my mapping class look like this

public class EntityBaseMap : ClassMap<EntityBase<???>>

what should i insert instead of ???

Thanks

like image 982
Ivan Avatar asked Nov 27 '25 10:11

Ivan


1 Answers

Still you can simplify the process via reflection by creating a generic mapping and then using runtime typing, instead of creating static types:

    private static void AddWeakReferenceMappings(FluentMappingsContainer container, Assembly assembly)
    {
        var genericMappingType = typeof (WeakReferenceMap<>);
        var entityTypes = assembly.GetTypes().Where(type => type.IsSubclassOf(typeof (Entity)));
        foreach (var enitityType in entityTypes)
        {
            var newType = genericMappingType.MakeGenericType(enitityType);
            container.Add(newType);
        }
    }
like image 69
Pawel Gorczynski Avatar answered Nov 29 '25 06:11

Pawel Gorczynski



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!