0

I'm using the Fluent automapper to map my classes and I currently have a base class containing summary information for a person and a derived class containing their full information:

interface IPersonSummary
{
    string Name { get; set; }
    DateTime? DOB { get; set; }
}

interface IPerson : IPersonSummary
{
    string Address { get; set; }
    string HairColor { get; set; }
}

public class PersonSummary : IPersonSummary
{
    public string Name { get; set; }
    public DateTime? DOB { get; set; }
}

public class Person : PersonSummary, IPerson
{
    public string Address { get; set; }
    public string HairColor { get; set; }
}

I know it's possible to use a projection to load from the database only the fields needed to populate a PersonSummary but it requires explicitly declaring each field to map, which kind of defeats the whole point of using the automapper in the first place. I also haven't been able to find out how to use a projection to create a new entry in the database, with the extra fields (i.e. Address and HairColor) getting set to their default values. I've tried providing an override to set both Person and PersonSummary to use the same table but as expected Fluent complained about it.

How should I go about mapping these classes to the same table?

Mark Feldman
  • 15,731
  • 3
  • 31
  • 58

1 Answers1

0

use table per hierarchy inheritance mapping in AutomappingConfiguration which uses a discriminator column

Update: exclude them from automapping and use the classmaps below or incorporate the idea into automapping

class SharedMap<T> : ClassMap<T> where T : IPersonSummary
{
    public SharedMap()
    {
        Id(x => x.Id);
        Map(x => x.Name);
        Map(x => x.DOB);
    }
}

class PersonSummaryMap : SharedMap<PersonSummary>
{
    public PersonSummaryMap()
    {
        SchemaAction.None();
    }
}

class PersonMap : SharedMap<Person>
{
    public PersonMap()
    {
        Map(x => x.Address).Default("nowhere");
        Map(x => x.HairColor).Default("black");
    }
}
Firo
  • 30,626
  • 4
  • 55
  • 94
  • Thanks for your answer, but I unless I'm not understanding you correctly I don't believe a discriminator column is quite the solution here. A discriminator column would allow for different "types", as it were, to be saved in the same table. In my case I want to be able to save a "PersonSummary" record and have all the excess fields (i.e. Address and HairColor) to be set to their default values. Then after that I want to be able to load that record as either a Person or a PersonSummary. Projections handle the loading part exactly as I want, it's the saving part that I'm trying to get working. – Mark Feldman Feb 20 '13 at 10:09