Abstract base class and inheritance

marcus's Avatar

marcus

03 Mar, 2010 05:43 AM via web

I have an abstract base class and would like to be able to inherit from this class in multiple step e.g create a second abstract base class that inherits from the first and then inherit the second and so on.

I'm using the automapping feature but have figured out how to setup the mappings correct.

This is how it looks today with:

        var cfg = Fluently.Configure()
            .Database(MsSqlConfiguration.MsSql2008.ShowSql()
            .ConnectionString(c => c.FromConnectionStringWithKey("conn")))
            .Mappings(m => m.AutoMappings.Add(AutoMap.AssemblyOf<ContentItem>()
            .AddEntityAssemblys(x => x.AssembliesFromApplicationBaseDirectory())
            .Where(t => t.IsSubclassOf(typeof(ContentItem)))
            .Setup(s => {
                s.IsDiscriminated = type => true;
                s.DiscriminatorColumn = type => "type";
                s.SubclassStrategy = type => SubclassStrategy.Subclass;
            })
            ))

So how can I write the where statement if I would like to be able to have a chain of inheritance from contentitem?

  1. Support Staff 2 Posted by James Gregory on 04 Mar, 2010 01:20 PM

    James Gregory's Avatar

    Do you want your abstract base classes to be represented in the database? Or are they simply layer-supertype's that just contain shared implementation details?

    If it's the latter, then you'll need to use the IgnoreBase<T>() method to tell the automappings to ignore those base classes.

  2. 3 Posted by marcus on 04 Mar, 2010 03:00 PM

    marcus's Avatar

    ContentItem is represented in the database so IgnoreBase<T>() does not work for me.
    This is an example of how I use my code

    public abstract class ContentItem {
      public virtual int? Id { get; set; }
      public virtual string Name { get; set; }
      public virtual IList<ContentDetail> Details { get; private set; }
    
      protected ContentItem() {
        Details = new List<ContentDetail>();
      }
    
      protected virtual object GetDetail(string detailName) {
        return Details.Any(d => d.Name == detailName)
                       ? Details.Where(d => d.Name == detailName).SingleOrDefault().Value
                       : null;
      }
    }
    
    public abstract class BasePage : ContentItem {
      public virtual string Heading {
        get { return GetDetail("Heading"); }
      }
    }
    
    public class StandardPage : BasePage {
      // code here
    }
    
  3. Support Staff 4 Posted by James Gregory on 12 Mar, 2010 09:49 AM

    James Gregory's Avatar

    You're going to have to give me some more to work with here. What database structure do you desire? What are your current mappings producing?

    We've established ContentItem will be stored in the database. What about BasePage? Are these all going to be stored in the same table, or do you want them split table-per-subclass?

  4. 5 Posted by marcus on 12 Mar, 2010 11:01 AM

    marcus's Avatar

    Yes ContentItem will be stored in the database. I decorate all members with a custom attribute to decide if they should be mapped and this is my mappings for that.
    .OverrideAll(o => o.IgnoreProperties(p => p.DeclaringType.GetCustomAttributes(typeof(ContentDetailAttribute), false).Length <= 0))

    My members on the entites that inherit ContentItem will not be automapped so I use the Details collection to save my properties to the database in a table called ContentDetail.

    I have figured out that if my BasePage not is abstract I will get the result I want so i guess this is not any problem for me anymore. I hope you understand what I try to discribe :)

  5. James Gregory closed this discussion on 24 Mar, 2010 08:59 AM.

Comments are currently closed for this discussion. You can start a new one.