Is there a way to replace the call to Activator.CreateInstance() used inside NHibernate 2.0.1GA to construct the entities? Ideally I’d like to replace it with StructureMap.ObjectFactory.GetInstance().
Is there a way to replace the call to Activator.CreateInstance() used inside NHibernate 2.0.1GA
Share
You can’t do this easily with constructor injection. The NHibernate internals may need to create a proxy object inherited from your domain class with Lazy loading code etc. sprinkled in there, so, as far as I know, there’s no simple option to override the construction of your object.
You can get dependency injection working with NHibernate fairly easily though, by writing an interceptor that will build up the object for you via property setter injection. There is an example of this here. That example is using Windsor as the DI container, but you can do the same thing with StructureMap of course – here are the relevant docs for StructureMap – look for the section on ‘Applying Setter Injection to an Existing Object (BuildUp)’.
Caveat: injecting dependencies into your domain model is often seen as a symptom of a design problem, and many people avoid doing this, so be sure this is really what you want to do before you start coding it!