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.
Calling ServiceStack services from an MVC Controller
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();
}
}
}
Share Session/Caching with the ServiceStackController
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.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…