IoC Containers and Domain Driven Design

后端 未结 3 529
时光取名叫无心
时光取名叫无心 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));
    

提交回复
热议问题