Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should ServiceStack be the service layer in an MVC application or should it call the service layer?

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?

like image 287
Craig A Avatar asked May 13 '12 11:05

Craig A


People also ask

What is use of service layer in MVC?

A service layer is an additional layer in an ASP.NET MVC application that mediates communication between a controller and repository layer. The service layer contains business logic. In particular, it contains validation logic. For example, the product service layer in Listing 3 has a CreateProduct() method.

How can add service in ASP NET MVC?

Right-click on the created ASP.NET MVC Application and click Add Service Reference, as shown below. Now, after clicking on the preceding Service reference option, it will show the Window given below. Now, click on advanced button.

What is a service MVC?

MVC (or Model-View-Controller) is an architectural pattern on which to build software. The basic idea in it is to separate internal data models from the user interface via the controller and view.


1 Answers

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.

like image 111
mythz Avatar answered Oct 16 '22 22:10

mythz