You can use enums for the IDs, which would allow you to write, for example:
Session.Load<WorkflowStatus>(WorkflowStatuses.Accepting);
You can use that in Criteria or HQL queries by using parameters:
Session.CreateQuery("from SomeBusinessCase b where b.Status.id = :status")
.SetParameter("status", WorkflowStatuses.Accepting);
or
Session.CreateQuery("from SomeBusinessCase b where b.Status = :status")
.SetParameter("status",
Session.Load<WorkflowStatus>(WorkflowStatuses.Accepting));
Of course if you use those enums as IDs you might as well ignore the
"dictionary" table completely (i.e. keep it unmapped) and just use map
SomeBusinessCase.Status as a property instead of a many-to-one.
A cleaner approach (if you're comfortable with it) is to use the Id as a
discriminator and use WorkflowStatus for a State/Strategy pattern.
Diego
On Sun, Dec 13, 2009 at 18:17, rg <[email protected]> wrote:
> Hi, I'm frequently using some short dictionary tables containing id/
> name columns, for example to represent possible statuses of a simple
> workflow. Such table contains few values, like:
>
> 1, Creating
> 2, Accepting
> 3, Executing
> 4, Finished
> 5, Cancelled
>
> and is referred by an entity representing some business object - so
> by joining it I can display current status of the object. My question
> is, how to represent such constant references in C# code, so I could
> write something like:
>
> r = new SomeBusinessCase()
> r.CreatedDate = DateTime.Now;
> r.Status = WorkflowStatus.Accepting; //this should be resolved as
> Session.Load<WorkflowStatus>(2);
>
> As you see, having aliases for constant key references would help keep
> the code clean. If such 'consts' were understood by NHibernate, this
> could also be used in HQL - like "from SomeBusinessCase where Status
> == WorkflowStatus.Executing". Is there something like that in NH, or
> what is the best way to achieve something similar?
>
> Best regards
> RG
>
> --
>
> You received this message because you are subscribed to the Google Groups
> "nhusers" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected]<nhusers%[email protected]>
> .
> For more options, visit this group at
> http://groups.google.com/group/nhusers?hl=en.
>
>
>
--
You received this message because you are subscribed to the Google Groups
"nhusers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/nhusers?hl=en.