1

Say I have a nice domain model, using (constructor) DI where needed. Now I want to be able to persist this model, so I start adding infrastructure(Entity Framework) to do this. What happens now is that the persistence framework should be able to initialize your types using your IoC container.

Maybe this is possible, maybe not. Anyway, what I'm wondering now is; is it usual to use DI on you POCO classes at all? And if it is, how do I make Entity Framework use my favorite IoC container(in my case NInject) to construct my classes.

Robert Massa
  • 4,345
  • 1
  • 32
  • 44

3 Answers3

5

It is more than OK to use IoC container to construct entities fetched from the database, I do that in my project.

ORM should not dictate your design. NHibernate can play nice with IoC Container, I have no idea about EF but I suspect it does not. I'd swap EF for NHibernate, or actually anything else if I were you.

Krzysztof Kozmic
  • 27,267
  • 12
  • 73
  • 115
  • Ok fair enough. That answers one of my questions, so I will keep using DI. I appreciate your recommendation for NH, but would really like this to work with EF. – Robert Massa Mar 03 '10 at 09:27
  • 1
    Than you might want to check out EF2, it is supposedly a lot less invasive so it's more likely it lets you plug into construction pipeline to supply services from IoC Container – Krzysztof Kozmic Mar 03 '10 at 09:34
  • I'm using EF4 with the POCO Templates, but the documentation is a bit messy and spread all over the web. – Robert Massa Mar 03 '10 at 09:49
2

This is one of the religious arguments in the DDD community: should I inject services into my entities? It's really a question that you'll have to answer yourself. I"m not going to tell you what I think because it's highly contextual - what I think changes depending on several factors.

I do think you need to take a few hours and really dig deep into the archives of the ddd list to establish an answer that will work for you.

Matt Hinze
  • 13,577
  • 3
  • 35
  • 40
  • Could you maybe elaborate on which factors it depends? Thanks for the link to the ddd archive, I'll dig into that! – Robert Massa Mar 03 '10 at 14:26
  • Sure.. it depends on.. everything. What's the testing story, what does injecting enable, what are your design and architectural goals, what is your architectural style, how hard is it to debug and otherwise work with.. – Matt Hinze Mar 03 '10 at 18:49
1

I you would like to use EF so consider EF4 where you can use POCO templates indeed. You can implement Repositories around your POCO classes and you will be able to use IoC as you use it with another ORM. Whan you have POCO it's all about application architecture layaring.

Look at this walktrough:

http://blogs.msdn.com/adonet/pages/walkthrough-poco-template-for-the-entity-framework.aspx

Tomasz Jaskuλa
  • 15,723
  • 5
  • 46
  • 73
  • That is exactly what I'm using :-) Also using repositories, but my objects still get instantiated by the EF. And if I want to use Lazy Loading(which required proxy classes) I need to use `context.CreateObject` from EF. This method is non-virtual and I don't see any other extension point. – Robert Massa Mar 03 '10 at 14:23
  • 1
    Youd don't need to use the context. If you mark EVERY property of your POCO class as virtual, then EF will perform a neat trick. It will spin up proxy EntityObjects on behalf of the POCO class which will do what EntityObjects already know how to do – notify the context of changes to properties & relationships as well as allow lazy loading. – Tomasz Jaskuλa Mar 03 '10 at 20:59