IoC Containers and Domain Driven Design

后端 未结 3 528
时光取名叫无心
时光取名叫无心 2021-02-02 15:50

I\'ve been searching for guidance for using IoC containers in domain driven design. Evan\'s book unfortunately doesn\'t touch on the subject. The only substantial guidelines I c

相关标签:
3条回答
  • 2021-02-02 16:01

    We use DDD, and dependency injection (the pattern), but do not use a dependency injection framework.

    One problem with popular dependency injection frameworks is how they separate configuration into XML files. XML is a great markup language. How it became a configuration language, I will never understand. The issue, of course, is that you have to run the application before you know if everything is wired up correctly. It is also hard to see what code is used where. If you are using interfaces, then the only reference to an implementation will be in an XML file, which is harder to spot. And finally you lose type safety and generics. (I once saw a horrible bug in production which would have been caught by the compiler had we not been using XML.)

    I should point out that I am not saying that dependency injection is bad. It is a fundamental pattern of good object design. But there is absolutely nothing wrong with doing wiring in a factory.

    At my last two projects we have removed large amounts of "code" out of XML files, and into factories. The factories are wired up with container managed services (such as JDBC connections, JMS connections and so forth). The application has become much simpler to understand, because the factory is less verbose than XML. And as a Java programmer, it is much easier to wire together a program using control-space, instead of XML twiddling - and your IDE will highlight when you have broken something.

    In an integration test, just create the objects as you would in a factory.

    ie,

    dbConnection = DatabaseConnectionFactory.connection();    
    serviceUnderTest = new PaymentProcessor(new CustomerRepository(connection));
    
    0 讨论(0)
  • 2021-02-02 16:07

    In my opinion he is correct about not using IoC container in domain model. That practice I follow myself as well. Basic idea is that services may contain infrastructure dependencies and therefore its wise to mock them. Domain entities don't have those, so its not important to mock them up (still coding to interfaces is good practice).

    Factories for domain entities should not be in IoC container, but factories for services should. Basically you may reference entity factories in your services. It's not very tight coupling.

    Good reading about IoC can be found at Billy McCafferty's blog post "Dependency Injection 101"

    0 讨论(0)
  • 2021-02-02 16:11

    IOC containers are invaluable when designing unit-testable code and are orthogonal to DDD. You could create your own implementation of factory and builder patterns if you want...by why go through the trouble?

    Absolutely. Use an IOC container that's just powerful enough to meet you specific requirements; no more, no less.

    0 讨论(0)
提交回复
热议问题