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().
相关问题
-
NHibernate Query
with detached criteria… - Fluent NHibernate automap PostGIS geometry type
- Using NHibernate to execute DDL statements
- StructureMap: Multithreaded env. No default instan
- Setting Linq to NHibernate ADO Command Timeout
相关文章
- Fluent NHibernate — Saving Entity with Composite K
- Is this the right way of using ThenFetch() to load
- Can Persistence Ignorance Scale?
- How to find if a referenced object can be deleted?
- NHibernate SQL query slow
- NHibernate: How to perform eager subselect fetchin
- Nhibernate Conformist Mapping “Unable to determine
- Using NHibernate transaction in SqlBulkCopy
You can do setterinjetion by implementing IInterceptor and doing the injection in the onload method with Objectfactory.BuildUp. This is easy to implement, setter injection might not be what you want.
You can do constructor injection by implementing your own IBytecodeProvider and ReflectionOptimizer. This is a little harder to do, but it is possible!
I don't recommend to inject things in entities. In 99.9% of the cases this will lead to bad design and make layering in your design impossible. Maybe you facing the 0.01% of the cases where it is the right thing to do. It's something you do as frequently as typing goto statements.
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!
Take a look at this post from Fabio Maulo (current NHibernate lead dev), he solved this some months ago. The code is for NHibernate 2.1 but I think it could be backported to 2.0.1GA by removing the IProxyFactoryFactory parts.