title: How to call operations other than CRUD in RIA Domain Service?
body: <p>I have some trouble getting my head around how to implement more complex operations in a Domain Service in RIA Services. This is all Silverlight 4, VS 2010 and .Net Framework 4 in Beta 2.</p>
  
  <h2>Goal</h2>
  
  <p>I wish I could create an operation on my LinqToEntitiesDomainService that would have a signature something like this: </p>
  
  <pre><code>public UnwieldyOperationResult PerformUnwieldyOperation( UnwieldyOperationParameters p );
  </code></pre>
  
  <p>The idea is that this operation takes a colleciton of parameters and performs rather complex operations which would update different instances and types of the entities that are otherwise manipulated through the DomainService CRUD functionality.</p>
  
  <h2>Problem</h2>
  
  <p>The immediate problem i hit is that does not seem to be allowed to pass the custom type as parameter to the method, and I suppose something along those lines go for the return value. I want to encapsulate the operation parameters in a DTO for clarity, and this unwieldy operation does not have any corresponding entity in the legacy database that I have wrapped with Entity Framework 4.0 model, which I am in turn basing the Domain Service on.</p>
  
  <p>Is a domain service supposed to deal with only the types that are entities in the underlying EF model? Is it not designed to expose more complex operations like my UnwieldyOperation?</p>
  
  <p>If so, can I build another service somehow that allows both the operation signature and to manipulate the entity framework? </p>
  
  <p>I have understood that only <em>one</em> Domain Service can handle an entity from the model. This has led me to cram all the CRUD and now also the UnwieldyOperation into one Domain Service, although my first idea was to split the service into smaller parts.</p>
  
  <p>If I'd get the operation to work with parameters and return value in the Domain Service, my next wish would be to have the entities that are already loaded in the domain context at the client refresh themselves. </p>
  
  <p>Is there any efficient mechanism for such a thing? </p>
  
  <p>How would you go about to do that?</p>
  
  <h2>What I have so far...</h2>
  
  <p>In short, this is what I have so far:</p>
  
  <ul>
  <li><p>I have wrapped an existing legacy database
  with an Entity Framework 4.0 model with as
  little extra padding/code as
  possible. This means right-click, add and generate from database. </p></li>
  <li><p>I have implemented the simpler CRUD operations in the DomainService and I am using them successfully to display and edit straight forward data. I have some encapsulation of logic through ViewModels in client but I expose the Entity classes directly, but I think this is unrelated to my problem/question. </p></li>
  <li><p>I have realized that I can't add the UnwieldyOperation in an as straight forward manner as I initially thought... Also I suspect/hope that I have misunderstood some aspects of the Domain Service mechanism, which has led me to the current situation.</p></li>
  </ul>
  
  <h2>One way to go?</h2>
  
  <p>Writing this down in a question like this gives me the idea that perhaps I'd go in this direction:</p>
  
  <ol>
  <li>LegacyModelService expose the CRUD operations as I have already done.</li>
  <li>Expose the Unwieldy operations in another service. Should I make that a RIA Doamin Service or just plain WCF?</li>
  <li>Access the Entity Framework model from the new UnwieldyOperationsService and manipulate the data layer there.</li>
  <li>Explicitly reload or refresh the client domain context for the LegacyModelService at the client to reflect the changes that may have resultet from the UnwieldyOperation. What would be a good way get this done?</li>
  </ol>