<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Injecting services into entities</title>
	<atom:link href="http://lostechies.com/jimmybogard/2010/04/14/injecting-services-into-entities/feed/" rel="self" type="application/rss+xml" />
	<link>http://lostechies.com/jimmybogard/2010/04/14/injecting-services-into-entities/</link>
	<description>Strong opinions, weakly held</description>
	<lastBuildDate>Sat, 25 May 2013 16:53:00 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.4.2</generator>
	<item>
		<title>By: Anonymous</title>
		<link>http://lostechies.com/jimmybogard/2010/04/14/injecting-services-into-entities/#comment-4642</link>
		<dc:creator>Anonymous</dc:creator>
		<pubDate>Sat, 16 Jun 2012 18:08:00 +0000</pubDate>
		<guid isPermaLink="false">/blogs/jimmy_bogard/archive/2010/04/14/injecting-services-into-entities.aspx#comment-4642</guid>
		<description>Have to agree with Michael Annucci - there seem to be lots of opinions out there, but not much consensus on best practice or analysis from the DDD experts of the pros and cons of each approach.  Here&#039;s Dan Haywood,  a very capable guy, suggesting some of the approaches you are against:  http://danhaywood.com/2010/04/30/accessing-domain-services-from-entities/.  I also have concerns that in data heavy applications (as most I work with in financial services are), the pure DDD approach (what I think you are encouraging here) either 1) results in anemic domain entities (where the majority of meaningful processing is done in a domain service layer) or 2) fails to make the best use of the more efficient database processing.</description>
		<content:encoded><![CDATA[<p>Have to agree with Michael Annucci &#8211; there seem to be lots of opinions out there, but not much consensus on best practice or analysis from the DDD experts of the pros and cons of each approach.  Here&#8217;s Dan Haywood,  a very capable guy, suggesting some of the approaches you are against:  http://danhaywood.com/2010/04/30/accessing-domain-services-from-entities/.  I also have concerns that in data heavy applications (as most I work with in financial services are), the pure DDD approach (what I think you are encouraging here) either 1) results in anemic domain entities (where the majority of meaningful processing is done in a domain service layer) or 2) fails to make the best use of the more efficient database processing.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Anonymous</title>
		<link>http://lostechies.com/jimmybogard/2010/04/14/injecting-services-into-entities/#comment-3788</link>
		<dc:creator>Anonymous</dc:creator>
		<pubDate>Thu, 01 Sep 2011 18:25:00 +0000</pubDate>
		<guid isPermaLink="false">/blogs/jimmy_bogard/archive/2010/04/14/injecting-services-into-entities.aspx#comment-3788</guid>
		<description>Don&#039;t assume that DTOs mean that your domain model is anemic. Imagine if your DTOs were actually command messages, and your domain model accepted command messages in a method.</description>
		<content:encoded><![CDATA[<p>Don&#8217;t assume that DTOs mean that your domain model is anemic. Imagine if your DTOs were actually command messages, and your domain model accepted command messages in a method.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Michael Annucci</title>
		<link>http://lostechies.com/jimmybogard/2010/04/14/injecting-services-into-entities/#comment-3787</link>
		<dc:creator>Michael Annucci</dc:creator>
		<pubDate>Thu, 01 Sep 2011 16:11:00 +0000</pubDate>
		<guid isPermaLink="false">/blogs/jimmy_bogard/archive/2010/04/14/injecting-services-into-entities.aspx#comment-3787</guid>
		<description>The frustrating part for me is that there is a dearth of good information on this topic that yields truly best practices.  Everything that you read (Martin Fowler et. al.) suggests that the anemic domain model is a code smell. Inevitably if you suggest using lightweight DTO data containers with a service layer you get called some sort of anti-pattern procedural programmer. Yet, the reality is that for most MVC based forms over data applications you will end up with an anemic model using service layers for behavior if you want an easily testable and maintainable application. 

As example let&#039;s talk about the Customer.Orders situation from the comments below.  It isn&#039;t about weather Orders is a root or about lazy loading from O/RM.  I am more interested in the best methodology for creating testable, loosely coupled rich domain models.  I only see examples for the most trivial of cases with no real complex dependencies.  In reality an application that has a create  new Order behavior is much more complicated that merely persisting raw data into the DB.   Initially it seems obvious to place the NewOrder() method as part of the Order domain object.  

Very quickly you will run into dependency issues were those dependancies are needed only for this method and will tightly couple the order to many other services/objects.  Something as simple as calculating the order total comes to mind.  You will need to access the Customer.Billing address in order to determine state and local taxes.  You will have multiple repository hits to get current tax rates.  Another hit to the Order.Products.Materials collection will be necessary since some Bill of Materials are tax exempt.  Then there is a call to the business rules service to validate that some order items either require or disqualify other line items.  Another call to Inventory to calculate shipping items on hand (and shipping time). Another call to  Shipping service with Customer.ShippingAddress to figure out estimated shipping costs.  When that is all done the Order.Salesperson and Customer need to get notified by email through yet another application service.

I could go on but I hope you all get the point.  This quickly becomes a strongly coupled untestable system just to adhere to the rich domain model.  This could all easily be resolved by creating an IOrderProcessingService whose single responsibility is to coordinate the above workflow. Since all of those dependencies are required for the service you can use constructor injection or your favorite DI container to initialize the service rather than jamming them into a rich Order object as nullable properties. Simple, testable, maintainable but not &quot;correct&quot;?  I must be missing something here.
</description>
		<content:encoded><![CDATA[<p>The frustrating part for me is that there is a dearth of good information on this topic that yields truly best practices.  Everything that you read (Martin Fowler et. al.) suggests that the anemic domain model is a code smell. Inevitably if you suggest using lightweight DTO data containers with a service layer you get called some sort of anti-pattern procedural programmer. Yet, the reality is that for most MVC based forms over data applications you will end up with an anemic model using service layers for behavior if you want an easily testable and maintainable application. </p>
<p>As example let&#8217;s talk about the Customer.Orders situation from the comments below.  It isn&#8217;t about weather Orders is a root or about lazy loading from O/RM.  I am more interested in the best methodology for creating testable, loosely coupled rich domain models.  I only see examples for the most trivial of cases with no real complex dependencies.  In reality an application that has a create  new Order behavior is much more complicated that merely persisting raw data into the DB.   Initially it seems obvious to place the NewOrder() method as part of the Order domain object.  </p>
<p>Very quickly you will run into dependency issues were those dependancies are needed only for this method and will tightly couple the order to many other services/objects.  Something as simple as calculating the order total comes to mind.  You will need to access the Customer.Billing address in order to determine state and local taxes.  You will have multiple repository hits to get current tax rates.  Another hit to the Order.Products.Materials collection will be necessary since some Bill of Materials are tax exempt.  Then there is a call to the business rules service to validate that some order items either require or disqualify other line items.  Another call to Inventory to calculate shipping items on hand (and shipping time). Another call to  Shipping service with Customer.ShippingAddress to figure out estimated shipping costs.  When that is all done the Order.Salesperson and Customer need to get notified by email through yet another application service.</p>
<p>I could go on but I hope you all get the point.  This quickly becomes a strongly coupled untestable system just to adhere to the rich domain model.  This could all easily be resolved by creating an IOrderProcessingService whose single responsibility is to coordinate the above workflow. Since all of those dependencies are required for the service you can use constructor injection or your favorite DI container to initialize the service rather than jamming them into a rich Order object as nullable properties. Simple, testable, maintainable but not &#8220;correct&#8221;?  I must be missing something here.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: bogardj</title>
		<link>http://lostechies.com/jimmybogard/2010/04/14/injecting-services-into-entities/#comment-2347</link>
		<dc:creator>bogardj</dc:creator>
		<pubDate>Fri, 11 Feb 2011 13:21:52 +0000</pubDate>
		<guid isPermaLink="false">/blogs/jimmy_bogard/archive/2010/04/14/injecting-services-into-entities.aspx#comment-2347</guid>
		<description>@RichB

Still no need to:

public string GetCreditCardNumber(IDecryptor decryptor) {
}

If it&#039;s an actual persisted value then yeah, IUserType would work.  I&#039;d probably go that route myself.</description>
		<content:encoded><![CDATA[<p>@RichB</p>
<p>Still no need to:</p>
<p>public string GetCreditCardNumber(IDecryptor decryptor) {<br />
}</p>
<p>If it&#8217;s an actual persisted value then yeah, IUserType would work.  I&#8217;d probably go that route myself.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: RichB</title>
		<link>http://lostechies.com/jimmybogard/2010/04/14/injecting-services-into-entities/#comment-2346</link>
		<dc:creator>RichB</dc:creator>
		<pubDate>Fri, 11 Feb 2011 10:47:00 +0000</pubDate>
		<guid isPermaLink="false">/blogs/jimmy_bogard/archive/2010/04/14/injecting-services-into-entities.aspx#comment-2346</guid>
		<description>Additionally, as an &quot;encrypted credit card number&quot; is a different &quot;type&quot; than something which is not encrypted, perhaps using an IUserType and then injecting the encryption key into this IUserType is a better way to model it?</description>
		<content:encoded><![CDATA[<p>Additionally, as an &#8220;encrypted credit card number&#8221; is a different &#8220;type&#8221; than something which is not encrypted, perhaps using an IUserType and then injecting the encryption key into this IUserType is a better way to model it?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: RichB</title>
		<link>http://lostechies.com/jimmybogard/2010/04/14/injecting-services-into-entities/#comment-2345</link>
		<dc:creator>RichB</dc:creator>
		<pubDate>Fri, 11 Feb 2011 10:29:40 +0000</pubDate>
		<guid isPermaLink="false">/blogs/jimmy_bogard/archive/2010/04/14/injecting-services-into-entities.aspx#comment-2345</guid>
		<description>Let&#039;s say you have a database which stores encrypted credit card numbers.

Obviously, the encryption key is held securely outside of the database. And it would make sense for a domain entity to be responsible for transparently encrypting/decrypting the credit card number.

Would you inject the encryption key into this domain entity?</description>
		<content:encoded><![CDATA[<p>Let&#8217;s say you have a database which stores encrypted credit card numbers.</p>
<p>Obviously, the encryption key is held securely outside of the database. And it would make sense for a domain entity to be responsible for transparently encrypting/decrypting the credit card number.</p>
<p>Would you inject the encryption key into this domain entity?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: ulu</title>
		<link>http://lostechies.com/jimmybogard/2010/04/14/injecting-services-into-entities/#comment-2344</link>
		<dc:creator>ulu</dc:creator>
		<pubDate>Tue, 27 Apr 2010 19:56:02 +0000</pubDate>
		<guid isPermaLink="false">/blogs/jimmy_bogard/archive/2010/04/14/injecting-services-into-entities.aspx#comment-2344</guid>
		<description>I think we already can have a lazy customer.Orders property without injecting a service. NHibernate or whatever ORM handles that for us. From this, customer.OpenOrders is just a simple LINQ query. In your tests, the entity will not query the database, even though you don&#039;t mock any services. In production, you&#039;ll have a proxy object with the required service auto-injected for you.</description>
		<content:encoded><![CDATA[<p>I think we already can have a lazy customer.Orders property without injecting a service. NHibernate or whatever ORM handles that for us. From this, customer.OpenOrders is just a simple LINQ query. In your tests, the entity will not query the database, even though you don&#8217;t mock any services. In production, you&#8217;ll have a proxy object with the required service auto-injected for you.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: isaiah</title>
		<link>http://lostechies.com/jimmybogard/2010/04/14/injecting-services-into-entities/#comment-2343</link>
		<dc:creator>isaiah</dc:creator>
		<pubDate>Thu, 15 Apr 2010 13:13:57 +0000</pubDate>
		<guid isPermaLink="false">/blogs/jimmy_bogard/archive/2010/04/14/injecting-services-into-entities.aspx#comment-2343</guid>
		<description>Well it all really depends on the context, it may well be that customers has orders within its aggregate. But in that case Orders shouldnt have a repository of it own. In my experience Orders would be its own aggregate root, Orders have their own identity, not reliant on identiy o the customer,, a Customer could potentially have thousands of orders, its doenst fit well for customer to be managing all this. again all this depends on the context of your domain. anyways again the main thing from a DDD point of view is if it makes sense in your domain for orders to be part of the customer aggregate then Orders should&#039;nt be retrieved from a repository, , it should be in the Customer, whether its lazy loaded or not is an implementation detail.
&quot;The domain model says Customer have Orders...&quot; do you mean the domain experts say that ?</description>
		<content:encoded><![CDATA[<p>Well it all really depends on the context, it may well be that customers has orders within its aggregate. But in that case Orders shouldnt have a repository of it own. In my experience Orders would be its own aggregate root, Orders have their own identity, not reliant on identiy o the customer,, a Customer could potentially have thousands of orders, its doenst fit well for customer to be managing all this. again all this depends on the context of your domain. anyways again the main thing from a DDD point of view is if it makes sense in your domain for orders to be part of the customer aggregate then Orders should&#8217;nt be retrieved from a repository, , it should be in the Customer, whether its lazy loaded or not is an implementation detail.<br />
&#8220;The domain model says Customer have Orders&#8230;&#8221; do you mean the domain experts say that ?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Andy Hitchman</title>
		<link>http://lostechies.com/jimmybogard/2010/04/14/injecting-services-into-entities/#comment-2342</link>
		<dc:creator>Andy Hitchman</dc:creator>
		<pubDate>Thu, 15 Apr 2010 11:31:47 +0000</pubDate>
		<guid isPermaLink="false">/blogs/jimmy_bogard/archive/2010/04/14/injecting-services-into-entities.aspx#comment-2342</guid>
		<description>@isaiah

So, if my Customer can&#039;t contain a set of Orders (another aggregate), where does the responsibility for getting orders for a customer lie? Are you suggesting I need a service that takes a customer object or a specification for a customer and return a set of orders?

Isn&#039;t having the &#039;GetOrders&#039; method on the Customer object preferable in terms of discoverability for maintenance in the future? 

The domain model says Customer have Orders, rather than needing to know that a service (/repository) can get Orders for Customers. (The GetOrders method is implemented to use this service internally)</description>
		<content:encoded><![CDATA[<p>@isaiah</p>
<p>So, if my Customer can&#8217;t contain a set of Orders (another aggregate), where does the responsibility for getting orders for a customer lie? Are you suggesting I need a service that takes a customer object or a specification for a customer and return a set of orders?</p>
<p>Isn&#8217;t having the &#8216;GetOrders&#8217; method on the Customer object preferable in terms of discoverability for maintenance in the future? </p>
<p>The domain model says Customer have Orders, rather than needing to know that a service (/repository) can get Orders for Customers. (The GetOrders method is implemented to use this service internally)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: isaiah</title>
		<link>http://lostechies.com/jimmybogard/2010/04/14/injecting-services-into-entities/#comment-2341</link>
		<dc:creator>isaiah</dc:creator>
		<pubDate>Thu, 15 Apr 2010 09:58:02 +0000</pubDate>
		<guid isPermaLink="false">/blogs/jimmy_bogard/archive/2010/04/14/injecting-services-into-entities.aspx#comment-2341</guid>
		<description>@andy
In my experience having to pass a domain service such as a repository into an enity, is usually a hint something I need to rethink my design choices. In general an aggregate root should not have non-transient references to other aggregate roots. In your example Order seems like an aggregate root and doesnt seem right for customer to be holding references on to this.
More over lifecycle of entities and services are completety different. . One of key benefits of tests is the feedback they provide and help guide your design, i&#039;m not a fan of tools like auto-mocking container and among others, they silence the feedback from test and simple hide underlying design issues</description>
		<content:encoded><![CDATA[<p>@andy<br />
In my experience having to pass a domain service such as a repository into an enity, is usually a hint something I need to rethink my design choices. In general an aggregate root should not have non-transient references to other aggregate roots. In your example Order seems like an aggregate root and doesnt seem right for customer to be holding references on to this.<br />
More over lifecycle of entities and services are completety different. . One of key benefits of tests is the feedback they provide and help guide your design, i&#8217;m not a fan of tools like auto-mocking container and among others, they silence the feedback from test and simple hide underlying design issues</p>
]]></content:encoded>
	</item>
</channel>
</rss>
