oonyalo
oonyalo

Reputation: 466

Fluent-NHibernate ICompositeUserType for Domain Entity Objects

I have the following domain entity objects:

public class Report
{
    public virtual int Id { get; set; }
    public virtual int Score { get; set; }
    public virtual EntityType Type { get; set; }
    public virtual Object Entity { get; set; }
}

public class Category 
{ 
    public virtual int Id { get; set; }
    public virtual string Name { get; set; }
}

public class Topic
{ 
    public virtual int Id { get; set; }
    public virtual string Name { get; set; }
}

Report.Entity can either be a Category or a Topic. The type is indicated by Report.Type. EntityType is an enum. The goal is to be able to save the report class using fluent-nhibernate. I believe I can accomplish this using the ICompositeUserType, which would give me the following:

public class Report
{
    public virtual int Id { get; set; }
    public virtual int Score { get; set; }
    public virtual EntityCompositeUserType Entity { get; set; }
    public virtual EntityType Type { get; set; }
}

My question is: Is it possible to have the NullSafeGet method in the EntityCompositeUserType class return a domain entity object (either Category or Topic)? All of the examples of ICompositeUserType that I have seen create a new object from column(s) in the current table (in my case, from columns in the Report table). I saw one mention of using columns from multiple tables but did not see an implementation of it.

Upvotes: 0

Views: 227

Answers (1)

Firo
Firo

Reputation: 30813

i would suggest

public class Report
{
    public virtual int Id { get; set; }
    public virtual int Score { get; set; }
    public virtual Entity Entity { get; set; }
}

public class Category : Entity
{ 
    public virtual string Name { get; set; }
}

public class Topic : Entity
{ 
    public virtual string Name { get; set; }
}

public class Entity
{ 
    public virtual int Id { get; set; }
}

public ReportMap()
{
    ReferenceAny(x => x.Entity)...
        .EntityIdentifierColumn("entirtyid")
        .EntityTypeColumn("entitytype")
        .IdentityType<int>()
        .MetaType<string>()
        .AddMetaValue<Category>("category")
        .AddMetaValue<Topic>("topic");
}

Upvotes: 1

Related Questions