Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I would do neither. </p> <p>Ideally both MVC and ServiceStack should use and share pure C# dependencies. A good example of an MVC + ServiceStack website living harmoniously together is in the <a href="https://github.com/ServiceStack/SocialBootstrapApi/">SocialBootstrapApi</a> demo project, which has been deployed on AppHarbor at: <a href="http://bootstrapapi.apphb.com">http://bootstrapapi.apphb.com</a></p> <p>I would register all your dependencies in your ServiceStack AppHost then register an <a href="https://github.com/ServiceStack/SocialBootstrapApi/blob/master/src/SocialBootstrapApi/AppHost.cs#L134">MVC Controller factory</a> so both your MVC Controllers and ServiceStack services get auto-wired with these dependencies.</p> <p>In your AppHost:</p> <pre><code>void Configure(Funq.Container container) { container.Register&lt;IGreeter&gt;(c =&gt; new Greeter()); //Set MVC to use the same Funq IOC as ServiceStack ControllerBuilder.Current.SetControllerFactory( new FunqControllerFactory(container)); } </code></pre> <p>Example of ServiceStack service using <code>IGreeter</code></p> <pre><code>public class HelloService : Service { public IGreeter Greeter { get; set; } //Autowired public HelloResponse Get(Hello request) { return new HelloResponse { Result = Greeter.SayHelloTo(request.Name) }; } } </code></pre> <p>Example of MVC Controller using same IGreeter:</p> <pre><code>public HelloController : ServiceStackController { public IGreeter Greeter { get; set; } //Autowired public void Index(string name) { ViewBag.GreetResult = Greeter.SayHelloTo(name); return View(); } } </code></pre> <p>The general idea is for logic inside MVC Controllers and ServiceStack services should be concerned with the HTTP layer/integration point i.e. collecting the user input from the QueryString or FORM POST'ed variables and calling pure/testable C# logic with it then preparing the Response, in ServiceStack that would be populating the Response DTO whilst for an MVC Controller you would be populating the ViewModel.</p> <h3>Calling ServiceStack services from an MVC Controller</h3> <p>Although I would have Controllers + ServiceStack share functionality via a C# greet service above, you also can call a ServiceStack service from an MVC Controller like:</p> <pre><code>public HelloController : ServiceStackController { public void Index(string name) { using (var helloService = AppHostBase.ResolveService&lt;HelloService&gt;()) { ViewBag.GreetResult = helloService.Get(name).Result; return View(); } } } </code></pre> <h3>Share Session/Caching with the ServiceStackController</h3> <p>Although the MVC Controller examples inherit from the <a href="https://github.com/ServiceStack/ServiceStack/blob/master/src/ServiceStack.FluentValidation.Mvc3/Mvc/ServiceStackController.cs#L51">ServiceStackController</a>, it's not necessary but does allow you to share the same Session / Caching / Authentication + RequiredRole/RequiredPermission attributes in MVC and ServiceStack.</p> <p>See the <a href="http://www.servicestack.net/mvc-powerpack/">MVC PowerPack</a> for other benefits that ServiceStack brings to MVC.</p>
 

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