Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h2>The Service Layer is your most important Contract</h2> <p>The most important interface that you can ever create in your entire system is your external facing service contract, this is what consumers of your service or application will bind to, i.e. the existing call-sites that often won't get updated along with your code-base - every other model is secondary. </p> <h3>DTOs are Best practices for remote services</h3> <p>In following of <a href="http://martinfowler.com/eaaCatalog/dataTransferObject.html">Martin Fowler's recommendation for using DTOs</a> (Data Transfer Objects) for remote services (<a href="http://msdn.microsoft.com/en-us/library/ff649585.aspx">MSDN</a>), <a href="http://www.palmmedia.de/Blog/2011/8/30/ioc-container-benchmark-performance-comparison">ServiceStack</a> encourages the use of clean, untainted POCOs to define a well-defined contract with that should kept in a largely implementation and dependency-free .dll. The benefits of this allows you to be able to re-use typed DTOs used to define your services with, as-is, in your <a href="https://github.com/ServiceStack/ServiceStack/wiki/Clients-overview">C#/.NET clients</a> - providing an end-to-end typed API without the use of any code-gen or other artificial machinery.</p> <h3>DRY vs Intent</h3> <p>Keeping things DRY should not be confused with clearly stating of intent, which you should avoid trying to DRY or <a href="http://ayende.com/blog/4769/code-review-guidelines-avoid-inheritance-for-properties">hide behind inheritance</a>, magic properties or any other mechanism. Having clean, well-defined DTOs provides a single source of reference that anyone can look at to see what each service accepts and returns, it allows your client and server developers to start their work straight away and bind to the external service models without the implementation having been written. </p> <p>Keeping the DTOs separated also gives you the freedom to re-factor the implementation from within without breaking external clients, i.e. your service starts to cache responses or leverages a NoSQL solution to populate your responses with.</p> <p>It's also provides the authoritative source (that's not leaked or coupled inside your app logic) that's used to create the auto-generated metadata pages, example responses, Swagger support, XSDs, WSDLs, etc. </p> <h3><a href="https://github.com/ServiceStack/ServiceStack/wiki/Auto-mapping">Using ServiceStack's Built-in auto-mapping</a></h3> <p>Whilst we encourage keeping separate DTO models, you don't need to maintain your own manual mapping as you can use a mapper like <a href="https://github.com/AutoMapper/AutoMapper">AutoMapper</a> or using ServiceStack's built-in Auto Mapping support, e.g:</p> <p>Create a new DTO instance, populated with matching properties on viewModel:</p> <pre><code>var dto = viewModel.ConvertTo&lt;MyDto&gt;(); </code></pre> <p>Initialize DTO and populate it with matching properties on a view model:</p> <pre><code>var dto = new MyDto { A = 1, B = 2 }.PopulateWith(viewModel); </code></pre> <p>Initialize DTO and populate it with <strong>non-default</strong> matching properties on a view model:</p> <pre><code>var dto = new MyDto { A = 1, B = 2 }.PopulateWithNonDefaultValues(viewModel); </code></pre> <p>Initialize DTO and populate it with matching properties that are annotated with the <strong>Attr</strong> Attribute on a view model:</p> <pre><code>var dto = new MyDto { A=1 }.PopulateFromPropertiesWithAttribute&lt;Attr&gt;(viewModel); </code></pre> <p>When mapping logic becomes more complicated we like to use extension methods to keep code DRY and maintain the mapping in one place that's easily consumable from within your application, e.g:</p> <pre><code>public static class MappingExtensions { public static MyDto ToDto(this MyViewModel viewModel) { var dto = viewModel.ConvertTo&lt;MyDto&gt;(); dto.Items = viewModel.Items.ConvertAll(x =&gt; x.ToDto()); dto.CalculatedProperty = Calculate(viewModel.Seed); return dto; } } </code></pre> <p>Which is now easily consumable with just:</p> <pre><code>var dto = viewModel.ToDto(); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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