Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fluent NHibernate JoinedSubClass is obsolete

I wonder about something. I'm sitting here with a solution there I have 1 superclass that has 2 subclasses and I'm currently mapping this using JoinedSubClass, but I get that this method is obsolete, and says that I should ClassMap and SubClassMap, but if I do this the AutoMapping does not work, and I don't want that. Is there any workaround for this?

Here's the hierarchy:

public class Tag : Entity
{

public virtual string Name {get;set;}
public virtual User User {get;set;}

}

public class RespondentTag : Tag
{
    public virtual IList<Respondent> Respondents {get;set;}
}


public class ArchiveTag : Tag
{
    public virtual IList<Survey> Surveys {get;set;}
}

As you probably figured out I want this to be a table per hierarchy-mapping with subclasses with lists that are Many-To-Many. Like a table 'Tag', then Tag_Respondent and Tag_Archive (for many-to-many relationship).

Here's the mapping that I'm currently using:

public class TagMap : IAutoMappingOverride<Tag>
{
  public void Override(AutoMapping<Tag> mapping)
  { 
     //This is obsolete
     mapping.JoinedSubClass("RespondentTagId", RespondentTagMap.AsJoinedSubClass());
     mapping.JoinedSubClass("ArchiveTagId", ArchiveTagMap.AsJoinedSubClass());

  }
}

public class RespondentTagMap
{
    public static Action<JoinedSubClassPart<RespondentTag>> AsJoinedSubClass()
    {
     return part =>

        part.HasManyToMany(x => x.RespondentList)
           .Cascade
           .SaveUpdate()
           .Inverse()
           .Table("Tag_Respondent");

    }
}


public class ArchiveTagMap
{
    public static Action<JoinedSubClassPart<ArchiveTag>> AsJoinedSubClass()
    {
     return part =>

        part.HasManyToMany(x => x.Surveys)
           .Cascade
           .SaveUpdate()
           .Inverse()
           .Table("Tag_Archive");

    }
}

Does anyone know about a workaround or another solution for solving this? (Without disabling automapping)

Any answers will be appreciated.

Thanks in advance!

like image 446
dbn2k Avatar asked Jul 23 '10 07:07

dbn2k


1 Answers

Forgive me if I'm misunderstanding your objective, but I'll take a stab at this since I have similar inheritance in my project (though I use a table-per-base-class pattern with a discriminator column).

I believe you can accomplish what you're looking to do by having FNH ignore your Tag base class, then overriding the mapping on your RespondentTag and ArchiveTag objects to implement the many-to-many relationship. So in your FNH configuration, you'd specify an argument to your mappings call of:

m.AutoMappings.Add(AutoMap.AssemblyOf<SomeObjectInMyAssembly>(new MyAutoMapConfig()) // Assuming you're using a config class
    .IgnoreBase(typeof(Entity))
    .IgnoreBase(typeof(Tag))
    .UseOverridesFromAssemblyOf<SomeOverrideClass>());

Then you'd have to set up overrides in whatever assembly you're storing them. You'd have something like this:

public class RespondentTagOverride : IAutoMappingOverride<RespondentTag>
{
    public void Override(AutoMapping<RespondentTag> mapping)
    {
        mapping.HasManyToMany(x => x.RespondentList)
            .Cascade
            .SaveUpdate()
            .Inverse()
            .Table("Tag_Respondent"); // Not sure if the table call works in the override...you may have to use a convention for this
    }
}

Same for the ArchiveTag object.

That's similar to what I do in my inheritance scheme, though as I mentioned, in my automap config class I override the IsDiscriminated method to indicate that my objects are table-per-base-class and discriminated.

like image 63
Josh Anderson Avatar answered Sep 18 '22 16:09

Josh Anderson