3

This question has many parts:

  1. Is there any ORM for .NET that actually supports using an IoC container ?
  2. If no, Why not ?
  3. Wouldn't that force a person who loves domain-driven design (me included) to go towards using lots of service classes in the domain model making all domain object pure data structures with no behavior (don't know if that is really a bad thing) or worse to use method or property injection.
  4. Is there any direction towards supporting IoC containers in the future.
Ibrahim Najjar
  • 19,178
  • 4
  • 69
  • 95
  • 1
    This is a good question, but not for StackOverflow's question and answer format. It is likely to provoke discussion and no real answers. Maybe you can turn it into an _answerable_ question by telling which concrete programming problem you are facing that you can't seem to solve with IoC + ORM. Also, you should know that there are more questions about entities and dependency injection. Like this one: http://stackoverflow.com/questions/4835046/why-not-use-an-ioc-container-to-resolve-dependencies-for-entities-business-objec – Gert Arnold Apr 15 '13 at 22:34
  • @GertArnold thanks for the feedback. There is no actual concrete question i am facing and i asked this because it keeps popping in my face now and then. The question you referred too is great and has great connection with mine. – Ibrahim Najjar Apr 16 '13 at 10:36
  • 4
    Related: http://thinkbeforecoding.com/post/2009/03/04/How-not-to-inject-services-in-entities – Steven Apr 16 '13 at 17:05
  • @Steven, indeed a pretty relevant link, it's addressing the same questions i have asked, and i feel that the solution the author presented is somewhat method injection (which is something i don't like pretty much because it doesn't scale pretty well). but indeed a very nice article. – Ibrahim Najjar Apr 16 '13 at 17:41
  • 1
    @Sniffer: If method injection doesn't scale well, it might be that you're injecting too much into your entities. I believe the author does address this a bit. – Steven Apr 16 '13 at 18:11

1 Answers1

2

For NHibernate, yes it is possible to inject in dependencies. But for Entity Framework I'm not 100% sure.

NHibernate

With respect to NHibernate there are quite a few similar questions already about this on SO.

Injecting Dependencies into Domain Model classes with Nhibernate (ASP.NET MVC + IOC)

Dependency injection with NHibernate objects

DI/IoC, NHibernate and help in getting them to work together

To summarise these answers it is possible to inject dependencies into your POCO using Interceptors.

Entity Framework

For Entity Framework I understand it may be possible with EF 4.0 onwards, see this blog post. It seems the main drawback of this approach is that it doesn't work with lazy loading.

Community
  • 1
  • 1
mickfold
  • 2,003
  • 1
  • 14
  • 20
  • 1
    I have chosen this to be the correct answer because it addresses my first question in the list which negates all the other questions. But i have some regret regarding the question as i should have asked "Should" not "Why" and for that i recommend all users who are reading this to check out the great article Steven mentioned in the comments on the question. – Ibrahim Najjar Apr 16 '13 at 22:22