title: WCF data contract design with dependency injection
body: <p>So I have a layered application that I am adding a WCF service interface on top of.  The service is simply a facade with all of our business logic already existing in Business Objects (BOs) within the Business Logic Layer (BLL) which is a class library.  Within the BLL we use constructor injection to inject dependencies into the BOs.  This is all working with good unit testing, etc.  On to the problem...</p>
  
  <p>Ordinarily I'd simply create a set of Request/Response objects as DataContracts for each service method with the appropriate properties for the operation.  If the operation required one of our "entities" to be passed either to or from the method, I'd simply define a property of that type and everything would be fine (all of our BOs are serializable).  However when one of these "entities" is passed into a service method, WCF deserializes the object without ever invoking the constructors we've defined and, as a result, the dependencies don't resolve.</p>
  
  <p>Let's use the case of a service method called <strong>CreateSomething</strong>.  I'd normally define this as a service operation with a signature like:</p>
  
  <pre><code>CreateSomethingResponse CreateSomething(CreateSomethingRequest request);
  </code></pre>
  
  <p><strong>CreateSomethingRequest</strong> would be a DataContract and have amongst its properties a property of type <strong>Something</strong> that represented the "entity" being passed into the service.  <strong>Something</strong>, in this case, is a business object that expects to receive an instance of the <strong>ISomethingRepository</strong> interface from the DI container when instantiated - which, as I said above, does not happen when WCF deserializes the object on the server.</p>
  
  <p>Option #2 is to remove the <strong>Something</strong> property from the DataContract and define each of the properties explicitly in my DataContract then inside my service method, create a new instance of the <strong>Something</strong> class, letting the container inject the dependency, then map the property values from the DataContract object into the BO.  And I can certainly do that but I am concerned about now having two places to make changes if, say, I want to add a property to the <strong>Something</strong> type.  And, with a lot of properties, there's a lot of code duplication.</p>
  
  <p>Has anyone crossed this bridge and, if so, can you share your thoughts and how you have or would approach this situation in your own applications?  Thx!!!</p>