We have several cases like this. We handle them the following way:
1. All of our entities implement a specific interface (which we call IEntity).
2. The IEntity interface defines a property of type IEntityStrategy. The IEntityStrategy interface defines methods SetDefaults() and Validate().
3. Both our implementation of IInterceptor.Instantiate(), and our entity factory (which creates new, transient entity instances) creates and attaches an instance of the appropriate strategy to the entity. When a transient entity is created, its strategy's SetDefaults() method is called. We then code whatever is necessary in SetDefaults() for that entity's strategy.
The strategy classes live in a separate DLL from the entities themselves, and are "persistence aware" (i.e. can execute HQL). If SetDefaults() needs to get some next sequence number, possibly based on some filter like a parent foreign key, by executing some HQL, it can do that.
|