views:

32

answers:

1

I think this is a moderately neophyte question. I've been using NHibernate/FluentNHibernate for about 6 months or so, and I think I have a fair grasp of the basics. Now I'm pushing a little bit beyond. I'm sure this is documented, but I've spent several hours now on Google, and haven't quite figured it out.

I have simple class, call it Simple:

public class Simple
{ public string Name { get; set;}
  public string Desc { get; set; }
  public string Status { get; set;}
}

Status is actually constrained to be one of a small set of values: "ACTIVE", "ON-HOLD", and "CLOSED". In the DB schema, I have a simple read-only table called RetrofitStatus:

CREATE TABLE [dbo].[RetrofitStatus](
[Status] [nvarchar](10) NOT NULL,
[SortOrder] [smallint] NOT NULL,
CONSTRAINT [PK_RetrofitStatus] PRIMARY KEY CLUSTERED 
(
    [Status] ASC
)

And there's a Foreign-key constraint on the Simple table to make sure Status is in RetrofitStatus.

The purpose of the RetrofitStatus table is to include the SortOrder column so that I can do (in SQL)

SELECT * from Simple s join RetrofitStatus r on r.Status=s.Status ORDER BY r.SortOrder

and sort my results into a display order (e.g., all ACTIVE entries first, then ON-HOLD entries, then CLOSED entries, since I'm sorting on a logical order, not alphabetical or anything else easily recognize).

My question is, how do I model that object and relationship in FNH? I can add the SortOrder property to my Simple definition and a make a View that represents that, but what will happen when I try to insert new Simple objects into the table? Obviously, I don't want to try to insert the SortOrder property. I think I need to use the NHib element somehow, but again, I'm not clear on where that needs to go, what happens on insert, and how to do it in Fluent?

I'm going to try experimenting with trial-and-error, but I thought that maybe somebody who knows what they're doing (which I don't) can point me in the right direction and save me some time. Thanks for your help.

+1  A: 

Assuming you had a Simple class modified as follows (I wasn't sure what you were doing about a key):

public class Simple
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Desc { get; set; }
    public string Status { get; set; 
    public int SortOrder { get; set; }
}

you could map like this:

public SimpleMap : ClassMap<Simple>
{
    Id(x => x.Id).Unique().GeneratedBy.Native();
    Map(x => x.Name);
    Map(x => x.Desc);
    Join("RetrofitStatus", join =>
    {
        join.KeyColumn("Status") 
            .Map(x => x.Status);
        join.Map(x => x.SortOrder).Not.Insert();
    });
}

I haven't tested this, but this is the gist of it.

You can specify that a property cannot be inserted or updated by using Not.Insert() or Not.Update(), respectively in your mapping, or can be neither inserted nor updated by using ReadOnly().

Jay