Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<blockquote> <p>But what is not clear to me is where the factory "layer" lies with a DDD architecture? Should the factory be calling directly into the repository to get its data or the service library?</p> </blockquote> <p>The factory should be the one-stop shop to construct domain objects. Any other part of the code that needs to do this should use the factory.</p> <p>Typically, there are at least three sources of data that are used as input into a factory for domain object construction: input from the UI, the results of queries from persistence, and domain-meaningful requests. So to answer your specific question, the repository would use the factory.</p> <p>Here is an example. I am using <a href="http://www.javaworld.com/javaworld/jw-01-2004/jw-0102-toolbox.html?page=4" rel="noreferrer">Holub's Builder pattern</a> here. <strong>Edit</strong>: disregard the use of this pattern. I've started realizing that it <a href="http://moffdub.wordpress.com/2009/02/22/i-suffer-from-ocd-odorless-code-dreamer/" rel="noreferrer">doesn't mix too well with DDD factories</a>. </p> <pre><code>// domain layer class Order { private Integer ID; private Customer owner; private List&lt;Product&gt; ordered; // can't be null, needs complicated rules to initialize private Product featured; // can't be null, needs complicated rules to initialize, not part of Order aggregate private Itinerary schedule; void importFrom(Importer importer) { ... } void exportTo(Exporter exporter) { ... } ... insert business logic methods here ... interface Importer { Integer importID(); Customer importOwner(); Product importOrdered(); } interface Exporter { void exportID(Integer id); void exportOwner(Customer owner); void exportOrdered(Product ordered); } } // domain layer interface OrderEntryScreenExport { ... } // UI class UIScreen { public UIScreen(OrderEntryDTO dto) { ... } } // App Layer class OrderEntryDTO implements OrderEntryScreenExport { ... } </code></pre> <p>Here is what the OrderFactory might look like:</p> <pre><code>interface OrderFactory { Order createWith(Customer owner, Product ordered); Order createFrom(OrderEntryScreenExport to); Order createFrom(List&lt;String&gt; resultSets); } </code></pre> <p>The logic for the featured Product and the generation of the Itinerary go in the OrderFactory.</p> <p>Now here is how the factory might be used in each instance.</p> <p>In OrderRepository:</p> <pre><code>public List&lt;Order&gt; findAllMatching(Criteria someCriteria) { ResultSet rcds = this.db.execFindOrdersQueryWith(someCriteria.toString()); List&lt;List&lt;String&gt;&gt; results = convertToStringList(rcds); List&lt;Order&gt; returnList = new ArrayList&lt;Order&gt;(); for(List&lt;String&gt; row : results) returnList.add(this.orderFactory.createFrom(row)); return returnList; } </code></pre> <p>In your application layer:</p> <pre><code>public void submitOrder(OrderEntryDTO dto) { Order toBeSubmitted = this.orderFactory.createFrom(dto); this.orderRepo.add(toBeSubmitted); // do other stuff, raise events, etc } </code></pre> <p>Within your domain layer, a unit test perhaps:</p> <pre><code>Customer carl = customerRepo.findByName("Carl"); List&lt;Product&gt; weapons = productRepo.findAllByName("Ruger P-95 9mm"); Order weaponsForCarl = orderFactory.createWith(carl, weapons); weaponsForCarl.place(); assertTrue(weaponsForCarl.isPlaced()); assertTrue(weaponsForCarl.hasSpecialShippingNeeds()); </code></pre> <blockquote> <p>Where does the factory fit into the following framework: UI > App > Domain > Service > Data</p> </blockquote> <p>Domain.</p> <blockquote> <p>Also, because the factory is the only place allowed for object creation would'nt you get circular references if you wanted to create your objects in your data and service layers?</p> </blockquote> <p>In my example, all dependencies flow from top to bottom. I used the <a href="http://www.objectmentor.com/resources/articles/dip.pdf" rel="noreferrer">Dependency Inversion Principle</a> (PDF link) to avoid the problem you speak of.</p> <blockquote> <p>If the role of the factory class is for object creation then what benefits does the service layer have?</p> </blockquote> <p>When you have logic that doesn't fit into any single domain object OR you have an algorithm that involves orchestrating multiple domain objects, use a service. The service would encapsulate any logic that doesn't fit in anything else and delegate to domain objects where it does fit. </p> <p>In the example I scribbled here, I imagine that coming up with an Itinerary for the Order would involve multiple domain objects. The OrderFactory could delegate to such a service.</p> <p>BTW, the hierarchy you described should probably be UI > App > Domain Services > Domain > Infrastructure (Data)</p> <blockquote> <p>I've asked a lot of questions and appreciate any response. What i'am lacking is a sample application which demonstrates how all the layers in a domain driven design project come together...Is there anything out there?</p> </blockquote> <p><a href="http://rads.stackoverflow.com/amzn/click/0321268202" rel="noreferrer">Applying Domain Driven Design and Patterns</a> by Jimmy Nilsson is a great compliment to Eric Evans' <a href="http://rads.stackoverflow.com/amzn/click/0321125215" rel="noreferrer">Domain-Driven Design</a>. It has lots of code examples, though I don't know if there is an emphasis on layering. Layering can be tricky and is almost a topic separate from DDD.</p> <p>In the Evans book, there is a very small example of layering you might want to check out. Layering is an enterprise pattern, and Martin Fowler wrote <a href="http://rads.stackoverflow.com/amzn/click/0321127420" rel="noreferrer">Patterns of Enterprise Application Architecture</a>, which you might find useful too.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload