Note that there are some explanatory texts on larger screens.

plurals
  1. POServiceStack new service side by side ASP.NET MVC website
    text
    copied!<p>In the <a href="https://github.com/ServiceStack/ServiceStack.Examples" rel="noreferrer">examples</a> for ServiceStack I don't see a single application that is ASP.NET MVC website first and then made ServiceStack service second.</p> <p>Let's take a very simple ASP.NET MVC web application that renders products through Views. It uses controllers, views, models and viewmodels.</p> <p>Let's say we have a model of <code>Product</code> which gets persisted into a document DB. Let's assume we have a viewmodel of <code>ProductViewModel</code> which gets mapped from <code>Product</code> and display within MVC Razor View/PartialView.</p> <p>so this is a web side of things..now let's assume we want to add a service returning products to various clients like the Windows 8 applications.</p> <p>Should the request/response classes be completely disconnected from what we already have? Our <code>ProductViewModel</code> might already contain everything we want to return from the service.</p> <p>Since we already have <code>Product</code> (model class) we can't have another <code>Product</code> class in the API namespace..well we could but that makes things unclear and I'd like to avoid that.</p> <p>So, should we introduce standalone <code>ProductRequest</code> class and <code>ProductRequestResponse</code> (inherits ProductViewModel) class in the API namespace?</p> <p>Like so <code>ProductRequestResponse : ProductViewModel</code>?</p> <p>What i'm saying is, we already have the Model and ViewModel classes and to construct Request and Response classes for the SS service we would have to create another two files, mostly by copying everything from the classes we already have. This doesn't look DRY to me, it might follow the separation of concerns guidelines but DRY is important too, actually more than separating everything (separating everything leads to duplication of code).</p> <p>What I would like to see is a case where a web application has already been made, it currently features Models and ViewModels and returns the appropriate Views for display on the Web but can be extended into a fully functional service to support programmatic clients? Like AJAX clients etc...with what we already have.</p> <p>Another thing:</p> <p>If you take a look at this example <a href="https://github.com/ServiceStack/ServiceStack.Examples/blob/master/src/ServiceStack.MovieRest/MovieService.cs" rel="noreferrer">https://github.com/ServiceStack/ServiceStack.Examples/blob/master/src/ServiceStack.MovieRest/MovieService.cs</a></p> <p>you will see there is <code>Movie</code> Request class and <code>Movies</code> Request class (one for single movie request, the other one for a list of movies). As such, there are also two services, <code>MovieService</code> and <code>MoviesService</code>, one dealing with requests for a single movie, the other one for a genre of movies. </p> <p>Now, while I like SS approach to services and I think it is the right one, I don't like this sort of separation merely because of the type of request. What if I wanted movies by director? Would I be inventing yet another request class having a <code>Director</code> property and yet another service (<code>MoviesByDirector</code>) for it?</p> <p>I think the samples should be oriented towards one service. Everything that has to deal with movies need to be under one roof. How does one achieve that with ServiceStack?</p> <pre><code>public class ProductsService : Service { private readonly IDocumentSession _session; private readonly ProductsHelperService _productsHelperService; private readonly ProductCategorizationHelperService _productCategorization; public class ProductRequest : IReturn&lt;ProductRequestResponse&gt; { public int Id { get; set; } } // Does this make sense?  // Please note, we use ProductViewModel in our Views and it holds everything we'd want in service response also public class ProductRequestResponse : ProductViewModel { } public ProductRequestResponse GetProducts(ProductRequest request) { ProductRequestResponse response = null; if (request.Id &gt;= 0) { var product = _session.Load&lt;Product&gt;(request.Id); response.InjectFrom(product); } return response; } } </code></pre>
 

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