views:

42

answers:

1

Hi

I'm not sure if my problem is solvable in a more or less comfortable way.

There is a class Person which has mapped 'hasOne' a participant.

The person has a birthday but this field is not required on the person itself. But if I would like to add a participant to the person then the birthday is required.

How to get rid of this

  • move the birthday information to the participant object -> That's how I did it for now, but I think moving person related information to other objects cannot be the final solution
  • Map the property in both classes -> how to tell the validator when it's required and when not?
  • Merge the two objects -> for now not allowed

Maybe someone has a better idea, I use the nHibernate Validator and there I configure the validation in the class (where the information 'birthday' is not a property on both - maybe this would be a solution?)

A: 

With NHibernate validator attributes as you mentioned. This would be in the Participant class. Would this work for you? It is optional on the base class and nullable as you see (can't change type on overriding)

[NotNull, NotEmpty]
public override DateTime? Birthday { get; set; }

This would still leave field nullable in the DB, but don't see way around this without having a table per class implementation rather than a class per hierarchy fluent implementation. Your domain validation will be there to protect though.

dove
this would require that the participant class is derived from the person class. It was, but for now it's not (seemed to throw some new problems when removing the participant from the person)
griti
@griti what was the problem having this derived from person. it's only my opinion but wouldn't that be a more natural order? i know then you'd have that issue to fix but it wouldn't be a workaround...
dove
A co-worker dealed with it and when he derived from person, it messed up by saving a new person to the participant. But I will try this by myself.
griti