All of our entities have an associated strategy object, since our entity classes themselves contain only generated code.
We have an entity factory that keeps a dictionary of entity and strategy types to instantiate, given the class name only (replacements must preserve the class name -- i.e. the namespace and assembly must be different). Actually, for each type (entity or strategy), a stack of overriding types is kept, with each override in a given stack associated with a "customization layer". This allows customization layers to be enabled, disabled, added, etc. For entity types, the "top" override on its stack must be kept in sync with the assembly-qualified entity type name defined in the NHibernate mappings (*.hbm.xml files). Our entity generator / customization layer management utility assures this.
Our entity factory and our NHibernate interceptor then simply instantiate and attach the "top override" strategy class to all entity instances they provide.
|