I'm creating an MVC website and also intend to create a web API for use both within the website and potentially by third parties.
From the MVC controllers I'll be calling into a service layer which will contain business logic, act on domain models, perform validation, make infrastructure external service calls etc. The service layer in turn will call into repositories for any database interactions.
Now, I like the look of ServiceStack and intend on using it for the Web API - it seems more mature than the ASP.NET MVC 4 Web API. My question is, should I have the ServiceStack API call into my service layer above, similar to the MVC controllers, or should I make it the service layer, servicing both web clients and the MVC controllers?
I would do neither.
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 SocialBootstrapApi demo project, which has been deployed on AppHarbor at: http://bootstrapapi.apphb.com
I would register all your dependencies in your ServiceStack AppHost then register an MVC Controller factory so both your MVC Controllers and ServiceStack services get auto-wired with these dependencies.
In your AppHost:
void Configure(Funq.Container container) {
container.Register<IGreeter>(c => new Greeter());
//Set MVC to use the same Funq IOC as ServiceStack
ControllerBuilder.Current.SetControllerFactory(
new FunqControllerFactory(container));
}
Example of ServiceStack service using IGreeter
public class HelloService : Service {
public IGreeter Greeter { get; set; } //Autowired
public HelloResponse Get(Hello request) {
return new HelloResponse {
Result = Greeter.SayHelloTo(request.Name) };
}
}
Example of MVC Controller using same IGreeter:
public HelloController : ServiceStackController {
public IGreeter Greeter { get; set; } //Autowired
public void Index(string name) {
ViewBag.GreetResult = Greeter.SayHelloTo(name);
return View();
}
}
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.
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:
public HelloController : ServiceStackController {
public void Index(string name)
{
using (var helloService = AppHostBase.ResolveService<HelloService>())
{
ViewBag.GreetResult = helloService.Get(name).Result;
return View();
}
}
}
Although the MVC Controller examples inherit from the ServiceStackController, it's not necessary but does allow you to share the same Session / Caching / Authentication + RequiredRole/RequiredPermission attributes in MVC and ServiceStack.
See the MVC PowerPack for other benefits that ServiceStack brings to MVC.